codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

MYSQL - multi-dimensional array - loop or recursion?


By : , Category : php

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:
 var_dump($tags[1]);
 var_dump($tags[4]);

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.

ReLated :

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) {
        print_r($object);   
    }
}
Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .