1

I´m trying to create this structure to send data to API

$metaDataList = [
            [        
                "key" => "item_description0",
                "value" => "DCALUXE",
            ]
        ];

i need this structure.

I´m trying to do this:

$items_selected = [];
        for($i=0; $i<count($precontractData->items);$i++){
            $items_selected["item_description$i"] = $precontractData->items[$i]->name;
            $items_selected["item_quantity$i"] = intval($precontractData->items[$i]->cuantity);
        }
        array_push($metaDataList, $items_selected);

but my result it´s not equal to my needed structure. I try with merge array, i try with array_map, but i don´t know how i can to do one array with this structure. in key "key" should be "item_description$i" and in value should be $precontractData->items[$i]->name

Thanks for readme and sorry for my bad english

4

1 Answer 1

2

You are constructing your array wrongly. There is no keys named item_description_0, just key and value. So you need to adjust your loop:

$metaDataList = [];

foreach ($precontractData->items as $i => $item) {
    $metaDataList[] = [
        'key' => "item_description{$i}",
        'value' => $item->name,
    ];
    $metaDataList[] = [
        'key' => "item_quantity{$i}",
        'value' => intval($item->cuantity),
    ];
}

Final $metaDataList will be similar to

[
    [
        'key' => 'item_description0',
        'value' => 'Name',
    ],
    [
        'key' => 'item_quantity0',
        'value' => 100,
    ],
    [
        'key' => 'item_description1',
        'value' => 'Name 1',
    ],
    [
        'key' => 'item_quantity1',
        'value' => 101,
    ],
];

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.