0

Below given is my array structure

Array
(
[0] => Array
    (
        [product_id] => 59
        [product_name] => Samsung Champ DUOS E2652 
        [product_price] => 4439
        [sub_cat_name] => Samsung
        [sub_cat_url] => samsung
        [image] => samsung-champ-duos-e2652---1414801404364308.jpg
        [currency] => ₹
    )

[1] => Array
    (
        [product_id] => 195
        [product_name] => Samsung Galaxy Tab 4 7.0
        [product_price] => 17847
        [sub_cat_name] => Samsung
        [sub_cat_url] => samsung
        [image] => galaxy-tab-4-7-0---1715601405057269.png
        [currency] => ₹
    )

[2] => Array
    (
        [product_id] => 284
        [product_name] => Samsung Galaxy Tab 3 Lite 7.0
        [product_price] => 18000
        [sub_cat_name] => Samsung
        [sub_cat_url] => samsung
        [image] => galaxy-tab-3-lite-7-0---1590061405576878.png
        [currency] => ₹
    )

[3] => Array
    (
        [product_id] => 19
        [product_name] => Samsung Galaxy Star
        [product_price] => 4833
        [sub_cat_name] => Samsung
        [sub_cat_url] => samsung
        [image] => feature-img144811404109906.jpg
        [currency] => ₹
    )

[4] => Array
    (
        [product_id] => 186
        [product_name] => Samsung Galaxy S5 Active
        [product_price] => 31922
        [sub_cat_name] => Samsung
        [sub_cat_url] => samsung
        [image] => galaxy-s5-active368091404989896.png
        [currency] => ₹
    )

)

i would like to sort this array based on sub_cat_url like below

array
(
[0] => samsung
   [0] => Array
    (
        [product_id] => 186
        [product_name] => Samsung Galaxy S5 Active
        [product_price] => 31922
        [sub_cat_name] => Samsung
        [sub_cat_url] => samsung
        [image] => galaxy-s5-active368091404989896.png
        [currency] => ₹
    )
    [1] => Array
    (
        [product_id] => 284
        [product_name] => Samsung Galaxy Tab 3 Lite 7.0
        [product_price] => 18000
        [sub_cat_name] => Samsung
        [sub_cat_url] => samsung
        [image] => galaxy-tab-3-lite-7-0---1590061405576878.png
        [currency] => ₹
    )
)

for thet I wrote a code like below.

for($i = 0; $i < $count; $i++){
            if($i == 0){
                $search_array[$i] = $search_data[$i]["sub_cat_name"];
                $search_array[$i] = $search_data[$i]["product_name"];
                $j += 1;
            }
            else if($search_data[$i]["sub_cat_url"] !== $search_data[$i]["sub_cat_url"]){
                $search_array[$i] = array($search_data[$i]["sub_cat_name"]);
                $search_array[$i] = array($search_data[$i]["product_name"]);
                $j += 1;
            }
            else if($search_data[$i]["sub_cat_url"] === $search_data[$i]["sub_cat_url"]){
                $search_array[$i] = array($search_data[$i]["product_name"]);
                $j += 1;
            }
        }

but it didn't work well. And I tried using inner for loop also. It too didn't give me the actual result. Can some one please hlp me to write the correct loop statement for this? Thank you in advance

5
  • 1
    You want to get products filtered by sub_cat_url field ? Commented Jul 18, 2014 at 6:24
  • yeah..exactly..sorry...forget to add in question..will edit now Commented Jul 18, 2014 at 6:27
  • 1
    Possible duplicate: stackoverflow.com/questions/24724430/… Commented Jul 18, 2014 at 6:29
  • 1
    Can't you get it directly from database by passing your subcategory ? Or you still need it by coding Commented Jul 18, 2014 at 6:29
  • I need it by coding.. Commented Jul 18, 2014 at 6:30

1 Answer 1

1

If I understood you well this should work:

$out = array();

$map = array();

foreach ($array as $k => $v) {
   $url = $v['sub_cat_url'];

   if (!isset($map[$url])) {
     $map[$url] = count($out);
   }
   $out[$map[$url]][$url][$k] = $v; // or $out[$map[$url]][$url][] = $v;

}

However I haven't tested it (you haven't provided data in PHP format)

Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much for the help...It works 100% as i expected... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.