0

I have the following multidimensional array:

Array
(
    [0] => Array
        (
            [area] => 5
            [estante] => 5
            [anaquel] => 5
            [no_caja] => 5
            [id_tipo] => 3
            [nombre_tipo] => Administrativo
        )
[1] => Array
    (
        [area] => 5
        [estante] => 5
        [anaquel] => 5
        [no_caja] => 5
        [id_tipo] => 1
        [nombre_tipo] => Copiador
    )

[2] => Array
    (
        [area] => 5
        [estante] => 5
        [anaquel] => 5
        [no_caja] => 5
        [id_tipo] => 2
        [nombre_tipo] => Judicial
     )

)

and I want to reduce it by having all the different values (intersection) between them. The dimension of the array may change (I'm retrieving the info from a database). I have thought in using functions like array_reduce and array_intersect, but I have the problem that they work only with one-dimension arrays and I can't find the way to pass an indefinite (not previous known) number of parameters to these function. I'd like to have an output like this:

Array([0]=>Copiador, [1]=>Administrativo, [2]=>Judicial).

How can I do this?

Thanks in advance.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
emco
  • 4,589
  • 3
  • 18
  • 20
  • it is not clear what your question is - what exactly are you trying to do? Why does "Array([0]=>Copiador, [1]=>Administrativo, [2]=>Judicial)" not work? – Fraser Mar 16 '11 at 17:14
  • is there any specific calculation to get output? – Gaurav Mar 16 '11 at 17:16
  • The problem that I'm having is that the array is not one-dimension array. The question would be, how can I reduce it to have the wanted output? – emco Mar 16 '11 at 17:32

4 Answers4

1

This task is exactly what array_column() is for -- extracting columnar data.

Call this:

var_export(array_column($your_array, 'nombre_tipo'));

This will output your desired three-element array. ...I don't understand the sorting in your desired output.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0
$reduced = array();
foreach ($oldarray as $value) {
    $reduced[] = $value['nombre_tipo'];
}

Although, a better solution may be to just modify your SQL query so you get the correct data to begin with.

Note: you can also do it with array_reduce, but I personally prefer the method above.

$reduced = array_reduce($oldarray,
                        function($a, $b) { $a[] = $b['nombre_tipo']; return $a; },
                        array()
                       );
mfonda
  • 7,873
  • 1
  • 26
  • 30
  • Thanks for your answer, but, if I'm not wrong, you're treating $oldarray as one-dimension array, in my case I have a multidimensional array. – emco Mar 16 '11 at 17:40
  • @user521631 I am treating it as the array you posted in your question :) – mfonda Mar 16 '11 at 17:41
0
$arr=array(
  array (
    'area' => 5 ,
    'estante' => 5 ,
    'anaquel' => 5,
    'no_caja' => 5,
    'id_tipo' => 3,
    'nombre_tipo' => 'Administrativo'),
  array (//etc.
  )
); 

$fn=function(array $a, $k){
    if(array_key_exists($k,$a)) return $a[$k];
};

$b=array_map($fn,$arr,array_fill(0,count($arr),'nombre_tipo'));
print_r($b);

/*
Array
(
    [0] => Administrativo
    [1] => Copiador
    [2] => Judicial
)
*/
dnagirl
  • 20,196
  • 13
  • 80
  • 123
0

Seems like you want array_map


$newArr = array_map(function($a) {
    return $a['nombre_tipo'];
}, $oldArr);

var_dump($newArr);
Avedis Kiyici
  • 67
  • 1
  • 7