2013-04-24 209 views
1

我有一個多維數組,我想獲取數組集只匹配我的具體值,現在我有一個列調用related_users,我需要一個語句,將獲得此數組值當這個數組的related_user值爲1PHP多維數組按特定值

Array 
(
[0] => Array 
    (
     [id] => 1 
     [advertiser] => Hairvolution 
     [postdate] => 
     [campaign_period] => 
     [related_users] => 1 
     [reporting_period] => 
     [Delivered Impressions] => 1439763 
     [Clicks] => 4124 
     [Click-Through Rate] => 0.29 
    ) 

[1] => Array 
    (
     [id] => 4 
     [advertiser] => 
     [postdate] => 
     [campaign_period] => 
     [related_users] => 2 
     [reporting_period] => 
     [Delivered Impressions] => 
     [Clicks] => 
     [Click-Through Rate] => 
    ) 

[2] => Array 
    (
     [id] => 7 
     [advertiser] => maxlibin 
     [postdate] => 
     [campaign_period] => 
     [related_users] => 2 
     [reporting_period] => 
     [Delivered Impressions] => 
     [Clicks] => 
     [Click-Through Rate] => 
    ) 
[3] => Array 
    (
     [id] => 8 
     [advertiser] => maxlibin 
     [postdate] => 
     [campaign_period] => 
     [related_users] => 1 
     [reporting_period] => 
     [Delivered Impressions] => 
     [Clicks] => 
     [Click-Through Rate] => 
    ) 

[4] => Array 
    (
     [id] => 9 
     [advertiser] => maxlibin 
     [postdate] => 
     [campaign_period] => 
     [related_users] => 1 
     [reporting_period] => 
     [Delivered Impressions] => 
     [Clicks] => 
     [Click-Through Rate] => 
    ) 
} 
+1

不要說 「我需要一個說法」,更好地告訴我們,你已經嘗試什麼。 – Antony 2013-04-24 05:22:03

+0

你可以給一個「我想讓數組只匹配我的具體數值」的例子嗎? – bestprogrammerintheworld 2013-04-24 05:24:32

回答

1

使用for循環並重復並檢查related_users值,如果匹配1然後追加數組新的數組。

$arr = array(); 
for($i=0;$i< count($your_array);$i++) { 
    if($your_array[$i]['related_users'] == 1) { 
     $arr[] = $your_array[$i]; 
    } 
} 
print_r($arr); 
0

我不知道,你想要什麼,但你可以試試這個代碼:

$final = array(); 
    foreach($arrayName as $arrayItem){ 
     if ($arrayItem['related_user'] == 1){ 
      $final[] = $arrayItem; 
     } 
    } 
var_dump($final); 
0

使用foreach爲數組簡單的方法來遍歷數組和檢查related_users值,如果是1然後附加到新陣列$result

$result = array(); 
foreach($your_array as $Item){ 
    if ($Item['related_user'] == 1){ 
    $result[] = $Item; 
    } 
} 
print_r($result); 
0
$rated = array(); 
foreach($yourArr as $arr) { 
    if($arr['related_user'] == 1) { 
     $rated[] = $arr; 
    } 
} 
0

使用此

   $array=YourArray; 
      $result=array(); 
      foreach($array as $a) { 

       if($a['related_users']==1){ 
        $result=$a; 
       } 

      } 

      echo "Resultant Array ="; 
      echo "<pre>"; 
       print_r($result); 
      echo "</pre>"; 
0
function get_specific_arr_set($array) { 

     $arr_final = array(); 
     if (!empty($array)) { 

     foreach ($array as $item) { 

      if ($item['related_users'] == 1) { 

       $arr_final[] = $item; 
      } 
     } 
     } 

     return $arr_final; 
    } 

$result = get_specific_arr_set($your_arr);

echo "<pre>";print_r($result);echo "</pre>";exit;