数组推送php

时间:2017-12-03 13:07:01

标签: php arrays

我希望将多个数组的值放入一个数组中 我创建此代码但是不起作用 它只是推送完整数组的最后一个值 结果必须是

$ rest = Array([OPEN] => Array(),[HAPPY] => Array(),[ALIVE] => Array(),[GOOD] => Array())

<?
error_reporting(0);
 $arr=array(
    0 => array(
        'OPEN' => 'understanding',
        'HAPPY' => 'great',
        'ALIVE' => 'playful',
        'GOOD' => 'calm'
    ),
    1 => array(
        'OPEN' => 'confident',
        'HAPPY' => 'gay',
        'ALIVE' => 'courageous',
        'GOOD' => 'peaceful'
    ),
    2 => array(
        'OPEN' => 'reliable',
        'HAPPY' => 'joyous',
        'ALIVE' => 'energetic',
        'GOOD' => 'at ease'
    ));
$feeling = array();
$x=0;
foreach ($arr[0] as $key => $value) {
$rest = array_merge($feeling,array("$key" => array()));
}
//$rest=Array ( [OPEN] => Array ( ) ) Array ( [HAPPY] => Array ( ) ) Array ( [ALIVE] => Array ( ) ) Array ( [GOOD] => Array ( ) )

 for ($i=0; $i <count($arr) ; $i++) { 

foreach ($arr[$i] as $key => $value) {
    array_push($rest[$key], $arr[$i][$key]);
}

 }
 print_r($rest);


?> 

    -----     结果是什么给了我     -----     排列     (         [GOOD] =&gt;排列             (                 [0] =&gt;冷静                 [1] =&gt;平静的                 [2] =&gt;安逸             )

    [OPEN] =>
    [HAPPY] =>
    [ALIVE] =>
)
----
i want is like that
----
Array
(
    [GOOD] => Array
        (
            [0] => calm
            [1] => peaceful
            [2] => at ease
        )

    [OPEN] =>Array
        (
            [0] => understanding
            [1] => confident
            [2] => reliable
        )
    [HAPPY] =>Array
        (
            [0] => great
            [1] => gay
            [2] => joyous
        )
    [ALIVE] =>Array
        (
            [0] => playful
            [1] => courageous
            [2] => energetic
        )
)

2 个答案:

答案 0 :(得分:0)

我相信这会返回所需的输出?

$arr=array(
    0 => array(
        'OPEN' => 'understanding',
        'HAPPY' => 'great',
        'ALIVE' => 'playful',
        'GOOD' => 'calm'
    ),
    1 => array(
        'OPEN' => 'confident',
        'HAPPY' => 'gay',
        'ALIVE' => 'courageous',
        'GOOD' => 'peaceful'
    ),
    2 => array(
        'OPEN' => 'reliable',
        'HAPPY' => 'joyous',
        'ALIVE' => 'energetic',
        'GOOD' => 'at ease'
    )
);


$output=array();
$keys=array_keys( $arr[0] );

for( $i=0; $i < count( $arr ); $i++ ){
    foreach( $keys as $key )$output[$key][]=$arr[$i][$key];
}
echo '<pre>', print_r($output,true), '</pre>';

结果:

Array
(
    [OPEN] => Array
        (
            [0] => understanding
            [1] => confident
            [2] => reliable
        )

    [HAPPY] => Array
        (
            [0] => great
            [1] => gay
            [2] => joyous
        )

    [ALIVE] => Array
        (
            [0] => playful
            [1] => courageous
            [2] => energetic
        )

    [GOOD] => Array
        (
            [0] => calm
            [1] => peaceful
            [2] => at ease
        )

)

答案 1 :(得分:0)

如果我不理解您的要求,那么此解决方案将适合您。我使用array_column()方法,因为您当前使用for()foreach()循环的代码似乎有点混乱。

$arr=array(
    0 => array(
        'OPEN' => 'understanding',
        'HAPPY' => 'great',
        'ALIVE' => 'playful',
        'GOOD' => 'calm'
    ),
    1 => array(
        'OPEN' => 'confident',
        'HAPPY' => 'gay',
        'ALIVE' => 'courageous',
        'GOOD' => 'peaceful'
    ),
    2 => array(
        'OPEN' => 'reliable',
        'HAPPY' => 'joyous',
        'ALIVE' => 'energetic',
        'GOOD' => 'at ease'
    ));

$expected = [];
$keys = array_keys($arr[0]);
foreach($keys as $key){
   $expected[$key] = array_column($arr,$key);
}
print '<pre>';
print_r($expected);
print '</pre>';

输出

阵列

(
    [OPEN] => Array
        (
            [0] => understanding
            [1] => confident
            [2] => reliable
        )

    [HAPPY] => Array
        (
            [0] => great
            [1] => gay
            [2] => joyous
        )

    [ALIVE] => Array
        (
            [0] => playful
            [1] => courageous
            [2] => energetic
        )

    [GOOD] => Array
        (
            [0] => calm
            [1] => peaceful
            [2] => at ease
        )

)