PHP Pull from Array

I'm trying to:

values->likes->_total
and
values->updateComments->_total

From this array.

array(2) {
  ["_total"]=>
  int(2)
  ["values"]=>
  array(2) {
    [0]=>
    array(10) {
      ["isCommentable"]=>
      bool(true)
      ["isLikable"]=>
      bool(true)
      ["isLiked"]=>
      bool(false)
      ["likes"]=>
      array(2) {
        ["_total"]=>
        int(1)
        ["values"]=>
        array(1) {
          [0]=>
          array(0) {
          }
        }
      }
      ["numLikes"]=>
      int(1)
      ["timestamp"]=>
      int(1437164088854)
      ["updateComments"]=>
      array(2) {
        ["_total"]=>
        int(1)
        ["values"]=>
        array(1) {
          [0]=>
          array(5) {
            ["comment"]=>
            string(4) "...."
            ["company"]=>
            array(2) {
              ["id"]=>
              int(6408466)
              ["name"]=>
              string(17) "Odem Global, Inc."
            }
            ["id"]=>
            int(6027903381492817920)
            ["sequenceNumber"]=>
            int(0)
            ["timestamp"]=>
            int(1437164159147)
          }
        }
      }
      ["updateContent"]=>
      array(2) {
        ["company"]=>
        array(2) {
          ["id"]=>
          int(6408466)
          ["name"]=>
          string(17) "Odem Global, Inc."
        }
        ["companyStatusUpdate"]=>
        array(1) {
          ["share"]=>
          array(5) {
            ["comment"]=>
            string(36) "Test LinkedIn test description123123"
            ["id"]=>
            string(20) "s6027903086507409408"
            ["source"]=>
            array(3) {
              ["application"]=>
              array(1) {
                ["name"]=>
                string(17) "Odem Global Login"
              }
              ["serviceProvider"]=>
              array(1) {
                ["name"]=>
                string(8) "LINKEDIN"
              }
              ["serviceProviderShareId"]=>
              string(20) "s6027903086507409408"
            }
            ["timestamp"]=>
            int(1437164088854)
            ["visibility"]=>
            array(1) {
              ["code"]=>
              string(6) "anyone"
            }
          }
        }
      }
      ["updateKey"]=>
      string(35) "UPDATE-c6408466-6027903086494838784"
      ["updateType"]=>
      string(4) "CMPY"
    }
    [1]=>
    array(6) {
      ["isCommentable"]=>
      bool(false)
      ["isLikable"]=>
      bool(false)
      ["timestamp"]=>
      int(1436885039866)
      ["updateContent"]=>
      array(2) {
        ["company"]=>
        array(2) {
          ["id"]=>
          int(6408466)
          ["name"]=>
          string(17) "Odem Global, Inc."
        }
        ["companyJobUpdate"]=>
        array(2) {
          ["action"]=>
          array(1) {
            ["code"]=>
            string(4) "none"
          }
          ["job"]=>
          array(6) {
            ["company"]=>
            array(2) {
              ["id"]=>
              int(6408466)
              ["name"]=>
              string(17) "Odem Global, Inc."
            }
            ["description"]=>
            string(1028) "Award winning global marketing agency with groundbreaking technology offering is seeking a Marketing Director to support both client and internal initiatives. Qualified candidates will have:   - Ability to put together a sound marketing recommendation (strategy and plan) from a client brief of category, budget, and goal. - Experience with cross-channel, integrated marketing solutions (Design, Development, Production, and Media) including Traditional "above the line" tactics, Digital, Social, Search, and Influencer Marketing.   - Bachelors Degree in Marketing, Communications, Advertising, or similar from reputable four year College or University. (Advanced degree a plus but not required)   - Minimum of four years experience at a major advertising, marketing communications, media, or public relations firm. - Direct client management experience having served as day-to-day lead. (Managerial experience a plus but not required) Responsibilities will include: - Providing strategic oversight to day-to-day team  - Lea"
            ["id"]=>
            int(62367178)
            ["locationDescription"]=>
            string(26) "Greater New York City Area"
            ["position"]=>
            array(1) {
              ["title"]=>
              string(18) "Marketing Director"
            }
            ["siteJobRequest"]=>
            array(1) {
              ["url"]=>
              string(53) "https://www.linkedin.com/jobs?viewJob=&jobId=62367178"
            }
          }
        }
      }
      ["updateKey"]=>
      string(35) "UPDATE-c6408466-6026732670262796289"
      ["updateType"]=>
      string(4) "CMPY"
    }
  }
}

Open in new window


I've tried this but not working.
for($i=0; $i<$data['_total']; ++$i){
        $id = $data['values'][$i]['likes'];
        $name=$data['values'][$i]['updateComments'];
        echo $id.' '.$name;

Open in new window

LVL 12
Nathan RileyFounderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
With questions like this it's helpful to create the test data set using this function.  It gives us something we can copy, install and test immediately.
http://php.net/manual/en/function.var-export.php

If you can give us a var_export() representation, I'm sure we can give you a tested and working example quickly!
0
Ray PaseurCommented:
And now that I look at it a little more, I'm thinking that the original data was in a different format.  Perhaps it was a JSON string or an XML document?  If you can give us the complete text of the original data, we might be able to show you a good way of handling this.
0
hieloCommented:
To help you "see" the path/trail to the elements that you want, try issuing:
echo '<pre>', print_r($data, true), '</pre>';

Open in new window


You should see an "array tree" that is a little easier to understand than the one from var_dump.

echo 'likes_total' . $data['values'][0]['likes']['_total'];
echo 'updateComments_total' . $data['values'][0]['updateComments']['_total'];

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Here are a couple of guesses (similar to what hielo shows) but as is true with most things, you get better results when you have a representative test data set - then you don't have to guess because you can test!  

In my personal and professional experience, I find that the most important thing a programmer can have is a good test case.  This is borne out by the work of Sebastian Bergmann and other leading thinkers in software development.
// UNTESTED EXAMPLES
foreach ($arr as $sub)
{
    var_dump($sub['values'][0]['likes']['_total']);
    var_dump($sub['values'][0]['updateComments']['_total']);
}

// ALTERNATIVE UNTESTED EXAMPLES
$val = $arr['values'][0];
var_dump($val['likes']['_total']);
var_dump($val['updateComments']['_total']);

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.