array of arrays count_values php

1

How can I apply the array_count_values () function to an array of arrays.

Example:

Array ( [1] => Array ( [91] => 123 [94] => 123 ) [2] => Array ( [91] => 10 [94] => 13 ) [3] => Array ( [91] => 02 [94] => 1 )

I want to obtain that for [1], 123 is repeated twice.

    
asked by DanielGB 09.11.2017 в 09:30
source

1 answer

0

You would iterate through each array element with, for example, a foreach :

<?php
/* Matriz de ejemplo tomado de la pregunta */
$matriz = [
  1 => [
    91 => 123,
    94 => 123,
  ],
  2 => [
    91 => 10,
    94 => 13,
  ],
  3 => [
    91 => 02,
    94 => 1,
  ]
];
/* Creamos una matriz vacía para almacenar el resultado */ 
$resultado = [];
/* Iteramos por cada elemento de la matriz inicial */
foreach ($matriz as $clave => $valor) {
  /* Almacenamos en la matriz de salida el resultado de contar elementos */
  $resultado[$clave] = array_count_values($valor);
}
/* Mostramos el resultado */
var_export($resultado);

The result of the execution will be:

array (
  1 => 
  array (
    123 => 2,
  ),
  2 => 
  array (
    10 => 1,
    13 => 1,
  ),
  3 => 
  array (
    2 => 1,
    1 => 1,
  ),
)
    
answered by 09.11.2017 / 09:39
source