note 86010 added to function.array-walk-recursive
| From: | martin dot velek at gmail dot com | Date: | Sun, 28 Sep 2008 21:01:18 +0000 |
| Subject: | note 86010 added to function.array-walk-recursive | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-144866@lists.php.net to get a copy of this message | ||
If you will know that a tree in DB is not huge you can use array_walk_recursive to create tree
structure e.g. in array.
let's assume following table:
NAME PARENT
a null
b null
c null
a1 a
a2 a
b1 b
b11 b1
b111 b11
<code>
require_once 'Zend/Db/Adapter/Pdo/Sqlite.php';
function treeFunc(&$item, $key, $dbConn)
{
$res = $dbConn->fetchPairs('SELECT name FROM tree where parent = ?',$key);
if(!empty($res))
$item = $res;
}
$arr = $dbConn->fetchPairs('SELECT name FROM tree where parent is null'); //
fetchPairs -> value is the index
$valid = true;
while($valid)
{
$oldCount = count($arr,COUNT_RECURSIVE);
array_walk_recursive($arr, 'treeFunc',$dbConn);
$newCount = count($arr,COUNT_RECURSIVE);
if ($oldCount == $newCount) // if there is no change, exit...
$valid = false;
}
print_r($arr);
</code>
--------------------------------------------------
Array
(
[a] => Array
(
[a1] =>
[a2] =>
)
[b] => Array
(
[b1] => Array
(
[b11] => Array
(
[b111] =>
)
)
)
[c] =>
)
----
Server IP: 147.32.160.17
Probable Submitter: 88.146.73.78
----
Manual Page -- http://www.php.net/manual/en/function.array-walk-recursive.php
Edit -- https://master.php.net/note/edit/86010
Del: integrated -- https://master.php.net/note/delete/86010/integrated
Del: useless -- https://master.php.net/note/delete/86010/useless
Del: bad code -- https://master.php.net/note/delete/86010/bad+code
Del: spam -- https://master.php.net/note/delete/86010/spam
Del: non-english -- https://master.php.net/note/delete/86010/non-english
Del: in docs -- https://master.php.net/note/delete/86010/in+docs
Del: other reasons-- https://master.php.net/note/delete/86010
Reject -- https://master.php.net/note/reject/86010
Search -- https://master.php.net/manage/user-notes.php