何我可以改变阵列结构?

时间:2018-01-17 18:28:18

标签: php arrays structure

我从数据库中获得以下数组结构,无法在此处进一步了解。也许有人可以帮我改变阵列结构。

Array
(
[0] => Array
    (
        [id] => 302
        [name] => Test Name
    )
)
Array
(
[0] => Array
    (
        [id] => 342
        [name] => Test Name 2
    )
[1] => Array
    (
        [id] => 346
        [name] => Test Name 3
    )
)
Array
(
[0] => Array
    (
        [id] => 344
        [name] => Test Name 4
    )
)

我需要一个根据以下逻辑的数组结构:

Array
(
[0] => Array
    (
        [id] => 302
        [name] => Test Name
    )
)
Array
(
[1] => Array
    (
        [id] => 342
        [name] => Test Name 2
    )
)
Array
(
[2] => Array
    (
        [id] => 346
        [name] => Test Name 3
    )
)
Array
(
[3] => Array
    (
        [id] => 344
        [name] => Test Name 4
    )
)

以下PHP代码生成输出

$ resultUpdateInstition - 这是数据库数组

        $output = false;
        if (is_array($resultUpdateInstition)) {
        foreach ($resultUpdateInstition as $keyUpdateInstition1 => $valueUpdateInstition1) {

            if (is_array($valueUpdateInstition1)) {
            $v = 0;
            foreach ($valueUpdateInstition1 as $keyUpdateInstition1 => $valueUpdateInstition1) {

                if (is_array($valueUpdateInstition1)) {
                foreach ($valueUpdateInstition1 as $usrvalvalvalkey => $usrvalvalvalval) {
                    $output .= $usrvalvalvalkey . " => " . $usrvalvalvalval . '<br />';
                }
                } else {
                 $output = $keyUpdateInstition1 . " => " . $valueUpdateInstition1 ;

                }
            }
            } else {
            $output = $valueUpdateInstition1;
            }
        }
        }

        print_r($output);

我希望有人知道我的意思......,这将是伟大的。 很多你的帮助

m.orange

1 个答案:

答案 0 :(得分:0)

您可以使用array_merge将所有数组合并为一个。这是一个让你前进的例子。

<?php

$arr1 = Array
(
    0 => Array
    (
        'id' => 302,
        'name' => 'Test Name',
    )
);
$arr2 = Array
(
    0 => Array
    (
        'id' => 342,
        'name' => 'Test Name 2',
    ),
    1 => Array
    (
        'id' => 346,
        'name' => 'Test Name 3',
    )
);
$arr3 = Array
(
    0 => Array
    (
        'id' => 344,
        'name' => 'Test Name 4',
    )
);

$arr4 = array_merge( $arr1, $arr2, $arr3 );

var_dump( $arr4 );

/*
result:
array(4) { 
    [0]=> array(2) { ["id"]=> int(302) ["name"]=> string(9) "Test Name" } 
    [1]=> array(2) { ["id"]=> int(342) ["name"]=> string(11) "Test Name 2" } 
    [2]=> array(2) { ["id"]=> int(346) ["name"]=> string(11) "Test Name 3" } 
    [3]=> array(2) { ["id"]=> int(344) ["name"]=> string(11) "Test Name 4" } 
}
*/

http://sandbox.onlinephpfunctions.com/code/b627ec59b07f8ad6a15c9b25125b0bf15ced7e67