djwd djwd - 1 month ago 5
PHP Question

Remove one level dept array and associate value to its parent key

Going crazy about that, please how can I turn this array:

Array
(
[par] => XXX
[price_item] => 100
[value] => 130
[qnt] => Array
(
[130] => 99
)

)


into this:

Array
(
[par] => XXX
[price_item] => 100
[value] => 130
[qnt] => 99

)


In a foreach loop?

Notes:
I can't specify the key [130] as it could be a different number, but the array levels structure is always the same as well as the [qnt] key, thank you.

Answer

You just need to overwrite your array by getting the first element from the given array:

//$yourArray
if(is_array($yourArray['qnt'])){//check if array, better safe
    $values = array_values($yourArray['qnt']); //get values as array
    $yourArray['qnt'] = $values[0];//get first and rewrite yout initial array
}
Comments