2

I have a multidimensional array that looks like this:

enter image description here

array:3 [
  0 => array:2 [
    "titre" => "Un package test"
    "nbDDL" => "3"
  ]
  1 => array:2 [
    "titre" => "retest"
    "nbDDL" => "1"
  ]
  2 => array:2 [
    "titre" => "ytjrtj"
    "nbDDL" => "1"
  ]
]

I would like to sort it by ASC or DESC order (depending on a variable passed in function parameter) with the nbDDL.

I looked at the method array_multisort() but I can not put it in place.

I work under Symfony 3. Currently, I have:

if($ordreTri == "ASC")
{
    $liste = array_multisort("nbDDL", ASC);
}

Thanks for your help !

1
  • 4
    Post code as text instead of text in images so people can copy paste easily Commented Apr 25, 2019 at 10:27

4 Answers 4

2

There's a few different ways of doing this - you can introduce your own custom sort by using usort() and the spaceship operator <=>, but you can use array_multisort(), you just have to combine it with array_column().

You can sort the array by first fetching all the nbDDLs. Then use that as the sorting-array in array_multisort(), and sort by ascending order (SORT_ASC). Apply that to $array, and you're done!

// By reference, $array is changed
array_multisort(array_column($array, "nbDDL"), SORT_ASC, $array);

This is done by reference, so you don't need to assign it to a variable. The return-value of array_multisort() is a boolean, which means that if you assign it as

// By reference - $result is bool
$result = array_multisort(array_column($array, "nbDDL"), SORT_ASC, $array);

Then $result is either true or false, but not the sorted array.

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

1 Comment

Wow, great, thanks for this help, and the explanations that go with it! Thank you so much !
1

You can try like below:

$data = [
    ['a' => 'a', 'order' => 1],
    ['b' => 'b', 'order' => 3],
    ['c' => 'c', 'order' => 1]
];

array_multisort(array_column($data, 'order'), SORT_DESC, SORT_NUMERIC, $data);

print_r($data);

Comments

1

You can do as following way:

function array_sort($array, $on, $order=SORT_ASC){

    $new_array = array();
    $sortable_array = array();

    if (count($array) > 0) {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                foreach ($v as $k2 => $v2) {
                    if ($k2 == $on) {
                        $sortable_array[$k] = $v2;
                    }
                }
            } else {
                $sortable_array[$k] = $v;
            }
        }

        switch ($order) {
            case SORT_ASC:
                asort($sortable_array);
                break;
            case SORT_DESC:
                arsort($sortable_array);
                break;
        }

        foreach ($sortable_array as $k => $v) {
            $new_array[$k] = $array[$k];
        }
    }

    return $new_array;
}

Use function as like:

$list = array(
   array( 'type' => 'suite', 'name'=>'A-Name'),
   array( 'type' => 'suite', 'name'=>'C-Name'),
   array( 'type' => 'suite', 'name'=>'B-Name')
 );

$list = array_sort($list, 'name', SORT_ASC);

Comments

0

normally, you can use array_multisort to sort the array, but somehow it's weird thatin php7.4 it returns boolean (true or false) instead of sorted array like in php 5. I mean, a sorted array is what we actually need, not the boolean values.

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

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.