I have an array that carries a definite number of dimensions so I’m not really looking at something recursive. It’s a numeric array gotten from the database with each row holding another array. Each of those level 2 arrays contain strings like var1, var2 , var3
And so on. Note the irregular appearance of commas in the string. So I intend to break the comma delimited string in the third level then log them in the final array but I get an error saying I am supplying an null array. My code goes below:
how does the array look like? in which part of the script does the error occur? did you check the contents at the given part of the array? you can still use explode() and trim() for the csv.
It occurs in that loop where I’m doing array_push(); on that particular line, it says the array I’m giving it is null. The array I’m giving it is $temp and from code, it clearly is not null.[quote=“chorn, post:2, topic:228432”]
you can still use explode() and trim() for the csv.
[/quote]
Won’t this be longer while still getting the same result? All the same, whichever would get it work seems fine by me.
explode/trim may be more clear but if the regex works thats fine. but for you using a closure, which is a function, which has its own scope, $temp is not available, because you did not pass it as a function parameter, so its null. take the use statement to make it accessible, as mentioned by directrix1 at gmail dot com in the manual of array_Reduce()
How does this work when we clearly do not go down level one? The preg_split should throw a fatal error for being supplied an array instead of a string.
It’s because we do go down multiple levels due to array_walk_recursive. This function visits each element in the passed array. If an element is not an array it will be passed to the user defined function. Otherwise it’s passed to array_walk_recursive for iteration. As the documentation says:
Any key that holds an array will not be passed to the function.
In effect all the strings in the structure will be passed to the user defined function regardless of how deep they are buried.
WONDERFUL!!.So there is no need for writing php functions to handle n-dimensional arrays. Thank you for stopping by.
This sort of makes my head spin since I actually tried passing that same value to the method and it complained I was passing it an array instead of a string. All the same, I may just be content with the recursive walk if it would be a pain explaining further. Thank you for stopping by.