0

I have a slider for news items and doing a loop to get post info for each. Works fine but I am stuck with one paramater that is returned by json and I loop trough it again . the $xtras parameter returns some additional values one of them is additional intro text. I need to check if this value isset otherwise I need to return default intro text.

problem I have is that if $xtra_values->id 44 does not exists I get the intro text from previous post instead that post $post['intro']

$xtras returns stdClass Object like this

stdClass Object
(
    [id] => 1
    [value] => 38
)
stdClass Object
(
    [id] => 28
    [value] => 1
)
stdClass Object
(
    [id] => 44
    [value] => This is extra intro text
)

and this is my loop

foreach ($post_array as $key=> $posts){

    $xtras  = json_decode($posts['xtra']);

     foreach($xtras as $key=> $xtra_values){

        if($xtra_values->id == 44){
           $intro_text = $xtra_values->value;
        }else{
           $intro_text = $post['intro'];
        }     

     }




      echo $post['title'];.'<br />';
      echo $intro_text;
}

Any help is appreciated. Thank you!

2 Answers 2

2

foreach ($post_array as $key=> $posts){

$xtras  = json_decode($posts['xtra']);
$intro_text = null;


 foreach($xtras as $key=> $xtra_values){

    if($xtra_values->id == 44){
       $intro_text = $xtra_values->value;
    }     

 }
if($intro_text == null){
       $intro_text = $post['intro'];
    }
Sign up to request clarification or add additional context in comments.

5 Comments

same as from mgraph , does not work and $xtra_values->id is always set since it returns more than 1 stdClass Object, in both cases now I get only $post[intro]
is $xtra_values an array of StdClasses?
foreach($xtras as $key $xtra_values){ to foreach($xtras as $key => $xtra_values){ typo?
yes just typo , what I need is to check if $xtra_values->id == 44 is set , not if $xtra_values->id is set but dont know how to check for that only
AWESOME! thank you much , I should have gotten myself out that second foreach thank you!!!!
1

try:

if(isset($xtra_values->id) && $xtra_values->id >= 0 && $xtra_values->id == 44){

3 Comments

does not work and $xtra_values->id is always set since it returns more than 1 stdClass Object, in both cases now I get only $post[intro]
cant do that , look at the json result above , I have more than just one
yes , I know what >= is for , but look at the result above , it will check in all cases and always be true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.