Things About JSON Data

Square brackets represent arrays.

Curly brackets represent objects.

How you would get the sum of all “value” attributes?

$data = array(
'title' => 'Nested Data Test',
'items' => array(
array(
'value' => 5,
),
array(
'value' => 10,
),
),
);

Answer:

$sum = 0;
foreach( $data['items'] as $item) {
  $sum += item['value'];
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s