“array_flatten php” Código de respuesta

Php plano de matriz

$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 aplanar la matriz

array_merge(...$a);
Indian Gooner

Php plano de matriz

$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

Respuestas similares a “array_flatten php”

Preguntas similares a “array_flatten php”

Más respuestas relacionadas con “array_flatten php” en PHP

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código