Transform array into single matrix

Asked

Viewed 28 times

1

Good morning,

I am having the following difficulty, currently my array returns as follows:

array(2) {
  [0]=>
  array(1) {
    ["name"]=>
    string(8) "Vermelho"
  }
  [1]=>
  array(1) {
    ["name"]=>
    string(7) "Azul"
  }
}

The above example is bad to work because one array is apparently inside another. I would like the return to be something like:

array(7) {
  [0]=>
  string(8) "Vermelho"
  [1]=>
  string(7) "Morango"
  [2]=>
  string(5) "Maça"
  [3]=>
  string(5) "Amora"
  [4]=>
  string(7) "Amarelo"
  [5]=>
  string(6) "Banana"
  [6]=>
  string(7) "Damasco"
}

How to work with an array so that it stays the way above and not 1 array inside the other?

  • How is it that you are mounting the first array?

1 answer

1


If you can ensure that all items within the array always have another arraywith the countryside name, just use the array_map.

$lista = array_map(function ($item) {
       return $item['nome'];
}, $array);

The function array_map returns a array formatted according to what is returned in your callback. In the above case, we use the callback to return only the present value in 'name'

  • Didn’t work here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.