I have an array of product samples that I am trying to group by the threshold price, with the rest of the samples to be accessed by the samples array.
This is what I am trying to achieve:
[
{
"threshold": 20.00,
"samples": [
{
...
}
]
},
{
"threshold": 100.00,
"samples": [
{
...
}
]
}
]
EDIT: And the fetchSamples data I am working with (before it is inputted into foreach as the input): e.g. var_dump($sortedProducts)
This code is working ok as intended.
array (size=7)
0 =>
array (size=9)
"sampleid"=> "1234" (length=4)
"productid"=> "11111" (length=5)
"threshold"=> "20.00" (length=5)
"stock"=> "345" (length=4)
"product"=> "Beauty sample 1" (length=16)
2 =>
array (size=9)
"sampleid"=> "5678" (length=4)
"productid"=> "22222" (length=5)
"threshold"=> "20.00" (length=5)
"stock"=> "2449" (length=4)
"product"=> "Beauty sample 2" (length=16)
3 =>
array (size=9)
"sampleid"=> "999" (length=4)
"productid"=> "33333" (length=5)
"threshold"=> "100.00" (length=6)
"stock"=> "345" (length=4)
"product"=> "Beauty sample 3" (length=16)
And my PHP code where the where that grouping happens. This is the attempt where the $sortedProducts is taken in the loop and $sortedProducts is used to output this modified data:
$sortedProducts = array();
foreach($fetchSamples as $samplekey => $sample)
{
$sortedProducts[$sample["threshold"]][] = $sample;
}
However the closest I have gotten to this solution when I dump $sortedProducts after the foreach loop:(e.g. dump($sortedProducts)
);
Main issue is trying to get the results grouped by the threshold and trying to having it as the fields with an sub array of samples containing threshold identified by same key.
[
{
"20.00": [
{
"sampleid": "1234",
"productid": "111111",
"threshold": "20.00",
"stock": "345",
"product": "Beauty sample 1",
},
{
"sampleid": "5678",
"productid": "222222",
"threshold": "20.00",
"stocklevel": "2449",
"product": "Beauty sample 2",
}
],
"100.00": [
{
"sampleid": "9999",
"productid": "33333",
"threshold": "100.00",
"stock": "345",
"product": "Beauty sample 3",
}
]
}
]
CodePudding user response:
is that you looking for ?
$arrs = array(
'1' => array(
"sampleid"=> "1234",
"productid"=> "11111",
"threshold"=> "20.00",
"stock"=> "345",
"product"=> "Beauty sample 1"
),
'2' => array(
"sampleid"=> "5678",
"productid"=> "22222",
"threshold"=> "20.00",
"stock"=> "2449",
"product"=> "Beauty sample 2"
),
'3' => array(
"sampleid"=> "999",
"productid"=> "33333",
"threshold"=> "100.00",
"stock"=> "345",
"product"=> "Beauty sample 3"
)
);
$data = array();
foreach($arrs as $key => $value){
$data[$value['threshold']]['threshold'] = $value['threshold'];
$data[$value['threshold']]['samples'][] = array(
'productid' => $value['productid'],
'stock' => $value['stock']
);
}
print_r($data);
Output:
Array
(
[20.00] => Array
(
[threshold] => 20.00
[samples] => Array
(
[0] => Array
(
[productid] => 11111
[stock] => 345
)
[1] => Array
(
[productid] => 22222
[stock] => 2449
)
)
)
[100.00] => Array
(
[threshold] => 100.00
[samples] => Array
(
[0] => Array
(
[productid] => 33333
[stock] => 345
)
)
)
)