0

I'd like to sort 4 arrays that are enclosed within 1 array, but I have no clue how to do it.

    $poster = [];
    $character = [];
    $title = [];
    $date = [];

    foreach($person->getMovieCredits()->getCast() as $movie)
    {
        array_push($poster, $movie->getPosterPath());
        array_push($character, $movie->getCharacter());
        array_push($title, $movie->getTitle());
        array_push($date, $movie->getReleaseDate()->format('Y'));
    }

    $personArray = [
        'date' => $date,
        'title' => $title,
        'character' => $character,
        'poster' => $poster
    ];

The thing is that I would like to sort them by date, but not losing data integrity with other arrays.

How should I do that?

Cookie
  • 13
  • 1
  • 5

4 Answers4

0

Try this

<?php
$date = ['2014-04-03', '2013-04-03', '2015-04-03', '2017-04-03', '2013-09-03'];
$title = ['title1', 'title2', 'title4', 'title5', 'title6'];
$character = ['character2', 'character5', 'character1', 'character8', 'character0'];
$poster = ['poster4', 'poster1', 'poster7', 'poster9', 'poster0'];

$personArray = [
    'date' => $date,
    'title' => $title,
    'character' => $character,
    'poster' => $poster
];

/*echo "<pre>";
print_r( $personArray);*/

$length_count = count($date);

$new_array = array();
for($i=0; $i<$length_count ; $i++){
$new_array[$i] = array();
    foreach($personArray as $key => $value){
        $new_array[$i][$key] = $personArray[$key][$i];
    }
}

function date_compare($a, $b){
    $t1 = strtotime($a['date']);
    $t2 = strtotime($b['date']);
    return $t1 - $t2;
}    
usort($new_array, 'date_compare');

//print_r( $new_array);
$sorted_array = array();
$sorted_array['date'] = array();
$sorted_array['title'] = array();
$sorted_array['character'] = array();
$sorted_array['poster'] = array();
foreach($new_array as $data){
     array_push($sorted_array['date'], $data['date']);
     array_push($sorted_array['title'], $data['title']);
     array_push($sorted_array['character'], $data['character']);
     array_push($sorted_array['poster'], $data['poster']);
}
//print_r( $sorted_array);

?>
Meera Tank
  • 699
  • 5
  • 13
0

You can also play around with the Laravel helpers array_sort() and array_sort_recursive(). Inside the Closure you put a check if the element in the parent array also is an array and also sort the inner array.

https://laravel.com/docs/5.4/helpers

online Thomas
  • 8,864
  • 6
  • 44
  • 85
0

Thanks guys for help, I managed to do that quite simply:

$movieArray = [];
    foreach($person->getMovieCredits()->getCast() as $movie)
    {
        array_push($movieArray, ['date' => $movie->getReleaseDate()->format('Y'), 'poster' => $movie->getPosterPath(),
            'character' => $movie->getCharacter(), 'title' => $movie->getTitle()]);
    }

    asort($movieArray);
Cookie
  • 13
  • 1
  • 5
0

For example, this multidimensional array:

$array = [];
$array[] = [
  'name' => 'Dupont', 'age' => 72,
  'name' => 'Albert', 'age' => 11,
  'name' => 'Durand', 'age' => 56,
  'name' => 'Michel', 'age' => 52
];

For sort by age, for example:

$columns = array_column($array, 'age');
array_multisort($columns, SORT_ASC, $array);

dump($array);

If need sort by 2 columns:

$columns_1 = array_column($array, 'name');
$columns_2 = array_column($array, 'age');
array_multisort($columns_1, SORT_ASC, $columns_2, SORT_DESC, $array);

dump($array);
Andrey
  • 345
  • 2
  • 7