2016-12-02 42 views
1

我有一個字符串,我需要知道該數組中存在哪個字符串。我的陣列如下:如何從任何數組中找到密鑰php

array(3) 
{ 
    [0]=>object(stdClass)#47170 (3) 
    { 
     ["countries"]=>string(2) "HK" 
     ["last_seen_date"]=>string(10) "2016-09-17" 
     ["ad_uid"]=>string(14) "157d5908a1ca83" 
    } 
    [1]=>object(stdClass)#47171 (3) 
    { 
     ["countries"]=>string(2) "HK" 
     ["last_seen_date"]=>string(10) "2016-09-27" 
     ["ad_uid"]=>string(14) "157d7978513bc3" 
    } 
    [2]=>object(stdClass)#47230 (3) 
    { 
     ["countries"]=>string(2) "HK" 
     ["last_seen_date"]=>string(10) "2016-09-27" 
     ["ad_uid"]=>string(14) "157ea7239824e9" 
    } 
} 

最後看到的日期是:2016年9月27日
我想知道在數組中存在哪些索引2016-09-27。所以我知道與該日期相關的ad_uid。我有一個方法可以做到這一點。

public function getAd_uid($last_seen_date,$values){  
$key = array_keys($values,$last_seen_date); 
print_r($key);  
} 

結果得到一個空數組。我曾嘗試array_serach()具有相同的空結果。任何其他解決方案來取得成果?

+1

你要搜索一個數組的關鍵? http://php.net/manual/en/function.array-key-exists.php或者你想搜索數組值? http://php.net/manual/en/function.array-search.php –

+0

你的意思是「什麼索引」,而不是一個單一的索引,對嗎? –

+0

是的,沒有一個索引 – sanainfotech

回答

1

要在特定日期查找所有$ad_uids last_seen,您可以使用array_filter這將返回您正在尋找的所有元素。如果你只需要ad_uids,你可以申請array_map到數組如下:

<?php 
// $array is the array in question. 

$filtered = array_filter($array, function($item) { 
    return $item->last_seen_date == "2016-09-27"; 
}); 

$ad_uids = array_map(function($item){return $item->ad_uid;}, $filtered); 

Example

+0

感謝亞歷克斯,這工作 – sanainfotech

0

由於每個陣列的每個條目是一個對象,你知道attributs'論文對象的名字(我以爲他們永遠不會改變),我會做這樣的:

/** 
* @param string $last_seen_date 
* @param array $values 
* @return mixed null|int 
*/ 
function getAdUid($last_seen_date, array $values) { 
    // Just in case no entry match 
    $matching_index = null; 
    // Loop through each entry: $entry is an object 
    foreach($values as $index => $entry) { 
     if($entry->last_seen_date == $last_seen_date) { 
      $matching_index = $index; 
      break; // end loop: we found that we are looking for 
     } 
    } 

    return $matching_index; 
} 
0

這樣做只是循環您的陣列

foreach($values as $key => $row) { 
    // do something 
} 

然後檢查$last_seen_date等於循環索引last_seen_date $row->last_seen_date

if ($row->last_seen_date == $last_seen_date) { 
    return $key; 
} 

如果只返回

return $key; 

所以你的PHP代碼會是這樣

$arr = array(
    0 => 
     (object)array(
     "countries" => "HK", 
     "last_seen_date" => "2016-09-17", 
     "ad_uid"=> "157d5908a1ca83" 
    ), 
    1 => 
     (object)array(
     "countries" => "HK", 
     "last_seen_date" => "2016-09-20", 
     "ad_uid" => "157d7978513bc3" 
    ), 
    2 => 
     (object)array(
     "countries" => "HK", 
     "last_seen_date" => "2016-09-26", 
     "ad_uid" => "157ea7239824e9" 
    ) 
); 
function getAd_uid($last_seen_date, $values){ 
    foreach($values as $key => $row) { 
     if ($row->last_seen_date == $last_seen_date) { 
      return $key; 
     } 
    } 
} 


echo '2016-09-17 is on index => '.getAd_uid('2016-09-17', $arr).'<br>'; 
echo '2016-09-20 is on index => '.getAd_uid('2016-09-20', $arr).'<br>'; 
echo '2016-09-26 is on index => '.getAd_uid('2016-09-26', $arr).'<br>'; 

結果

enter image description here

Working Demo

相關問題