1

I am trying to sort this array but at the same time, I am trying to change the index and all elements unchanged.

I want to sort the array by [name]. So array[7] with Cross Cutting Functions would be the first node and so on ...

This is the array:

Array
(
    [0] => stdClass Object
        (
            [tid] => 992
            [vid] => 70
            [name] => Global People
            [description] => 
            [format] => filtered_html
            [weight] => 0
            [depth] => 0
            [parents] => Array
                (
                    [0] => 0
                )

        )

    [1] => stdClass Object
        (
            [tid] => 1206
            [vid] => 70
            [name] => Global Department Head
            [description] => 
            [format] => filtered_html
            [weight] => 1
            [depth] => 0
            [parents] => Array
                (
                    [0] => 0
                )

        )

    [2] => stdClass Object
        (
            [tid] => 986
            [vid] => 70
            [name] => Global Private Leaders
            [description] => 
            [format] => filtered_html
            [weight] => 2
            [depth] => 0
            [parents] => Array
                (
                    [0] => 0
                )

        )

    [3] => stdClass Object
        (
            [tid] => 1208
            [vid] => 70
            [name] => Service Line Partnership
            [description] => 
            [format] => filtered_html
            [weight] => 3
            [depth] => 0
            [parents] => Array
                (
                    [0] => 0
                )

        )

    [4] => stdClass Object
        (
            [tid] => 984
            [vid] => 70
            [name] => Digital Stakeholders
            [description] => 
            [format] => filtered_html
            [weight] => 4
            [depth] => 0
            [parents] => Array
                (
                    [0] => 0
                )

        )

    [5] => stdClass Object
        (
            [tid] => 990
            [vid] => 70
            [name] => Regional Team
            [description] => 
            [format] => filtered_html
            [weight] => 5
            [depth] => 0
            [parents] => Array
                (
                    [0] => 0
                )

        )

    [6] => stdClass Object
        (
            [tid] => 988
            [vid] => 70
            [name] => Digital Team
            [description] => 
            [format] => filtered_html
            [weight] => 6
            [depth] => 0
            [parents] => Array
                (
                    [0] => 0
                )

        )

    [7] => stdClass Object
        (
            [tid] => 1210
            [vid] => 70
            [name] => Cross Cutting Functions
            [description] => 
            [format] => filtered_html
            [weight] => 7
            [depth] => 0
            [parents] => Array
                (
                    [0] => 0
                )

        )

)

This is what I tried:

(But it's not working)

 foreach ($terms as $key => $row) {
      $temp_array[$key] = $row['name'];
 }

array_multisort($temp_array, SORT_ASC, $data);

Thanks.

Steve
  • 2,546
  • 8
  • 49
  • 94

1 Answers1

1

You should be able to use PHP usort to solve this:

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

// Example with your $terms
function cmp($a, $b)
{
    return strcmp($a->name, $b->name);
}

usort($terms, "cmp");
giolliano sulit
  • 996
  • 1
  • 6
  • 11
  • `usort()` assigns new keys. – Barmar Nov 02 '17 at 01:21
  • _I am trying to change the index and all elements unchanged_ - He wants the index changes but the values the same ? I don't see the issue – giolliano sulit Nov 02 '17 at 01:24
  • he wants the order of the array to change, but the indexes to stay the same. So index `7` will be the first element of the array. – Barmar Nov 02 '17 at 01:34
  • Well it looks like we're understanding the question differently. I don't see what changing the order of the elements inside the `$terms object` will accomplish. But I can see how changing the order of `$terms` would be useful. Need @Steve to clarify I guess – giolliano sulit Nov 02 '17 at 01:36
  • Changing the order affects things like `foreach`. – Barmar Nov 02 '17 at 01:38
  • But it's a standard object.. he can still access the variables using `$object->name / $object->parents`. We will just wait for clarification – giolliano sulit Nov 02 '17 at 01:38
  • I mean `foreach ($terms as $i => $term)`. This should return `$i = 7` first, the `$i = 4`, and so on. – Barmar Nov 02 '17 at 01:40
  • It's perfect man! – Steve Nov 02 '17 at 02:37