cartfredri Posted June 11, 2016 Share Posted June 11, 2016 Hi Larry, i'm quite new to the forum, just have a quick question from chapter one from the pursue section, basically how would I display each element in the multidimentional array? without using print_r. Would I use a foreach loop to loop through element or within a function use a array_key_exist to test if key exist, then if true return that data from array please let me know. regards, fred Link to comment Share on other sites More sharing options...
cartfredri Posted June 17, 2016 Author Share Posted June 17, 2016 hello, can someone please let me know. fred. Link to comment Share on other sites More sharing options...
Larry Posted June 18, 2016 Share Posted June 18, 2016 Sorry about the delay! Behind schedule keeping up with the forums! It sounds like you have the basic grasp of looping through a multidimensional array. If you know the depths of the array, you can use X number of nested foreach loops. Otherwise you'll need to use a recursive function or http://php.net/array_walk_recursive, which is a shortcut there. The PHP Advanced book does have a recursive example of example this. Link to comment Share on other sites More sharing options...
cartfredri Posted September 15, 2016 Author Share Posted September 15, 2016 Sorry about the delay! Behind schedule keeping up with the forums! It sounds like you have the basic grasp of looping through a multidimensional array. If you know the depths of the array, you can use X number of nested foreach loops. Otherwise you'll need to use a recursive function or http://php.net/array_walk_recursive, which is a shortcut there. The PHP Advanced book does have a recursive example of example this. Hi Larry, been away for summer, just a quick one you know the 'array_walk_recursive' function how would I capture the Keys which are also arrays in the the multi-dimensional array because I used the array_map and it worked with the call back function but the key which are arrays didnt display for example could I say in user defined function '$key1 => $key2' => $value' I know this isnt quite right but i'm a bit unsure, please let me know. bless, fred. Link to comment Share on other sites More sharing options...
Larry Posted September 20, 2016 Share Posted September 20, 2016 I'm confused by what you mean when you say the keys are also arrays. I'm pretty sure that's not possible: keys must be singular values. 1 Link to comment Share on other sites More sharing options...
cartfredri Posted September 21, 2016 Author Share Posted September 21, 2016 I mean where you say for example '245 => array()' I want to display this without needing to use 'print_r' or 'var_dump' and use a custom call back function in 'array_walk_recursive' Link to comment Share on other sites More sharing options...
Larry Posted November 13, 2016 Share Posted November 13, 2016 If you're using http://php.net/array_walk_recursive, you write your callback to accept two parameters: the value and the key. If you're still having problems with this or confused, could you share the code you're trying to use that isn't working? Link to comment Share on other sites More sharing options...
Recommended Posts