Merhabalar, PHP ile seviyeli bir ilişkimiz var tabi şu sıralar kendisine biraz sövmekteyim. Zira kendileri çok fazla saçmalık içermekte. Herneyse, konumuza dönelim. Evet, maalesef, üzülerek söylüyorum; PHP değişken referansları hani o
&$degisken dediğimiz zımbırtılar düzgün çalışmıyor. Zend’ci abilerimiz Zend Engine’i böyle yapmış işte. Şimdi örneğimize gelelim;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
<?php $root = array( 'nodes'=>array( '1'=>array( 'id'=>'1', 'nodes'=>array( '4'=>array( 'id'=>'4', 'nodes'=>array( '5'=>array( 'id'=>'5', 'nodes'=>array() ) ) ) ) ), '2'=>array( 'id'=>'2', 'nodes'=>array() ), '3'=>array( 'id'=>'3', 'nodes'=>array() ) ) ); foreach ($root['nodes'] as $_node_id => &$_root_node) { $_put_parent = function (&$_node) use (&$_put_parent) { foreach ($_node['nodes'] as $_sub_node_id => &$_sub_node) { $_put_parent($_sub_node); $_sub_node['parent'] = $_node; } }; $_root_node['parent'] = null; $_put_parent($_root_node); } echo '<pre>'; var_dump($root['nodes']['1']['nodes']['4']); var_dump($root['nodes']['1']['nodes']['4']['nodes']['5']['parent']); echo '</pre>'; ?> |
Pek popüler PHP’miz burada tabiri caizse sıçıyor. Sonuç tam bir hüsran;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
array(3) { ["id"]=> string(1) "4" ["nodes"]=> &array(1) { [5]=> array(3) { ["id"]=> string(1) "5" ["nodes"]=> array(0) { } ["parent"]=> array(2) { ["id"]=> string(1) "4" ["nodes"]=> *RECURSION* } } } ["parent"]=> array(3) { ["id"]=> string(1) "1" ["nodes"]=> &array(1) { [4]=> *RECURSION* } ["parent"]=> NULL } } array(2) { ["id"]=> string(1) "4" ["nodes"]=> &array(1) { [5]=> array(3) { ["id"]=> string(1) "5" ["nodes"]=> array(0) { } ["parent"]=> *RECURSION* } } } |
…
Read the full article