Khine Thu
Khine Thu

Reputation: 137

How to access a value within a json string which is a deep value in an array

My array is :

$firstData=array (
    '000' => array (
        '000' => array (
            0 => '{"code":"11101000000","postal":"3310000","prefecture_kana":""}',
        ),
    ),
)

I want to get postal's value in this array. Could you help me to get postal value 3310000?

Upvotes: -1

Views: 64

Answers (2)

Duy Huynh
Duy Huynh

Reputation: 291

I think this is a very basic PHP technique. You can get a value by key ex: $value = $arr['key'].

Let's try

$jsonString = $firstData['000']['000'][0];
$array_from_json = json_decode($jsonString , true);
echo( $array_from_json['postal']);

Explanation

Firstly, you should get a JSON string first

$jsonString = $firstData['000']['000'][0];

$jsonString: '{"code":"11101000000","postal":"3310000","prefecture_kana":""}'

Then, we will parse this JSON string to array by using json_decode:

$array_from_json = json_decode($jsonString , true);

$array_from_json: ["code"=>"11101000000","postal"=>"3310000","prefecture_kana"=>""]

Finally, get value in this array by a key "postal"

$postal = $array_from_json['postal'];

$postal: 3310000

Upvotes: 1

Quan Nguyen
Quan Nguyen

Reputation: 99

I think $firstData['postal] @@

Upvotes: 0

Related Questions