0

Basically I'm trying to group my arrays like this:

Shopping
   Amazon
Social
   Amoeblo
   American express

By using below PHP code:

<?php

echo '<ul id="list"><h2 class="searchresults"></h2>';
foreach($records as $catval) {
  $sitechar = $catval->site_category; 
  echo '<h3 id="disappear">'. strtoupper($sitechar) .'</h3>';       
  echo '<li class="siteli"><a href="#" class="add">'; 
  echo '<p id="text-site">'.$catval->site_name. '</p></a>';
  echo '</li>'; 
}
echo '</ul>';
?>

But I'm getting values only like below.

Shopping
       Amazon
Social
       Amoeblo
Social
       American express

I'm not getting the exact PHP sorting to use for this.

Carolina
  • 207
  • 2
  • 8
  • 23
  • Try to put a variable $last_category which always store your last parent category. Then before showing parent category compare it with the last displayed one .. if they are the same .. don't display it again. Post the code here if you have issues. – Sorin Trimbitas Jan 09 '13 at 10:45
  • 2
    Can you show the database query you're using to retrieve the data? Edit it into your question please. – halfer Jan 09 '13 at 10:47
  • Why did you accept it if does not worked? http://stackoverflow.com/questions/14233428/group-php-array-by-key-value – Pedro Gabriel Jan 09 '13 at 10:48
  • @PedroGabriel - I think you meant this link: http://stackoverflow.com/questions/14232483/php-sort-array-with-key-value – halfer Jan 09 '13 at 10:50
  • you probably need GROUP BY clause in you Query – Fawad Ghafoor Jan 09 '13 at 10:50
  • Voted to close as exact duplicate. – halfer Jan 09 '13 at 10:51
  • @halfer, thanks I think copied this question one because both looks like the same thing... – Pedro Gabriel Jan 09 '13 at 10:53
  • possible duplicate of [Group array by keys value](http://stackoverflow.com/questions/7410285/group-array-by-keys-value) – T.Todua Mar 15 '15 at 15:39

3 Answers3

2

I would create a new array with categories as keys for arrays with the sites.

<?php
$arr = array();

// First create multidimensional array with categories as keys for site arrays
foreach($records as $catval) {
    $sitechar = $catval->site_category;
    if (!array_key_exists($sitechar, $arr)) {
        // Set new array for a category if it does not exist
        $arr[$sitechar] = array();
    }

    // Add site to category
    $arr[$sitechar][] = array(
        "name"=>$catval->site_name,
        "image"=>$catval->site_img
    );
}

// Then iterate the new array of categories
echo ("<ul>");
foreach($arr as $category => $sites) {
    echo("<h3>" . $site_category "</h3>");

    // Iterate array of sites
    foreach($sites as $site) {
        echo("<li>" . $site["name"] . "-" ,  $site["image"] . "</li>");
    }
}
echo("</ul>");
?>
Esben
  • 1,943
  • 1
  • 18
  • 37
1

You can do it using a foreach and ksort

Let $your_array be the array you mentioned above

$res_array    = array();
foreach($your_array as $val){
   $res_array[$val->site_category][] = $val->site_name;
}
ksort($res_array);

print_r($res_array);

OR search for multisort in php which will solve your problem :)

Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
0
$tmp = null;

   echo '<ul id="list"><h2 class="searchresults"></h2>';
     foreach($records as $catval) {
       $myHtml = makeHtml($catval,$tmp);
       echo  $myHtml;
      $tmp = $catval->site_category;
     }
   echo '</ul>';


 function makeHtml($catval,$tmp){
       if($tmp != $catval->site_category){ $html .= '<h3 id="disappear">'. strtoupper($catval->site_category) .'</h3>';}
       $html .='<li class="siteli"><a href="#" class="add"><p id="text-site">'.$catval->site_name. '</p></a></li>'; 
       return $html;
 }
Fawad Ghafoor
  • 6,039
  • 7
  • 41
  • 53