Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to unwrap an array in php

Tags:

arrays

php

unwarp

i have this array here:

Array
(
 [0] => Array
     (
         [presentation] => Präsentationen
     )

 [1] => Array
     (
         [news] => Aktuelle Meldungen
         [devplan] => Förderprogramme
         [salesdoc] => Vertriebsunterlagen
     )

 [2] => Array
     (
         [user/settings] => Mein Account
     )

 [3] => Array
     (
     )

 [4] => Array
     (
         [orders] => Projekte
     )

)

i want to unwrap the first depth of the array to get this:

 Array
 (
  [presentation] => Präsentationen
  [news] => Aktuelle Meldungen
  [devplan] => Förderprogramme
  [salesdoc] => Vertriebsunterlagen
  [user/settings] => Mein Account
  [orders] => Projekte
 )
like image 849
antpaw Avatar asked Jan 27 '10 12:01

antpaw


People also ask

How can I remove a specific item from an array PHP?

In order to remove an element from an array, we can use unset() function which removes the element from an array and then use array_values() function which indexes the array numerically automatically. Function Used: unset(): This function unsets a given variable.

What is the use of extract () function?

The extract() function imports variables into the local symbol table from an array. This function uses array keys as variable names and values as variable values. For each element it will create a variable in the current symbol table. This function returns the number of variables extracted on success.

How do you delete one level of an array?

The array_shift() function removes the first element from an array, and returns the value of the removed element.

What is array reduce in PHP?

PHP | array_reduce() Function This inbuilt function of PHP is used to reduce the elements of an array into a single value that can be of float, integer or string value. The function uses a user-defined callback function to reduce the input array.


2 Answers

With PHP 5.3.0+:

array_reduce($array, 'array_merge', array());
like image 120
Ignacio Vazquez-Abrams Avatar answered Sep 23 '22 00:09

Ignacio Vazquez-Abrams


I guess the simplest way is to use a foreach loop:

 $resultArray = array();

  foreach ($myArray as $array)
   foreach ($array as $key => $element)
    $resultArray[$key] = $element;
like image 30
Pekka Avatar answered Sep 21 '22 00:09

Pekka