我有主数组:
$data_cities = Array (
[0] => Array (
[_type] => _items_city_sorting [item_city_sorting] => Moscow
)
[1] => Array (
[_type] => _items_city_sorting [item_city_sorting] => Saint-Petersburg
)
[2] => Array (
[_type] => _items_city_sorting [item_city_sorting] => Sochi
) )
我想制作另一个数组,就像:
$cities = array (
[1] => 'Moscow',
[2] => 'Saint-Petersburg',
[3] => 'Sochi' )
我使用的函数仅返回最后一个值
if ( $data_cities ) {
foreach ( $data_cities as $key => $city ) {
$cities[ $city->$key+1 ] = $city['item_city_sorting'];
}
}
Array ( [1] => Sochi )
我在做什么错?感谢您的帮助=)
如果重要的话,可以使用Carbon Fields获得主阵列
答案 0 :(得分:0)
只要您想要键+1的数字,只需在foreach
中使用以下术语即可:
$cities[ $key+1 ] = $city['item_city_sorting'];
答案 1 :(得分:0)
您可以尝试在foreach中进行以下操作:
$cities[] = $city['item_city_sorting'];
or
array_push($cities,$city['item_city_sorting']);
将值动态添加到您的城市数组中
例如
if ( $data_cities ) {
foreach ( $data_cities as $key => $city ) {
$cities[] = $city['item_city_sorting'];
}
}