PHP 二维数组推送问题
PHP 2d Array Push Issue
我进退两难了..
这是我想要的输出;
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
之上,但是如果没有选择国家,那将输出一个空的国家数组,我不希望它出现在如果是这样的话。
你可以试试这个:
$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);
array_push 总是在数组末尾添加一个新元素。相反,使用像这样的所有键初始化数组
$result = array("country" => array(), "gor" => array(), etc);
并且在你的循环中假设你想要插入的值在 $value do
$result[$key][] = $value
或者如果您更喜欢使用 array_push
array_push($result[$key], $value);
'$list=array();'
/add array value
'array_push($list,array("name"=>"jone","surname"=>"doe"));'
//get array value
'$list[index][columnname]'
我进退两难了..
这是我想要的输出;
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
之上,但是如果没有选择国家,那将输出一个空的国家数组,我不希望它出现在如果是这样的话。
你可以试试这个:
$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);
array_push 总是在数组末尾添加一个新元素。相反,使用像这样的所有键初始化数组
$result = array("country" => array(), "gor" => array(), etc);
并且在你的循环中假设你想要插入的值在 $value do
$result[$key][] = $value
或者如果您更喜欢使用 array_push
array_push($result[$key], $value);
'$list=array();'
/add array value
'array_push($list,array("name"=>"jone","surname"=>"doe"));'
//get array value
'$list[index][columnname]'