Another question related to php array loop

Fight this for what seems, as always.

I have an array:

$url_array

It contains this information:

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 go through it and do things for each variable. I can make this work:

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

Not surprisingly, it only repeats the first value of these variables, but no matter what I try, I cannot miss it:

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

I would appreciate any help.

+3
source share
6 answers

This should do the trick:

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

You need to skip the array twice.

foreach($piggies as $piggy) {

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

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

}

}
+1
source

$id = $value [0] → id, 0

loo

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

, $value[0], . , , :

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

( ) URL-, count .

0

, "ppp". , , , 3 .

, ($url_array["ppp"]).

, , , . , :

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

:

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

, , . , :

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

:

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

!

0

like this (assuming it $piggies_arraymatches the $url_arrayone you dropped):

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

and make sure that some elements in the "ppp" array will not have the redirect_url property (hte second has the pixel_redirect_url property.

0
source

Source: https://habr.com/ru/post/1784097/


All Articles