Nested Arrays retrieved withh json\retrieve

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • esendino
    Senior Member
    • May 2019
    • 158

    Nested Arrays retrieved withh json\retrieve

    I need help to kow haw many objects are there in an array that is nested inside other array.

    Let me explain in detail:

    I got a json that (detailed below) in a working field named $prJsonOrder

    What is relevant is that it has an array object: line_item and that array has nested other arrays: meta:data & taxes (see Json below)

    For normal objects I retrieve the data with this instruction
    $prName= json\retrieve($prJsonOrder, 'id');
    $prParentId = json\retrieve($prJsonOrder, 'parent_id');

    When dealing with the object line_item which is an array I need to know how many are there. So I used

    $prJsonLineItems = json\retrieve($prJsonOrder, 'line_items');
    $prJsonLineItemsCount = array\length($prJsonLineItems);

    Then I need to retrieve the individual data. For that I built a field ($prClave) with the exact address to retrieve. This is the code

    $prCont = 0;
    while($prCont < $prJsonLineItemsCount,
    $prClave = string\concatenate('line_items.', $prCont, '.id');
    $prName = json\retrieve($prJsonOrder, $prClave);
    $prClave = string\concatenate('line_items.', $prCont, '.name');
    $prNombreProducto = json\retrieve($prJsonOrder, $prClave);
    $prClave = string\concatenate('line_items.', $prCont, '.taxes');
    $prJsonOLTaxes = json\retrieve($prJsonOrder, $prClave);
    $prClave = string\concatenate('line_items.', $prCont, '.meta_data');
    $prJsonOLMetaData = json\retrieve($prJsonOrder, $prClave);
    $prCont = $prCont + 1);

    And this worked fine so far.

    The problem I have arises when trying to retrieve the next level of nested information. First of all I need to know how large is the array (how many objects) Taxes. As I know $prJsonOLTaxes is an array, so I used this:

    $prJsonOLTaxesCount = array\length($prJsonOLTaxes);

    But it fails. It is the same schema I used before. I had the array $prJsonLineItems and asked for that. Now, when using with $prJsonOLTaxes, it doesn't work.

    I reviewed my code and seems OK to me. Does anyone see anything wrong in it? May it be a bug?

    The data of the json is:

    {
    "id": 79487,
    "parent_id": 0,
    "status": "completed",
    "billing": {
    "first_name": "Luis Eugenio Spanish Madrid",
    "address_1": "Calle San Fernando, 99"
    },
    "payment_method": "redsys",
    "transaction_id": "",
    "meta_data": [{
    "id": 2457983,
    "key": "__rp_order_date",
    "value": ""
    }, {
    "id": 2458018,
    "key": "_corruncy_code_redsys",
    "value": "978"
    }, {
    "id": 2458019,
    "key": "_redsys_secretsha256",
    "value": "/9999999999999999999ja"
    }],
    "line_items": [{
    "id": 85578,
    "name": "GXC - MALETA CABINA - Mensual",
    "taxes": [{
    "id": 1,
    "total": "0.6951",
    "subtotal": "0.6951"
    }],
    "meta_data": [{
    "id": 692980,
    "key": "plan",
    "value": "Mensual",
    "display_key": "Plan",
    "display_value": "Mensual"
    }],
    "price": 3.31,
    "parent_name": "GXC - MALETA CABINA"
    }, {
    "id": 85584,
    "name": "GXC - MALETA GRANDE - Mensual",
    "taxes": [{
    "id": 1,
    "total": "1.2159",
    "subtotal": "1.2159"
    }],
    "meta_data": [{
    "id": 693040,
    "key": "plan",
    "value": "Mensual",
    "display_key": "Plan",
    "display_value": "Mensual"
    }],
    "price": 5.79,
    "parent_name": "GXC - MALETA GRANDE"
    }],
    "tax_lines": [{
    "id": 85585,
    "rate_code": "ES-IVA 21%-1",
    "rate_id": 1
    }]
    }
  • telecastg
    Active Community Member
    • Jun 2018
    • 907

    #2
    Try this : https://stackoverflow.com/questions/...ensional-array

    Comment

    Working...