I'm trying to access a multidimensional array, and append to its sub-keys, but am having a hard time coming up with a function to do so.
Currently my array looks like this, with the key being the parent_id of the folder.
array (size=3)
0 =>
object(stdClass)[25]
public 'id' => string '18' (length=2)
18 =>
array (size=3)
19 =>
object(stdClass)[28]
public 'id' => string '19' (length=2)
20 =>
object(stdClass)[29]
public 'id' => string '20' (length=2)
21 =>
object(stdClass)[30]
public 'id' => string '21' (length=2)
19 =>
array (size=1)
24 =>
object(stdClass)[31]
public 'id' => string '24' (length=2)
What I've tried:
function getChildren($folder_id)
{
$folder_cursor = $this->db->get_where("folder", array("id" => $folder_id));
if ($folder_cursor->num_rows() > 0) {
$row = $folder_cursor->row();
array_push($this->temp, $row);
$this->recursiveGetChildren($row->id);
}
}
function recursiveGetChildren($parent_id)
{
$q = $this->db->get_where("folder", array("parent" => $parent_id));
if ($q->num_rows() > 0) {
$this->temp[$parent_id] = array();
foreach($q->result() as $q) {
$this->temp[$parent_id][$q->id] = $q;
$this->recursiveGetChildren($q->id);
}
}
}
I would like for the array to look like this:
array (size=3)
0 =>
object(stdClass)[25]
public 'id' => string '18' (length=2)
18 =>
array (size=3)
19 =>
array (size=2)
0=>
object(stdClass)[28]
public 'id' => string '19' (length=2)
24 =>
object(stdClass)[31]
public 'id' => string '24' (length=2)
20 =>
object(stdClass)[29]
public 'id' => string '20' (length=2)
21 =>
object(stdClass)[30]
public 'id' => string '21' (length=2)
Edited for clarity.