1

Having a problem here that maybe someone can help me with. Hours of searching couldn't find the solution so I'm at my wit's end.

I have an array,

$array = array(
array('id' => 1, 'parent_id' => 0, 'name' => 'Main'),
array('id' => 2, 'parent_id' => 0, 'name' => 'Something'),
array('id' => 3, 'parent_id' => 1, 'name' => 'Child of Main'),
array('id' => 4, 'parent_id' => 3, 'name' => 'Child of Child of Main'),
...
);

I want a function that will give me all of the ids of the parents for a given node, i.e.

$ids = getIDs(4);

would return an array of {3, 1}

Any suggestions? Thank you in advance.

0

3 Answers 3

1

I've made something similar to RiaD (but on my own). It works well with the structure you already have.

<?php
$array = array(
array('id' => 1, 'parent_id' => 0, 'name' => 'Main'),
array('id' => 2, 'parent_id' => 0, 'name' => 'Something'),
array('id' => 3, 'parent_id' => 1, 'name' => 'Child of Main'),
array('id' => 4, 'parent_id' => 3, 'name' => 'Child of Child of Main'),
);
$parents = array();
function getIDs($id){
    global $array, $parents;
    $tmp = $array[$id-1]['parent_id'];
    if($tmp){
        array_push($parents, $tmp);
        getIDs($tmp);
    }
}
getIDs(4);
var_dump($parents);
?>
3
  • I don't understand what you mean. Can you show an example when it won't work?
    – user882255
    Aug 7, 2011 at 23:24
  • array('id' => 5, 'parent_id' => 0, 'name' => 'Main'), array('id' => 7, 'parent_id' => 0, 'name' => 'Something'), array('id' => 15, 'parent_id' => 5, 'name' => 'Child of Main'), array('id' => 23, 'parent_id' => 15, 'name' => 'Child of Child of Main'), (I think id are auto-generaetd and maybe deleted)
    – RiaD
    Aug 7, 2011 at 23:25
  • Well, I assumed that first element in the main table has id "1", etc. If the main table won't look like in question it won't work. Then you are right.
    – user882255
    Aug 7, 2011 at 23:32
1

Is it possible to make structure like this?

 $array = array(
1=>array('id' => 1, 'parent_id' => 0, 'name' => 'Main'),
2=>array('id' => 2, 'parent_id' => 0, 'name' => 'Something'),
3=>array('id' => 3, 'parent_id' => 1, 'name' => 'Child of Main'),
4=>array('id' => 4, 'parent_id' => 3, 'name' => 'Child of Child of Main'),
...
);

with or without 'id' inside array

This one should works with this structure

function getIds($array,$x){
    if(!$array[$x]['parent_id'])
        return array();
    else
        return array_merge(array($array[$x]['parent_id']),getIds($array,$array[$x]['parent_id']));
}
8
  • I do it: var_dump(getIDs($array,4)); and it doesn't work for me. I get NULL as var_dump and a warning: array_merge(): Argument #1 is not an array.
    – user882255
    Aug 7, 2011 at 23:47
  • I changed first argument of array_merge to array($array[$x]['parent_id']). Then I get a notice: Undefined offset: 15 and only one element as var_dump -- 15.
    – user882255
    Aug 7, 2011 at 23:50
  • @Pawel, Oh,you are right, it was some error, edited, you can test now
    – RiaD
    Aug 7, 2011 at 23:50
  • @Pawel: about Undefined offset, there are should be no such error? can you show me error test?
    – RiaD
    Aug 7, 2011 at 23:53
  • It still doesn't work. After first execution of getIDs function $array[$x]['parent_id'] is 15. You execute getIDs(15) but this element doesn't exist. You should execute getIDs(3) but I don't know how to correct it. Only solution I see is a loop inside getIDs function...
    – user882255
    Aug 7, 2011 at 23:56
-1

you can try foreach loop or use array_count_values

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.