0

I got a JSON from a web site in the raw format:

{
    "A_B" : {
        "id" : 7,
        "last" : "0.00000038"
    },
    "A_C" : {
        "id" : 8,
        "last" : "0.00001938"
    },
    ...
}

From that I got an unique array $c with all "A_B", ...

I cannot find the right syntax to get the value for 'id', 'last' within the key c[0].

3
  • 1
    Kindly include the codes of what you've already tried Commented Dec 26, 2017 at 4:11
  • You wish to get the id of A_B or id of all the elements? Commented Dec 26, 2017 at 4:12
  • This might help stackoverflow.com/a/3411527/5039470 Commented Dec 26, 2017 at 4:15

3 Answers 3

2

A basic PHP example:

$raw = '{
        "A_B" : {
            "id" : 7,
            "last" : "0.00000038"
        },
        "A_C" : {
            "id" : 8,
            "last" : "0.00001938"
        }
    }';

// Translate from JSON to data object
$data = json_decode($raw);

// Read variables from the data object
echo 'Option 1)<br />
data->A_B->id = '.$data->A_B->id.'<br />
data->A_B->last = '.$data->A_B->last.'<br />';

// Extract and read a single object 
$c = $data->A_B;
echo 'Option 2)<br />
c->id = '.$c->id.'<br />
c->last = '.$c->last.'<br />';

// To an array version. Note: 'true' is included
$ar = json_decode($raw, true);

// Reading a array 
echo 'Option 3)<br />
ar[A_B][id] = '.$ar['A_B']['id'].'<br />
ar[A_B][last] = '.$ar['A_B']['last'].'<br />';

// And finally, I think what you have done
$c = $ar['A_B'];

echo 'Option 4)<br />
c[id] = '.$c['id'].'<br />
c[last] = '.$c['last'].'<br />';

Which will render the following:

Option 1)
data->A_B->id = 7
data->A_B->last = 0.00000038
Option 2)
c->id = 7
c->last = 0.00000038
Option 3)
ar[A_B][id] = 7
ar[A_B][last] = 0.00000038
Option 4)
c[id] = 7
c[last] = 0.00000038

Edit. I think I understand what you are after. See the following updated PHP script.

$raw = '{
    "A_B" : {
        "id" : 7,
        "last" : "0.00000038"
    },
    "A_C" : {
        "id" : 8,
        "last" : "0.00001938"
    }
}';

// Only interested in these valves from the JSON
// Value 'X_Y`' is expected to fail!
$knownId = array('A_C','X_Y');

// Extract JSON to array
$c = json_decode($raw,true);

// Only print values that match $knownId
foreach($knownId as $k => $v) {
    if ($c[$v]) {
        echo '<p>v:['.$v.'] 
             id:['.$c[$v]['id'].'] 
             last:['.$c[$v]['last'].']</p>';
    }
}

Which would display a single result from the sample data in $raw.

v:[A_C] id:[8] last:[0.00001938]
Sign up to request clarification or add additional context in comments.

3 Comments

The problem is that "A_C", "A_D", "B_C" .... are (the only) elements of the unique array $c.
@RonaldWiplinger I think I know what you want now. See edited answer.
@RonaldWiplinger : Happy to have helped!
0

try this assuming you got your array by json_decode($jsnstr,true)

 $id = $c[0]["id"];
 $last = $c[0]["last"];

you can also use it in a for loop and using loop index i as array index

1 Comment

I think this assumes you decode it as an array and not an object, but that might not be obvious to person asking question.
0

Hi Using following you can get the value of "id" and "last"

$json_raw = '{
    "A_B" : {
        "id" : 7,
        "last" : "0.00000038"
    },
    "A_C" : {
        "id" : 8,
        "last" : "0.00001938"
    }
}';

//decode the json 
$data = json_decode($json_raw);


$c = array();

foreach ($data as $key => $value) {
    //collect the keys
    $c[] = $key;
}

$c = array_unique($c);

foreach ($data as $key => $value) {

    //check the key is exist
    if(in_array($key, $c)){

        // do computation
        $id = $value->id;
        $last = $value->last;

        echo "Key is :".$key."  , id :".$id." , last :".$last."\n";
    }


}

Output

Key is :A_B  , id :7 , last :0.00000038
Key is :A_C  , id :8 , last :0.00001938

7 Comments

Json gives me 1000 data, but I have already selected the ones I need. These keys are in the unique array $c. $c contains e.g., "A_B", "A_C", "F_D", ... All solutions here assume, that I have A_B, but it's a variable, stored in an array. Each of these will be worked on to put into a database.
sorry i am not able to understand the problem, can you please explain
json_raw has 1000 dates, all starting with something "A_B", "A_C", "F_D" ... In my first run, I extracted the keys I actually only need, that are now 50. These 50 dates I want to insert into a database. However, all solutions I found assume, that I use hard coded "A_B" instead of the collected 50 keys, stored in a unique array (I called it $c)
in that case instead of extracting u can also insert there only. if u dont mind please share ur code it will easier to understand
$b=array(); array_push($b,'A_B'); array_push($b,'A_C'); array_push($b,'A_B'); ... $c = array_unique($b); From these elements of the array $c I want the values id and last
|

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.