I am new working with php, I am using a foreach loop to traverse an array of decoded objects. I would like to enter values to a new array for each iteration. This is a part of the code:
//example of array before decoding it [{"id":1,"quantity":12, other values...},{"id":2,"quantity":13,other values...}]
$data = json_decode($request->data);
$newArray = array();
foreach ($data as $key => $value){
$newArray[$key] = $value->{'id'} //[1,2,3....]
}
at the moment I am generating a one-dimensional array, what I need is to obtain an array of this type:
[
1 => ['quantity' => $other],
2 => ['quantity' => $other]
]
where 'other' would be another value that I get from my loop $value->{'other'}
. How can I generate this?
I'm not 100% confident, whether I really got your question ... but this is probably what you want:
foreach ($data as $key => $value) {
$newArray[$value->id] = ['quantity' => $value->quantity];
}
print_r($newArray);
after json_decode
you have objects of type stdClass
, so you can access the properties like shown above.
I am not sure why you need quantity
keys in one element arrays. Without this you can simply use array_column
function:
$items = json_decode($json);
// $items = json_decode($json, true); // For PHP < 7.0
$result = array_column($items, 'quantity', 'id');
But if you sure that quantity
keys are necessary you can map the result using array_map
:
$result = array_map(function ($quantity) {
return ['quantity' => $quantity];
}, $result);
Here is the demo.
Using a loop, you would do something like this:
$data = '[{"id":1,"quantity":12},{"id":2,"quantity":13}]';
$decoded = json_decode($data, $as_array = true); // decode as array, not object
$accumulator = [];
foreach($decoded as $index=>$values) {
$accumulator[$values['id']] = ['quantity' => $values['quantity']];
}
print_r($accumulator);
The same concept can be expressed more clearly in a functional manner with array_reduce
:
$data = '[{"id":1,"quantity":12},{"id":2,"quantity":13}]';
$decoded = json_decode($data, $as_array = true); // decode as array, not object
$reduced = array_reduce($decoded, function($carry, $entry){
$carry[$entry['id']] = ['quantity' => $entry['quantity']];
return $carry;
}, $initial_value = []);
print_r($reduced);
Both approaches would generate the same output:
Array
(
[1] => Array
(
[quantity] => 12
)
[2] => Array
(
[quantity] => 13
)
)
i think you are expecting like this,
$request = new stdClass();
$request->data = '[{"id":1,"quantity":12,"name":"item 1","code":"ITM-111"},{"id":2,"quantity":13,"name":"item 2","code":"ITM-222"}]';
$data = json_decode($request->data);
$newArray = array();
print_r($data);
example:
Array
(
[0] => stdClass Object
(
[id] => 1
[quantity] => 12
[name] => item 1
[code] => ITM-111
)
[1] => stdClass Object
(
[id] => 2
[quantity] => 13
[name] => item 2
[code] => ITM-222
)
)
$other = 'name'; // required key
foreach($data as $value){
$newArray[$value->id] = [
'quantity' => $value->quantity,
'name' => $value->{$other}
];
}
print_r($newArray);
output :
Array
(
[1] => Array
(
[quantity] => 12
[name] => item 1
)
[2] => Array
(
[quantity] => 13
[name] => item 2
)
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With