I am having a problem accessing an object in a multidimensional array.
THE CONTEXT
Basically, I have an object (category) which consists of Name, ID, ParentID and more. I also have an array ultimateArray which is multidimentional.
For a given category, I am writing a function (getPath()) that will return an array of ids. For example, an object named Granny Smith has a parentID of 406 and is therefore a child of Food(5) -> Fruits(101) -> Apples(406). The function will return either an array or string of the ids of the objects parents. In the above example this would be: 5 -> 101 -> 406 or ["5"]["101"]["406"] or [5][101][406]. Food is a root category!
THE PROBLEM
What I need to do is use whatever is returned from getPath() to access the category id 406 (Apples) so that I can add the object Granny Smith to the children of Apples.
The function $path = $this->getPath('406'); is adaptable. I am just having difficulty using what is returned in the following line:
$this->ultimate[$path]['Children'][]= $category;
It works when I hard code in:
$this->ultimate["5"]["101"]["406"]['Children'][]= $category;
//or
$this->ultimate[5][101][406]['Children'][]= $category;
Any help is much appreciated.
getPath()returns?getPath()can return anything. At the moment I return an array["5"]["101"]["406"]but can easily change the code to return a string of indexes or anything else.["5"]["101"]["406"]isn't an array at all! So, are you returning the element inside that slice of array? Or a string that contains what you had wrote? What ? :)getPath()function is not the issue I am having - it simply creates an array or list of the indexes to access the relevant category. What I am struggling with is how to use these indexes to access the parent category ofGranny Smith. Please see updated question which details what works when hard coded in.