-2

I got the following array called $resource:

{
    "id": 24927,
    "availability": [],
    "block_cost": 0,
    "name": "Nachtklettern 11.08.2020",
    "parent_id": 0,
    "qty": "20",
    "sort_order": 0,
    "meta_data": [{
        "id": 15548,
        "key": "kor_reg_status",
        "value": "off"
    }, {
        "id": 15549,
        "key": "qty",
        "value": "20"
    }, {
        "id": 15550,
        "key": "_arb_reservation_availability",
        "value": []
    }, {
        "id": 15572,
        "key": "kor_reg_status",
        "value": "off"
    }, {
        "id": 15573,
        "key": "qty",
        "value": "20"
    }, {
        "id": 15574,
        "key": "_arb_reservation_availability",
        "value": []
    }, {
        "id": 32463,
        "key": "_fusion",
        "value": []
    }, {
        "id": 96581,
        "key": "_edit_lock",
        "value": "1615588526:3"
    }, {
        "id": 97192,
        "key": "_edit_last",
        "value": "3"
    }]
}

and want to get the first value of qty -> 20

If I try

echo $resource[5] or
echo $resource["qty"] 

I didn't get the value. What can I do?

Patrick R.
  • 53
  • 6

1 Answers1

0

Decode the JSON string first, then retrieval of any property is straightforward:

$resource = '{"id":24927,"availability":[],"block_cost":0,"name":"Nachtklettern 11.08.2020","parent_id":0,"qty":"20","sort_order":0,"meta_data":[{"id":15548,"key":"kor_reg_status","value":"off"},{"id":15549,"key":"qty","value":"20"},{"id":15550,"key":"_arb_reservation_availability","value":[]},{"id":15572,"key":"kor_reg_status","value":"off"},{"id":15573,"key":"qty","value":"20"},{"id":15574,"key":"_arb_reservation_availability","value":[]},{"id":32463,"key":"_fusion","value":[]},{"id":96581,"key":"_edit_lock","value":"1615588526:3"},{"id":97192,"key":"_edit_last","value":"3"}]}';

$res = json_decode($resource);
echo $res->qty;     // 20