1

我有一个多维数组,我想让数组集只匹配我的特定值,现在我有一个列调用related_users,我需要一个语句,只有当这个数组的related_user值为1时才会获取这个数组值。

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] => 
    )
}
4

6 回答 6

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);
于 2013-04-24T05:23:31.650 回答
0

我不确定,你想要什么,但你可以试试这个代码:

$final = array();
    foreach($arrayName as $arrayItem){
        if ($arrayItem['related_user'] == 1){
            $final[] = $arrayItem;
        }
    }
var_dump($final);
于 2013-04-24T05:24:14.840 回答
0

对数组使用foreach是一种简单的方法来遍历数组并检查related_users值,如果它被1追加到新数组$result中。

$result = array();
foreach($your_array as $Item){
  if ($Item['related_user'] == 1){
     $result[] = $Item;
  }
}
print_r($result);
于 2013-04-24T05:26:12.833 回答
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;

于 2013-04-24T06:06:29.643 回答
0
$rated = array();
foreach($yourArr as $arr) {
    if($arr['related_user'] == 1) {
        $rated[] = $arr;
    }
}
于 2013-04-24T05:26:25.880 回答
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>";
于 2013-04-24T05:36:38.457 回答