1

Pulling my hair out here..

My JSON output is like this:

Array ( 
    [total] => 1 
    [rows] => Array ( 
            [0] => Array ( 
                [id] => 45 
                [name] => MacBook Pro (Retina 15-inch Late 2013) 
                [asset_tag] => 3041974 
                [serial] => C02M73123455 
                ...etc...

How do I output only the [asset_tag] ?

I am using :

$responseArray=json_decode($results,true);

I have tried:

echo $responseArray['asset_tag'];

echo $responseArray[0]['asset_tag'];

echo $responseArray->asset_tag;

Thanks

3
  • can you provide full array so it will help Commented Jun 28, 2019 at 5:41
  • single access is like: echo $responseArray['rows'][0]['asset_tag']; Commented Jun 28, 2019 at 5:46
  • Please learn basics. It will save you time. Commented Jun 28, 2019 at 7:13

3 Answers 3

1

You can apply foreach()

foreach($responseArray['rows'] as $row){
   echo $row['asset_tag'].PHP_EOL;
}

Sample output:- https://3v4l.org/8SmuY

Sign up to request clarification or add additional context in comments.

2 Comments

Yes. The foreach loop looks fine. Thank you very much.
@user2720970 glad to help you :):)
0

To access the asset_tag of all elements:

foreach ($responseArray['rows'] as $key => $value) {
    echo $value['asset_tag'];
}

Comments

0

You can get asset_tag collection like this:

$assertTags = array_cloumn($responseArray['row'], 'asset_tag');

Comments

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.