valores valor puede para multidimensional manejo index in_array emplear elemento ejemplos comparar buscar array_search array php multidimensional-array

valor - in_array multidimensional php



ComparaciĆ³n de elementos en una matriz multidimensional (3)

¿Qué tal ordenarlos por tipo?

$animalsByType = array(); foreach ($array as $animal) { $type = $animal[''animal_type'']; if (!isset($animalsByType[$type])) { $animalsByType[$type] = array(); } $animalsByType[$type][] = $animal; }

No estoy seguro de qué tipo de comparación quieres hacer, pero esto al menos te permite sublistas individuales por tipo.

Digamos que tengo una matriz multidimensional. Por ejemplo:

Array ( [0] => Array ( [animal_id] => 5494 [animal_name] => "Suzy" [animal_type] => "zebra" [animal_location] => 0 [animal_awake] => 1 [animal_age] => 3 ) [1] => Array ( [animal_id] => 5494 [animal_name] => "Joshua" [animal_type] => "panda" [animal_location] => 5 [animal_awake] => 0 [animal_age] => 8 ) [2] => Array ( [animal_id] => 5494 [animal_name] => "Debra" [animal_type] => "snake" [animal_location] => 7 [animal_awake] => 1 [animal_age] => 3 ) [3] => Array ( [animal_id] => 5495 [animal_name] => "Caleb" [animal_type] => "zebra" [animal_location] => 0 [animal_awake] => 1 [animal_age] => 3 ) [4] => Array ( [animal_id] => 5495 [animal_name] => "Joshua" [animal_type] => "panda" [animal_location] => 5 [animal_awake] => 0 [animal_age] => 8 ) [5] => Array ( [animal_id] => 5495 [animal_name] => "Debra" [animal_type] => "snake" [animal_location] => 7 [animal_awake] => 1 [animal_age] => 3 ) [6] => Array ( [animal_id] => 5496 [animal_name] => "Emily" [animal_type] => "zebra" [animal_location] => 0 [animal_awake] => 1 [animal_age] => 3 ) [7] => Array ( [animal_id] => 5496 [animal_name] => "Joshua" [animal_type] => "panda" [animal_location] => 5 [animal_awake] => 0 [animal_age] => 8 ) [8] => Array ( [animal_id] => 5496 [animal_name] => "Debra" [animal_type] => "snake" [animal_location] => 7 [animal_awake] => 1 [animal_age] => 3 ) )

Y quiero comparar todas las serpientes contra las serpientes, y todos los pandas contra el panda, etc. (pero no una serpiente contra un panda) y poner los elementos únicos en una matriz (todos los elementos únicos en una única matriz), ¿cómo lo haría? hacer esto. Como son elementos dentro de una matriz, estoy un poco perplejo. Además, no sabré de antemano cuántos tipos diferentes habrá. Por ejemplo, una vez podría pasar una matriz multidimensional con Panda, Bear, Snake. La próxima vez podría pasar un array con Bird, Cat, Panda, Zebra.

¿Algunas ideas?

SALIDA FINAL

Array ( [0] => Array ( [animal_id] => 5494 [animal_name] => "Suzy" [animal_type] => "zebra" [animal_location] => 0 [animal_awake] => 1 [animal_age] => 3 ) [1] => Array ( [animal_id] => 5494 [animal_name] => "Joshua" [animal_type] => "panda" [animal_location] => 5 [animal_awake] => 0 [animal_age] => 8 ) [2] => Array ( [animal_id] => 5495 [animal_name] => "Caleb" [animal_type] => "zebra" [animal_location] => 0 [animal_awake] => 1 [animal_age] => 3 ) [3] => Array ( [animal_id] => 5495 [animal_name] => "Debra" [animal_type] => "snake" [animal_location] => 7 [animal_awake] => 1 [animal_age] => 3 ) [4] => Array ( [animal_id] => 5496 [animal_name] => "Emily" [animal_type] => "zebra" [animal_location] => 0 [animal_awake] => 1 [animal_age] => 3 ) )


Las claves de matriz deben ser únicas, así que vamos a usar eso para nuestra ventaja.

function get_animal_key($animal) { return $animal[''animal_type''] . ''-'' . $animal[''animal_name'']; } $uniques = array(); foreach ($array as $animal) { $key = get_animal_key($animal); $uniques[$key] = $animal; } var_export($uniques);

Da la siguiente matriz

array ( ''zebra-Suzy'' => array ( ''animal_id'' => 5494, ''animal_name'' => ''Suzy'', ''animal_type'' => ''zebra'', ''animal_location'' => 0, ''animal_awake'' => 1, ''animal_age'' => 3, ), ''panda-Joshua'' => array ( ''animal_id'' => 5496, ''animal_name'' => ''Joshua'', ''animal_type'' => ''panda'', ''animal_location'' => 5, ''animal_awake'' => 0, ''animal_age'' => 8, ), ''snake-Debra'' => array ( ''animal_id'' => 5496, ''animal_name'' => ''Debra'', ''animal_type'' => ''snake'', ''animal_location'' => 7, ''animal_awake'' => 1, ''animal_age'' => 3, ), ''zebra-Caleb'' => array ( ''animal_id'' => 5495, ''animal_name'' => ''Caleb'', ''animal_type'' => ''zebra'', ''animal_location'' => 0, ''animal_awake'' => 1, ''animal_age'' => 3, ), ''zebra-Emily'' => array ( ''animal_id'' => 5496, ''animal_name'' => ''Emily'', ''animal_type'' => ''zebra'', ''animal_location'' => 0, ''animal_awake'' => 1, ''animal_age'' => 3, ), )

Como puede ver, esto toma el tipo y el nombre del animal como identificadores únicos. Su pregunta no indicó qué hace que un animal sea único, por lo tanto, modifique lo anterior para adaptarlo a sus necesidades.


Puede ejecutar la matriz y luego verificar cada elemento:

foreach($aAnimals AS $iKey => $aAnimalData { foreach($aAnimals AS $iSubKey => $aData) { if($aAnimalData[''animal_type''] == $aData[''animal_type''] && $iKey != $iSubKey) { // Start doing whatever you want to do when the types match. // The last part makes sure the second foreach does not // match with the first one. } } }

Si especifica qué tipo de comparación quiere, podría mejorar esta respuesta con eso.