数组格式
Array
(
[76ea881ebe188f1a7e7451a9d7f17ada] => Array
(
[rowid] => 76ea881ebe188f1a7e7451a9d7f17ada
[id] => 1
[qty] => 2
[price] => 20
[name] => First
[options] => Array
(
[permName] => beer
)
[subtotal] => 40
)
[e7a36fadf2410205f0768da1b61156d9] => Array
(
[rowid] => e7a36fadf2410205f0768da1b61156d9
[id] => 3
[qty] => 6
[price] => 20
[name] => Second
[options] => Array
(
[permName] => achieve
)
[subtotal] => 120
)
我有没有办法在这个数组中搜索标题,或者在这种情况下我们说e7a36fadf2410205f0768da1b61156d9
,一旦找到它,只返回price
和qty
值?
非常感谢任何帮助。
答案 0 :(得分:1)
$title = 'e7a36fadf2410205f0768da1b61156d9';
if (isset($arr[$title])) {
return array('price'=>$arr[$title]['price'], 'qty'=>$arr[$title]['qty']);
}
答案 1 :(得分:1)
这个功能就足够了。
function search_title($title, $array){
if(array_key_exists($array[$title])){
return array($array[$title]['price'],$array[$title]['qty']);
}else{
return array( false, false);
}
}
用法,
list ($price, $qty) = search_title('e7a36fadf2410205f0768da1b61156d9', $array);
if($price!==false){
// search was successful
}
答案 2 :(得分:1)
您可以使用array_key_exists函数来检查是否存在具有给定键的元素。
$title = 'e7a36fadf2410205f0768da1b61156d9';
if (array_key_exists($title, $arr)) {
return array('price'=>$arr[$title]['price'], 'qty'=>$arr[$title]['qty']);
}