0

我收到一个包含以下信息的对象

Array ( [0] => stdClass Object ( [campaigns_id] => 100 [views] => 28 )
        [1] => stdClass Object ( [campaigns_id] => 99 [views] => 346 )
        [2] => stdClass Object ( [campaigns_id] => 98 [views] => 73 )
        [3] => stdClass Object ( [campaigns_id] => 97 [views] => 204 )
        [4] => stdClass Object ( [campaigns_id] => 96 [views] => 10 )
        [5] => stdClass Object ( [campaigns_id] => 57 [views] => 12 )
      )

基本上,它的每一行都有两个值——一个 ID 和一个视图计数。

如果我的“活动 ID”为“100”,我将如何成功地从该对象中获取数字“28”?

4

3 回答 3

1

尝试,

//$arr is the array, loop through it
foreach ($arr as $v) {
 if( $v->campaigns_id === 100 ) //Similarly you can get any other value for 'views'
  echo $v->views;
}
于 2013-01-23T23:40:15.197 回答
1

循环遍历你的数组

$yourval = 0;;

foreach( $yourarray as $id => $val ){

if( $val->campaigns_id === 100 ){
  $yourval = $val->views;
}

}

echo $yourval;
于 2013-01-23T23:40:45.120 回答
1

您可以内联或作为函数执行此操作,我认为您可能更容易将其视为函数,所以在这里。

$view = get_view($the_array);

function get_view(& $my_array) {

  foreach($my_array as $object) {

    if($object->campaigns_id == 100) return $object->views;

  }

  return false;

}
于 2013-01-23T23:41:20.587 回答