array_key_exists( )函數使用問題

來源:互聯網
上載者:User
$res數組,使用print_r($res);語句輸出後為:
Array ( [0] => Array ( [tagname] => KF_FIC01341.TOTALIZERA_2.PV [units] => KNm3 [value] => 0 ) [1] => Array ( [tagname] => KF_FIC01342.TOTALIZERA_4.PV [units] => KNm3 [value] => 0 ) )
在使用array_key_exists("value",$res);語句時,返回false。我仔細查看知道原因了,$res數組是多維陣列,鍵名有兩個:0、1,不存在value,所以返回了false。
那該如何判斷$res數組,有value鍵啊?就是多維陣列咋寫代碼?


回複討論(解決方案)

array_key_exists("value",$res[0])

$arr = Array ('0' => Array ( 'tagname' => 'KF_FIC01341.TOTALIZERA_2.PV','units' => 'KNm3', 'value' => 0 ) ,'1' => Array ( 'tagname' => 'KF_FIC01342.TOTALIZERA_4.PV', 'units' => 'KNm3' ,'value' => 0 ));function  check_index ( $item ,  $key ){    if(array_key_exists("value",$item)){    echo "存在此索引值"."
"; }else{ echo "沒有此索引值"."
"; }}array_walk ( $arr , 'check_index' );

$arr=array ( 0 => array ( 'tagname' =>'KF_FIC01341.TOTALIZERA_2.PV',              'units' => 'KNm3',              'value' => 0 ),1 => array ( 'tagname' =>'KF_FIC01342.TOTALIZERA_4.PV',               'units'=> 'KNm3',               'value' => 0 ) );function my(){    $arr=func_get_args();    foreach($arr as $k=>$v){        $item=array_key_exists('value',$v);        if($item) {          echo '存在';          break;          }else{           '不存在';         }     }}call_user_func_array('my',$arr);

版主方法最簡單,謝謝各位~~

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.