“PHP Flatten Array” Kode Jawaban

PHP Flat Multidimensional Array

<?php

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

array php datar

$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 Flatten Array

array_merge(...$a);
Indian Gooner

array php datar

$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

Ratakan dalam array php

array_merge(...$a);
gerson ondangan

Jawaban yang mirip dengan “PHP Flatten Array”

Pertanyaan yang mirip dengan “PHP Flatten Array”

Lebih banyak jawaban terkait untuk “PHP Flatten Array” di PHP

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya