Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7626117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:16:56+00:00 2026-05-31T05:16:56+00:00

Anyone know how to do this? I’m trying to loop through an array where

  • 0

Anyone know how to do this?

I’m trying to loop through an array where not every object has a value. What I’d like to get is an array where the key holds the number that specifies the stdClass Object and has “value” as value. If there is no “value” specified “0” should be printed.

So the final array should look something like this:

Array ( 
[123456789_123456789] => 192 
[54321_98765] => 0 
[987654321_123456789] => 1292
[987854321_123456734] => 0
)

Here is the original output:

stdClass Object ( 
[123456789_123456789] => stdClass Object ( 
    [data] => Array ( 
        [0] => stdClass Object ( 
        [id] => 123456789_123456789/insights/name_of_metric/period 
        [name] => name_of_metric
        [period] => lifetime 
        [values] => 
            Array ( 
                [0] => stdClass Object ( 
                [value] => 1292 ) 
                ) 
        [title] => Lifetime Post Organic Reach 
        [description] => A Description ... 
        [paging] => stdClass Object ( 
        [previous] => https://graph.facebook.com/ALINK
        [next] => https://graph.facebook.com/ALINK
 ) )

[54321_98765] => stdClass Object ( 
    [data] => Array ( 

        [paging] => stdClass Object ( 
        [previous] => https://graph.facebook.com/ALINK
        [next] => https://graph.facebook.com/ALINK
))

[987654321_123456789] => stdClass Object ( 
    [data] => Array ( 
        [0] => stdClass Object ( 
        [id] => 987654321_123456789/insights/name_of_metric/period 
        [name] => name_of_metric
        [period] => lifetime 
        [values] => 
            Array ( 
                [0] => stdClass Object ( 
                [value] => 1292 ) 
                ) 
        [title] => Lifetime Post Organic Reach 
        [description] => A Description ... 
        [paging] => stdClass Object ( 
        [previous] => https://graph.facebook.com/ALINK
        [next] => https://graph.facebook.com/ALINK
))

[987854321_123456734] => stdClass Object ( 
    [data] => Array ( 

        [paging] => stdClass Object ( 
        [previous] => https://graph.facebook.com/ALINK
        [next] => https://graph.facebook.com/ALINK
))
)

This is what I tried so far:

    $aVar = array();
    foreach ($var as $var_1){ 
       foreach ($var_1->data as $var_2 ){ 
          foreach ($var_2->values as $var_3){ 
    $aVar[] = $var_3->value; 
        } 
    } 
}

I believe I have to use foreach (var_1->data as $var_2 => $var2_1)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:16:57+00:00Added an answer on May 31, 2026 at 5:16 am

    The isset() “language construct” and is_array() function are your friend in this scenario, unless you are having another problem not specified, your problem is not very clear 😉

    What you need to do is change your outer for-each loop to something similar to the following, with additional validation within:

    $aVar = array();
    foreach ($var as $object_key=>$var_1)
    { 
        if(isset($var_1->data) && is_array($var_1->data))
        {
            foreach ($var_1->data as $var_2 )
            { 
                if(isset($var_2->values) && is_array($var_2->values))
                {
                    foreach ($var_2->values as $var_3)
                    { 
                        $aVar[$object_key] = $var_3->value; 
                    }
                }
            }
        }
    }
    

    This code is untested, I don’t believe I have made any syntactical errors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Anyone know why this: <?PHP $title = trim($_POST['title']); $description = trim($_POST['description']); // Array of
Does anyone know what this means. Getting this in C# winforms applications: Not a
Does anyone know what this value is for? If it is set to 1,
Does anyone know if this has been released yet? I went to asp.net and
Does anyone know why this code doesn't work. This means, the alert is NOT
I know this has been asked before but I'd like to ask it here
I know this has to be be doable, does anyone know how and if
I know this sort of question has been banded about before but I've not
I know this question has been asked, how to make a site like facebook/gmail
Does anyone know of a good implementation of bcrypt, I know this question has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.