Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

257
Visualizações
How to sort two OR MORE arrays by the order of first one?

Suppose I have several arrays.

$a = array("E", "A", NULL, "D", "C");
$b = array("Dog","Cat","Horse","Bear","Zebra");
$c = array(12, 11, 20, 30, 19);

First array not necessary numeric and can contain nulls.

I want to sort all 3 array by the order of first, i.e. to get

"A", "C", "D", "E", NULL

"Cat", "Zebra", "Bear", "Dog", "Horse"

11, 19, 30, 12, 20

I.e. tuples ("E", "Dog", 12), ("A", "Cat", 11), (NULL, "Horse", 20), ("D", "Bear", 30), ("C", "Zebra", 19) should be conserved.

I don't care what happens with null cases: thay can stay in place or go to end or beginning.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can used array_multisort for sorting multiple array based on first array. See below example

$a = array("E", "A", NULL, "D", "C");
$b = array("Dog","Cat","Horse","Bear","Zebra");
$c = array(12, 11, 20, 30, 19);

array_multisort($a, SORT_ASC, SORT_STRING, $b, $c); // $b and $c sorting based on $a
echo "<pre>";
print_r($b);
print_r($c);
over 4 years ago · Santiago Trujillo Relatório

0

Hope my post will help you out.. Here we are using two function array_combine and ksort. array_combine will combine values with the keys and ksort will sort an array by key

Try this code snippet here

<?php
ini_set('display_errors', 1);
$a = array(5, 1, 2, 4, 3);
$b = array("Dog","Cat","Horse","Bear","Zebra");
$combined=array_combine($a, $b);
ksort($combined);
print_r($combined);

Output:

Array
(
    [1] => Cat
    [2] => Horse
    [3] => Zebra
    [4] => Bear
    [5] => Dog
)
over 4 years ago · Santiago Trujillo Relatório

0

you can make array like

$array = array(
          '1'=>"Cat",
          '2' => 'Horse',
          '3' => 'Zebra',
          '4' => 'Bear',
          '5' => 'Dog',
      );

This is achieved using array_combine($a, $b) After that, you can use ksort on the combined array.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda