2016-12-02 7 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

감사합니다. Alex, this worked – sanainfotech

0

는 객체이고 당신은 논문 객체의 attributs의 이름 (나는 그들이 변하지 않는 가정), I는 다음과 같이 그것을 할 것입니다 알고

/** 
* @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

을 그렇게하기를 단지 루프 배열 $last_seen_date가 last_seen_date 루프 인덱스와 동일한 경우

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

후 확인 $row->last_seen_date

if ($row->last_seen_date == $last_seen_date) { 
    return $key; 
} 
0,123,516

그냥 반환하면

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