“PHP -Flacharray” Code-Antworten

Multidimensionales Array von PHP -Flachabflachen

<?php

$notFlat = [[1,2],[3,4]];
$flat = array_merge(...$notFlat);
var_dump($flat);
Sminkie

Array flach Php

$c = ["a" => ["x" => "X", "y" => "Y"], "b" => ["p" => "P", "q" => "Q"]];
print_r(array_merge(...array_values($c)));

Array
(
    [x] => X
    [y] => Y
    [p] => P
    [q] => Q
)
Defeated Dunlin

Array_flatten Php

/**
 * Function converts multidimentional array to a plain one
 *
 * @param $arr multidimensional array
 *
 * @return array
 */
function array_flatten($arr) {
    $return = [];
    foreach ($arr as $key => $value) {
        if (is_array($value)) {
            $return = array_merge($return, array_flatten($value));
        } else {
            $return[] = $value;
        }
    }
    return $return;
}
P.S.

PHP -Flacharray

array_merge(...$a);
Indian Gooner

Array flach Php

$a = [[10, 20], [30, 40]];
$b = [["x" => "X", "y" => "Y"], ["p" => "P", "q" => "Q"]];

print_r(array_merge(...$a));
print_r(array_merge(...$b));

Array
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
)
Array
(
    [x] => X
    [y] => Y
    [p] => P
    [q] => Q
)
Defeated Dunlin

in Array -PHP abgeflacht

array_merge(...$a);
gerson ondangan

Ähnliche Antworten wie “PHP -Flacharray”

Fragen ähnlich wie “PHP -Flacharray”

Weitere verwandte Antworten zu “PHP -Flacharray” auf PHP

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen