how to unwrap an array in php

php array reduce with key
php unpack array
array_reduce vs foreach php
php get array value by key
php extract
php extract specific keys from array
php extract values from associative array
php turn associative array into variables

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
 )

With PHP 5.3.0+:

array_reduce($array, 'array_merge', array());

extract - Manual, array_reduce() applies iteratively the callback function to the elements of the array , so as to reduce the array to a single value. Parameters ¶. array. The input array  Get The Length of an Array - The count() Function. The count() function is used to return the length (the number of elements) of an array:

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;

array_reduce - Manual, If you are using plain variables, you don't have to worry about this. But if you are using arrays with indices you usually expect the order of the indices in the array  Is there a way to unwrap an element other than jQuery? I’d like to unwrap the div with the ID fromWhere in the code below. I tried doing that with jQuery but it didn’t work. <?php ob_start

Try

array_merge($array[0], $array[1], $array[2], $array[3], $array[4]);

or

$new = $array[0] + $array[1] + $array[2] + $array[3] + $array[4];

list - Manual, Laravel has a wrap method and `array_wrap()` helper to normalize values into an array. Raul (@rcubitto) shared this nice tip about about  PHP guzzlehttp\promise unwrap - 30 examples found.These are the top rated real world PHP examples of guzzlehttp\promise\unwrap extracted from open source projects. You can rate examples to help us improve the quality of examples.

This is also a beautifull one liner

$array = new RecursiveArrayIterator($yourArray);

Unwrapping array_wrap(), Wrapping and unwrapping functions with closures in php. wrap_unwrap.php. <?​php. class Memo{. static $fns = array();. } class Call{. var $fn;. var $args = array();. The array() function is used to create an array. In PHP, there are three types of arrays: Indexed arrays - Arrays with numeric index; Associative arrays - Arrays with named keys; Multidimensional arrays - Arrays containing one or more arrays

With PHP 5.6.0+:

$new = array_merge(...$array);

(A more general version of Gordons approach.)

Wrapping and unwrapping functions with closures in php · GitHub, $result = array_merge($result, array($key => $value)); And for those who want a copy-pasta of the working function: function array_flatten($array = null) { $result​  If you want a sequence of int, then use a vector<int>. Using the key_char string, the values of the chars in it will serve as the initial value of the ints. std::vector<int> key_num(key_char.begin(), key_char.end()); Then, iterate over each character of key_num and convert it to the equivalent int value for

PHP array_flatten() function. Convert a multi-dimensional array into , php ob_start(); ?> <div id="fromWhere" ></div> <?php $contents = ob_get_contents(div); $params = array( 'origin'  The best way to merge two or more arrays in PHP is to use the array_merge () function. Items of arrays will be merged together, and values with the same string keys will be overwritten with the last value: To remove array values from another array (or arrays), use array_diff () .

unWrap an element with php - PHP, Basically, collections are PHP arrays but it's an Object Oriented approach to deal with PHP arrays. see full series. An array can be created using the array () language construct. It takes any number of comma-separated key => value pairs as arguments. array ( key => value , key2 => value2 , key3 => value3 , ) The comma after the last array element is optional and can be omitted.

Laravel Collections – wrap() and unwrap() Methods, PHP Array Reference. Example. Send each value of an array to a function, multiply each value by itself, and return an array with the new values: <?php function  Multidimensional array − An array containing one or more arrays and values are accessed using multiple indices. NOTE − Built-in array functions is given in function reference PHP Array Functions. Numeric Array. These arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index

Comments
  • nothing, i know some ways to do this, but the are all ugly :)
  • Beautiful one-liner, but I still have to go with Gordon's approach, it seems simpler to me.
  • I like this one better than mine, because it works for an arbitrary amount of arrays on the first level
  • wow this looks cool, but it doesnt work for me :( array_merge() [function.array-merge]: Argument #1 is not an array, but im 100% sure its the same array form the example.
  • Works perfectly here, but I have a different structure: Before: array(2) { [0]=> array(1) { ["a"]=> int(1) } [1]=> array(1) { ["b"]=> int(2) }} After: array(2) { ["a"]=> int(1) ["b"]=> int(2)}
  • I also wonder why this works, as I tried it myself in another context without success. In comments on the array_reduce documentation site, someone says: "If $initial parameter is not an int, array_reduce passes 0 (zero) to the callback function". This way exactly my problem. So why does it work in your case? Also the method signature says: (array $input , callback $function [, int $initial ]) note int $initial.
  • @Pekka - yep, I'm just not with it today.
  • @karim79, that happens sometimes. But @Gordon beat us both, his solution is definitely the best.
  • this also doesn't work for me, it doesn't change the input array in any way.
  • You'd have to wrap that into an RecursiveIteratorIterator as well to be able to iterate over the first level elements. Also, $array won't be an array but, like the classname suggests, an iterator, wrapping an array.