1

How to sort the following array?

I have an multidimensional array that gets filled with total hours for each brand

$totalHours = array(
                'brand'   => array(),
                'project' => array(),
                'hours'   => array()
            );

The output is something like this (project is not filled):

array(3) {
  ["brand"]=>
  array(3) {
    [0]=>
    string(4) "Nike"
    [1]=>
    string(9) "Coke Cola"
    [2]=>
    string(8) "Converse"
  }
  ["project"]=>
  array(3) {
    [0]=>
    string(6) "Bonobo"
    [1]=>
    string(4) "LDRU"
    [2]=>
    string(2) "US"
  }
  ["hours"]=>
  array(3) {
    [0]=>
    int(28)
    [1]=>
    int(106)
    [2]=>
    string(1) "2"
  }
}

Now I would like to sort the array based on the "hours" field.

I've tried the array_multisort but it seems like I simply don't get how this function works and how to apply it to this array. I have been able to sort an single array with just one row of values. If I apply that to this problem im sorting only the hours field, leaving the others unsorted and therefore the array is corrupted.

The "project" does actually contains a string. It always does. In this example I didn't filled it.

3 Answers 3

1

That data format is not very convenient because there is no direct association between brands and hours, they even sit in different arrays! Plus, the last hour is a string, not an integer.

We're going to have to create an intermediate array to associate them and sort them. We'll then re-inject everything in the original array.

// Make sure both arrays of brands and hours and the same size
if (count($totalHours['brand']) != count($totalHours['hours'])) {
    throw new Exception('Invalid data!');
}

// Make sure every hour record is an integer, not a string
$totalHours['hours'] = array_map('intval', $totalHours['hours']);

// array_combine will sort all arrays based on the sorting of the first one
array_multisort($totalHours['hours'], $totalHours['brand'], $totalHours['project']);

EDIT: Using array_multisort was @delprks 's idea originally. Here I applied it to both brand and project arrays.

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

4 Comments

Thank you so much. Could you help me on how to apply this including the 'project' array? or perhaps how i had to assign the values in the array the first place
My solution is not the best as I didn't use array_multisort. Now I read other answers, I think @delprks 's might be better for that case. I'll try to come up with something.
I've edited my answer to use array_multisort on brand and project arrays.
Excellent! Thank you!
1

array_multisort should work:

$totalHours = array(
  'brand' => array('Nike', 'Coke Cola', 'Converse'),
  'project' => array(),
  'hours' => array(28, 106, '2')
);

array_multisort($totalHours['hours'], $totalHours['brand']);

var_dump($totalHours);

Comments

0

You cant do that with a single array_* function!

if (project is not filled) always and the indexes of brand and hours are equal, then you can do:

$forsort = array_combine($totalHours["brands"],$totalHours["hours"]);
asort($forsort,SORT_NUMERIC);
$totalHours["brands"]=array_keys($forsort);
$totalHours["hours"]=array_values($forsort);

But, this is just an answer for you question, not best practise at all.

1 Comment

can i just add another $totalHours["projects"]=array_values($forsort); to add the project field?

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.