0

I Need to push in a array all the values of "total_value" and all the values of "car_id" that came form a json url.

Json:

[
  {
    "control": {
      "color": "blue",
      "total_value": 21.5,
      "car_id": 421118
    }
  },
  {
    "control": {
      "color": "green",
      "total_value": 25,
      "car_id": 421119
    },
  {
    "control": {
      "color": "red",
      "total_value": 18,
      "car_id": 421519
    }
}
]

My Php:

<?php

$getJson = file_get_contents("http://url.com/control.json",false);

$j = json_decode($getJson);
4
  • Does $j = json_decode($getJson,true); (add true as second value) help? Commented Feb 17, 2018 at 12:18
  • maybe, but then? how I store the values in diferent arrays? Commented Feb 17, 2018 at 12:23
  • What is your expected output? Commented Feb 17, 2018 at 12:23
  • an array with the "total_value" and another array with the "car_id" Commented Feb 17, 2018 at 12:26

3 Answers 3

2

Your json is not properly formatted, you are missing an }

You could loop your $j and get the values like $item->control->total_value. Then add the 2 values you are looking for to an array and add the array to the $result.

$j = json_decode($data);
$result = [];
foreach ($j as $item) {
    array_push($result,[
        'total_value' => $item->control->total_value,
        'car_id' => $item->control->car_id
    ]);
}

Php output example

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

Comments

0

Something like this:

$result = [];
foreach ($j as $item) {
    array_push($result, [
        'total_value' => $item['total_value'],
        'car_id' => $item['car_id']
    ]);
}

Comments

0

You can also achieve this using array_walk() and removing the item you don't want, this is a shorter amount of code and does not require you to create temporary variables or start with an object and end up with an array.

array_walk($obj, function(&$v, $k) {
    unset($v->control->color);
});

So for example (fixing your broken json):

<?php
$json = '[{
        "control": {
            "color": "blue",
            "total_value": 21.5,
            "car_id": 421118
        }
    },
    {
        "control": {
            "color": "green",
            "total_value": 25,
            "car_id": 421119
        }
    },
    {
        "control": {
            "color": "red",
            "total_value": 18,
            "car_id": 421519
        }
    }
]';

$obj = json_decode($json);

array_walk($obj, function(&$v, $k) {
    unset($v->control->color);
});

print_r($obj);

https://3v4l.org/hXada

Result:

Array
(
    [0] => stdClass Object
        (
            [control] => stdClass Object
                (
                    [total_value] => 21.5
                    [car_id] => 421118
                )

        )

    [1] => stdClass Object
        (
            [control] => stdClass Object
                (
                    [total_value] => 25
                    [car_id] => 421119
                )

        )

    [2] => stdClass Object
        (
            [control] => stdClass Object
                (
                    [total_value] => 18
                    [car_id] => 421519
                )

        )

)

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.