noob push/merge problem

noob push/merge problem

am 01.12.2006 19:18:04 von zac.carey

I know this is probably completely straightforward but how do can adapt
this function so as to merge the results of the recursive queries into
a single, flat array?

function display_children($parent) {
if($parent == 'null'){
$query = "SELECT `id` FROM `task` WHERE ISNULL(`parent`);";}else{

$query = "SELECT `id` FROM `task` WHERE `parent`= '$parent';";}
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
display_children($row['id']);
}
}

So if the results of the 1st query was 1,2,3
and the results of the first recursion was 11,21,31
and the results of second recursion was 118,213,315

I'd end up with array(1,2,3,11,21,31,118,213,315)

As always, any help appreciated.

Re: noob push/merge problem

am 02.12.2006 00:19:08 von zac.carey

strawberry wrote:
> I know this is probably completely straightforward but how do can adapt
> this function so as to merge the results of the recursive queries into
> a single, flat array?
>
> function display_children($parent) {
> if($parent == 'null'){
> $query = "SELECT `id` FROM `task` WHERE ISNULL(`parent`);";}else{
>
> $query = "SELECT `id` FROM `task` WHERE `parent`= '$parent';";}
> $result = mysql_query($query);
> while ($row = mysql_fetch_array($result)) {
> display_children($row['id']);
> }
> }
>
> So if the results of the 1st query was 1,2,3
> and the results of the first recursion was 11,21,31
> and the results of second recursion was 118,213,315
>
> I'd end up with array(1,2,3,11,21,31,118,213,315)
>
> As always, any help appreciated.

here's my stab at it. it doesn't work - but i'm not sure why not:

function display_children($parent) {

if($parent == 'null'){
$query = "SELECT `id` FROM `task` WHERE ISNULL(`parent`);";}else{

$query = "SELECT `id` FROM `task` WHERE `parent`= '$parent';";}
$result = mysql_query($query);
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
if ( !is_array($myarray) ) $myarray = array();
$myarray[] = array_push($myarray,$row['id']);
display_children($row['id']);
}
return $myarray;
echo "
";
}

Re: noob push/merge problem

am 03.12.2006 18:45:09 von Paul Lautman

strawberry wrote:
> strawberry wrote:
>> I know this is probably completely straightforward but how do can
>> adapt this function so as to merge the results of the recursive
>> queries into a single, flat array?
>>
>> function display_children($parent) {
>> if($parent == 'null'){
>> $query = "SELECT `id` FROM `task` WHERE ISNULL(`parent`);";}else{
>>
>> $query = "SELECT `id` FROM `task` WHERE `parent`= '$parent';";}
>> $result = mysql_query($query);
>> while ($row = mysql_fetch_array($result)) {
>> display_children($row['id']);
>> }
>> }
>>
>> So if the results of the 1st query was 1,2,3
>> and the results of the first recursion was 11,21,31
>> and the results of second recursion was 118,213,315
>>
>> I'd end up with array(1,2,3,11,21,31,118,213,315)
>>
>> As always, any help appreciated.
>
> here's my stab at it. it doesn't work - but i'm not sure why not:
>
> function display_children($parent) {
>
> if($parent == 'null'){
> $query = "SELECT `id` FROM `task` WHERE ISNULL(`parent`);";}else{
>
> $query = "SELECT `id` FROM `task` WHERE `parent`= '$parent';";}
> $result = mysql_query($query);
> while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
> if ( !is_array($myarray) ) $myarray = array();
> $myarray[] = array_push($myarray,$row['id']);
> display_children($row['id']);
> }
> return $myarray;
> echo "
";
> }

It doesn't work because you're not doing anything with the return value of
display_children() and $myarray is a local array, local to each recursion of
display_children().
You want something along the lines of:

$myarray = array_merge($myarray,display_children($row['id']));

although I haven't examined the logic fully.

You should be