MYSQL - multi-dimensional array - loop or recursion?

You could use references if you don't mind fetching everything in 1 go (2 queries) (probably the total amount of tags should be below a couple of hundred, and you could cache the resulting construct for a while):

 //fill tags
 $tags[] = array()
 foreach($pdo->query('SELECT * FROM tags')->fetchAll(PDO::FETCH_ASSOC) as $tag){
    $tags[$tag['id']] = $tag;

 //add relationships
 $relation_query = $pdo->query("SELECT * FROM relationships 
   WHERE x_table='tags'  AND y_table = 'tags'");
 foreach($relation_query->fetchAll(PDO::FETCH_ASSOC) as $relationship){
   //ensure targets exists:
   if(!isset($tags[$relationship['x_id '])) $tags[$relationship['x_id '] = array();
   if(!isset($tags[$relationship['y_id '])) $tags[$relationship['y_id '] = array();

   if(!isset($tags[$relationship['x_id ']['children'])) 
     $tags[$relationship['x_id ']['children'] = array();       

   //add as reference
   $tags[$relationship['x_id ']['children'] = &$tags[$relationship['y_id ']; 

 //pick the tag you want:

Not that a closure table , or just path enumeration(slide 18 onwards, but the whole thing is worth a read) will give you even bigger performance, and the possibility to just query for a subtree. Big props to Bill Karwin.

If you can reorganize your array, you can stick the categories as keys, subcategories as values:

$categories = array(
        'sport' => array(
               "surfing", "basketball", "nfl", "afl"
        'Automotive' => array(
               "cars", "f1", "bikes", "etc"
        'comedy' => array(
               "stand up", "pranks", "...", "test"
 // Here is an imploded list, you don't need a second loop then
 foreach($categories as $cat => $subcat) {
        echo "<h1>".$cat."</h1>";
        echo '<ul>';
        echo '<li>'.implode('</li>'.PHP_EOL.'<li>',$subcat).'</li>';
        echo '</ul>';

As far as i know there is not foeach loop in c++. Use two for loops instead. Something like this

for (int i = 0; i < 9; ++i)
   for (int j = 0; j < 16; ++j)
     BYTE value = CPUKeys[i][j];

Check if value is an array. If yes loop it too.

if (is_array($value)) foreach ($value as $value2) {}
for($i=0; $i<$data["count"]; $i++) {
    if(!isset($data[$i]["proxyaddresses"])) continue;
    foreach($data[$i]["proxyaddresses"] as $object) {

Message :
Login to Add Your Comments .
Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © All Rights Reserved .