Jump to content

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

Edited by cyberRobot
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.