PHP 2d阵列推送问题

时间:2015-04-20 17:23:47

标签: php arrays laravel-5 array-push

我在绑定..

这是我想要的输出;

Array
(
[country] => Array(
                    [0] => England
                    [1] => Channel Islands
                )
[gor] => Array(
                    [0] => North East
                    [1] => North West
                )
[parliamentaryconstituency] => Array(
                    [0] => Aldershot
                    [1] => Aldridge-Brownhills
                )
)

这就是我现在所拥有的;

Array
(
[0] => Array
    (
        [country] => England
    )
[1] => Array
    (
        [country] => Channel Islands
    )
[2] => Array
    (
        [gor] => North East
    )
[3] => Array
    (
        [gor] => North West
    )
[4] => Array
    (
        [parliamentaryconstituency] => Aldershot
    )
[5] => Array
    (
        [parliamentaryconstituency] => Aldridge-Brownhills
    )
)

我的代码是;

foreach ($input as $key => $value) {
    foreach ($value as $subkey => $subvalue) {
        switch ($key)
        {
            case 'country':
                $country = Country::description($subvalue)->get()->first();
                array_push($new_input, array('country' => $country->description));
              break;                    
            case 'gor':
                $gor = Gor::description($subvalue)->get()->first();
                array_push($new_input, array('gor' => $gor->description));
              break;
            case 'parlc':
                $parliamentaryconstituency = ParliamentaryConstituency::description($subvalue)->get()->first();
                array_push($new_input, array('parliamentaryconstituency' => $parliamentaryconstituency->description));
              break;
        }
    }
}

我考虑了array_push($new_input['country'], $country->description);并且$new_input['country']位于foreach之上,但是如果没有选择国家/地区,则会输出一个空的国家/地区数组,如果没有选择国家/地区,我希望它不会出现就是这样。

3 个答案:

答案 0 :(得分:2)

你可以尝试这个:

$result = array();
foreach ($input as $key => $value) {
    foreach ($value as $subkey => $subvalue) {
        switch ($key)
        {
            case 'country':
                $country = Country::description($subvalue)->get()->first();
                if ($country) {
                    $result['country'][] = $country;
                }
                break;
            case 'gor':
                $gor = Gor::description($subvalue)->get()->first();
                if ($gor) {
                    $result['gor'][] = $gor;
                }
                break;
            case 'parlc':
                $parliamentaryconstituency = ParliamentaryConstituency::description($subvalue)->get()->first();
                if ($parliamentaryconstituency) {
                    $result['parliamentaryconstituency'][] = $parliamentaryconstituency;
                }
                break;
        }
    }
}
print_r($result);

答案 1 :(得分:0)

array_push总是在数组的末尾添加一个新元素。相反,使用像这样的所有键初始化数组

$result = array("country" => array(), "gor" => array(), etc);

在你的循环中,假设你要插入的值是$ value do

$result[$key][] = $value

或者如果您更喜欢使用array_push

array_push($result[$key], $value);

答案 2 :(得分:-1)

'$list=array();'


/add array value
'array_push($list,array("name"=>"jone","surname"=>"doe"));'

//get array value
'$list[index][columnname]'