2012-02-19 75 views
0

陣列形式搜索與嵌套數組的數組並返回

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值?

任何幫助將不勝感激。

回答

1

你,你array_key_exists可以起到檢查是否存在與給定鍵的任何元素。

$title = 'e7a36fadf2410205f0768da1b61156d9'; 
if (array_key_exists($title, $arr)) { 
    return array('price'=>$arr[$title]['price'], 'qty'=>$arr[$title]['qty']); 
} 
+0

謝謝效果很好 – Claremont 2012-02-19 19:52:39

1
$title = 'e7a36fadf2410205f0768da1b61156d9'; 

if (isset($arr[$title])) { 
    return array('price'=>$arr[$title]['price'], 'qty'=>$arr[$title]['qty']); 
} 
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 
}