Jump to content

Write value from function into array


thugsr

Recommended Posts

Hi,

I have problem with recursive function. I want to store value form that function into array because i need to reverse result of function. 

Here is function 

function display_children($category_id, $level) 
{
  global $database;
    $result = mysql_query("SELECT * FROM parents WHERE id_roditelja='$category_id'") or die(mysql_error());
    
   $broj = $database->num_rows($result);
    $niz = array();
while ($row = mysql_fetch_array($result)) 
    {
        echo str_repeat('  ',$level) . $row['naziv'] . "-> ";
      
      $niz =  display_children($row['parent_id'], $level+1); 
        }
    
}

I just want to save  "display_children($row['parent_id'], $level+1)" this into an array reverse it and then return it. Can someone help me with this? 

Link to comment
https://forums.phpfreaks.com/topic/280667-write-value-from-function-into-array/
Share on other sites

In my first post is code of function, on that i changed line  

$niz[] =  display_children($row['parent_id'], $level+1);

 like this,and in other variation i try with for loop where counter $i was key in array, that isn't worked as well...i need this to be stored into array 

display_children($row['parent_id'], $level+1);

That ($niz[]) should give you an array. To see for yourself, you could temporarily add the following line of code after the while loop:

<?php
echo '<pre>' . print_r($niz, true) . '</pre>';
?>

From there you should look into array_reverse():

http://php.net/manual/en/function.array-reverse.php

 

...and the return statement:

http://php.net/manual/en/function.return.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.