我正在使用foreach
循环为数组赋值。
$route_selection;
foreach ($routes as $route) {
$from_state = $route->fromState->name;
$to_state = $route->toState->name;
$from_country = $route->fromCountry->name;
$to_country = $route->tocountry->name;
$route_selection[] = [$route->hash_id => 'From: '.$from_state.' ('.$from_country.') To: '.$to_state.' ('.$to_country.')'];
}
我怎么能用PHP做到这一点?
答案 0 :(得分:4)
将阵列分配线更改为:
$route_selection[$route->hash_id] = 'From: '.$from_state.' ('.$from_country.') To: '.$to_state.' ('.$to_country.')';
只要每个hash_id
都是唯一的,这将为每条路线创建一个新的数组元素。
答案 1 :(得分:1)
$array => array(pWjY=>'Value',
YA8N=>'Value',);
您可以添加第三个值:
$array['somekey'] = 'Value';
答案 2 :(得分:1)
更改此行
$route_selection[] = [$route->hash_id => 'From: '.$from_state.' ('.$from_country.') To: '.$to_state.' ('.$to_country.')'];
到
$route_selection[$route->hash_id] = 'From: '.$from_state.' ('.$from_country.') To: '.$to_state.' ('.$to_country.')';
答案 3 :(得分:1)
这样做:
CAAnimation