php - values not push on recursive array -
Let me explain my problem I try to generate an array of categories.
This is my job.
Personal function recursive concert ($ id_parent, $ level, $ ranges) {$ tree = array (); Foreign exchange ($ categories as category $) {if ($ id_parent == $ taxonomy ['id_parent']) {$ tree [$ level] [] = $ categorie; $ This- & gt; Recurring categories trade ($ categorie ['id_category'], ($ level + 1), $ ranges); }} $ Tree returned; } When I locate the loop with the echo, everything works, all parent categories and girls are covered, but they are not pushed into the array.
here ([id_category] = & gt; 4 [name] = & gt; birds de tachà © es [id_parent] => 1) [1] = & gt; Array ([id_category] => 5 [name] = & gt; Aquavaer [id_pay] => 4) [2] = & gt; Array [id_category] = & gt; 6 [name] = & gt; Sà © rie 100 [id_parent] = & gt; 5) [3] = & gt; Array ([id_category] = & gt; 7 [name] = & gt; [id_parent] = & gt; 6) [4] = & gt; Array ([id_category] => 8 [name] => Systemam Hydraulic [id_parine => 7] [5] = & gt; Array ([id_category] = & gt; 9 [name] = & gt; sà © rie 200 [id_parent] => 5) [6] = & gt; Array ([id_category] = & gt; 10 [name] => thru [id_parent] => 6) [7] = & gt; Array ([id_category] => 11 [name] => coincidence [id_paray] => 4))
Here is the result of the code> PR int_r generated
array ([0] => array ([0] => array ([id_category] => 5 [name] = & gt; Aquaventer [id_paray] => 4) [([id_category] => [[id_category] => 11 [name] = & gt; compaction [id_parine => 4))) < / Pre> I call my function like this
$ tree = $ this- & gt; Recursive links (4, 0, $ ranges) What's the problem? Either get your return value from your recursive call and surrender it, or instead of making a personal property of a class named tree and pushing the values on it, you can use the variable on recursive function calls $ Tree are not passing. e.g. If you do this then it will work: ( Edit : Fixed ... [again]) Private $ catTree = array (); Private $ catStartLevel = FALSE; Personal Function Recursive Content Tree ($ ID_perrent, $ Level, $ Categories) {If ($ this-> CatStartLevel! == Incorrect) $ this- & gt; CatStartLevel = $ level; Foreign Currency ($ category as category $) {if ($ id_parent == $ taxonomy ['id_parent']) {$ this-> Catree [$ level] [] = $ categorie; $ This- & gt; Recurring categories trade ($ categorie ['id_category'], ($ level + 1), $ ranges); }} If ($ this-> CatStartLevel === $ level) {$ tree = $ this- & gt; CatTree; $ This- & gt; CatTree = array (); $ This- & gt; CatStartLevel = FALSE; } $ Tree returned; } ... although this is not good because now you have 'useless' private property in your class. You would be better to change the array structure, and $ this-> Recursive Concert () ... Edit Thinking about it, if you really want an array in that structure, You probably want to pass the variable to populate the array:
Personal Function Recursive Concerty ($ Id_parent, $ level, $ ranges, & amp; $ tree) {foreach (Dollar categories $ Ranges) {if ($ id_parent == $ categorie ['id_parent']) {$ tree [$ level] [] = $ categorie; $ This- & gt; Recurring categories tari ($ classified ['id_category'], ($ level + 1), $ ranges, $ tree); }}} ... and then you call it this way ...
$ myTree = array (); $ Obj- & gt; Recursive links ($ id_parent, $ level, $ ranges, $ myTree); Print_r ($ myTree);
Comments
Post a Comment