我有一个脚本,它创建以下格式的数组
$named_array["vehicles"][0]['vehicle'] = "i100-1 " ;
$named_array["vehicles"][1]['vehicle'] = "i100-2 " ;
$named_array["vehicles"][2]['vehicle'] = "i100-46 " ;我想在脚本后面做的是从$named_array获取索引值0-1-2等,但我只有一个值( i100-1等)作为查询选项,这是为了以后可以修改它。我想要实现的是,值为i100-2的$named_array的索引值是多少
这是最后输出到json的代码。
我希望这是有意义的!有什么需要帮忙的吗?
发布于 2013-03-22 02:20:54
function complex_index_of($named_array, $val){
for($i=0, $n=count($named_array['vehicles']); $i<$n; $i++){
if ($named_array['vehicles'][$i]['vehicle'] == $val)
return $i;
}
return -1;
}
echo complex_index_of($named_array, 'i100-2 ');
// output: 1发布于 2013-03-22 02:22:49
尝试这样的操作(如果需要多次创建函数,可以创建一个函数)
$needle = 'i100-1';
$vIndex = -1;
foreach ($named_array["vehicles"] as $index => $data) {
if($data['vehicle'] == $needle) {
$vIndex = $index;
break;
}
}https://stackoverflow.com/questions/15555124
复制相似问题