Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

262
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda