0

I have a MySQL query that gets the result of the entire MySQL database into a php array called $parents

then I have a foreach function loop that display result from that array

the function:

 function subtree($id, $parents) { 
 if (isset($parents[$id]))
 foreach ($parents[$id] as $child) 
 {
 subtree($child, $parents);
 }
 } 

I need to sort the array result of this function, or to get the result into a new_sub_array then sort it and display result

any way to modify the function to do what i need?

5
  • Sort in the database query.. Commented Dec 30, 2013 at 21:51
  • i need to sort from array not the query Commented Dec 30, 2013 at 21:55
  • yes there are many ways. Commented Dec 30, 2013 at 21:56
  • @chx, so where is your idea in how to do the many ways? Commented Dec 30, 2013 at 22:13
  • possible duplicate of Order multidimensional array recursively at each level in PHP Commented Dec 30, 2013 at 22:46

2 Answers 2

0

Change your condition

if (is_array($parents[$id]))

If Value is array then look for next sub tree

else

otherwise write value.

If you want to sort an array - just use one of this function at the beginning of subtree()

Good idea is to don't use recursion, instead of this you can use iteration and well prepared sql query instead of sorting in PHP ;)

Your function could look like this, I am not sure if I understand you, but it should be ok.

function subtree($parents) 
{
    if (is_array($parents))
    {
        sort($parents);
        foreach ($parents as $key => $value) 
        {
            subtree($value);
        }
    }
    else
    {
        echo $parents;
    }
} 

The $id argument is unnecessary.

Sign up to request clarification or add additional context in comments.

3 Comments

can you modify the function ? been trying that with no luck
the $id is the parent id that i need to get the child of. the array is like parent-LeftChild1,RightChild1 Parent(LeftChild1)-Leftchild2,RightChild2 ,, then the function should print each child of the main parents and childs of childs and so on, i just need to sort the result of the function
So if you want get child of specific parent id, then put to function as argument something like this $parent[$specific_id]. Did your array looks like this pastebin.com/JXdDRL5A ?
-1

You'd sort the values when you call them from the database. So if you sorted them in descending order of date, for example:

SELECT * FROM values WHERE :id=$id ORDER BY date DESC

Basically, ORDER BY is your friend. This helped me a lot.

3 Comments

need to sort from the function and not the query
Then this link is definitely what you want to be using. Loads of functions for sorting arrays.
the problem is that i can't get to find a way to sort it inside the function., the result is always unsorted

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.