开发者

Another php array looping question

开发者 https://www.devze.com 2023-02-03 05:59 出处:网络
Been battling with this one for what seems, like forever. I have an array: $url_array It contains this info:

Been battling with this one for what seems, like forever.

I have an array:

$url_array

It contains this info:

Array ( 
   [ppp] => Array ( 
      [0] => stdClass Object ( 
         [id] => 46660 
         [entity_id] => 0 
         [redirect_url] => http://www.google.com 
         [type] => Image 
      ) 
      [1] => stdClass Object ( 
         [id] => 52662 
         [entity_id] => 0 
         [pixel_redirect_url] => http://www.yahoo.com 
         [type] => Image 
      ) 
      [2] => stdClass Object ( 
         [id] => 53877 
         [entity_id] => 0 
         [redirect_url] => http://www.msn.com 
         [pixel_type] => Image 
      ) 
   ) 
   [total_count] => 3 
)

I need to loop through it, and do things to each variable. I can get this to work:

foreach ($piggies_array as $key => $value) {
$id = $value[0]->id;
$redirect_url = $value[0]->redirect_url; }

Not unsurprisingly, it's only echoing the first开发者_高级运维 value of those variables, but no matter what I try I cannot get it to loop through:

$value->redirect_url;
$value=>redirect_url;

I would appreciate any help.


This should do the trick:

foreach ($url_array['ppp'] as $key => $object) {
    echo $object->redirect_url;
}


You need to loop through the array twice.

foreach($piggies as $piggy) {

foreach($piggy as $key=>$value) {

$id = $value->id;
$redirect_url = $value->redirect_url; 

}

}


your doing $id = $value[0]->id which will only do the statments for the 0 element

you need a for loo

for($i = 0; $i < count($piggies_array['ppp']); $i++)
{
    $id = $value[$i]->id;
    $redirect_url = $value[$i]->redirect_url;
}   


Your array contains another array by calling $value[0] you are only asking for the first item in the second array. You will need two loops, not forgetting the value may not be an array:

    foreach ($piggies as $var)
    {
        if (is_array($var))
        {
            foreach ($var as $obj)
            {
                echo $obj->redirect_url;
            }
        }
        else 
        {
            echo $var;
        }
    }

The above (not tested) will output each URL and will also output the total count item of the first array.


You're iterating over the first dimension of the array, which only haves the "ppp" element. Check your code, and you'll see that you're actually asking for the first element of the array, when you have 3 elements.

What you have to do is, iterate into the array of objects ($url_array["ppp"]).

When you're in doubt about what you're actually iterating, a good way to start debuggin it, is printing every iteration step. If you do this with the example that you've posted, you'll see this:

Array( [0] => stdClass [...], [1] => stdClass [...] ... )

doing this:

foreach ($piggies_array as $key => $value) {
    var_dump($value);
}

Looking at this, you'll automatically find out what your problem is. So, changing it to:

foreach ($piggies_array['ppp'] as $key => $value) {
    var_dump($value);
}

You'll have the result you want:

Object ( id: ... )
Object ( id: ... )
Object ( id: ... )

Good luck!


loop like this (assuming that $piggies_array is the same as $url_array you dumped):

foreach ($piggies_array['ppp'] as $key => $value) {
   $id = $value->id;
   $redirect_url = $value->redirect_url; 
}

and watch out because some of the items in 'ppp' array will not have a property redirect_url (hte second one has a pixel_redirect_url property instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消