Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert values into an associative array within a loop

Tags:

arrays

php

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?

like image 836
FeRcHo Avatar asked Apr 27 '18 21:04

FeRcHo


4 Answers

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.

like image 56
nimmneun Avatar answered Nov 03 '22 01:11

nimmneun


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.

like image 24
sevavietl Avatar answered Nov 03 '22 01:11

sevavietl


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
        )

)
like image 22
jstur Avatar answered Nov 02 '22 23:11

jstur


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
        )

)
like image 41
Anfath Hifans Avatar answered Nov 03 '22 01:11

Anfath Hifans