如何按阈值价格将样本名称名称为子数组密钥进行分组?
我有一系列产品样本,我试图按阈值价格进行分组,其余样本将由样品阵列访问。
这是我要实现的目标:
[
{
"threshold": 20.00,
"samples": [
{
...
}
]
},
{
"threshold": 100.00,
"samples": [
{
...
}
]
}
]
正在使用的fetchsamples数据(在将其输入为输入之前):例如
编辑:以及我 故意的。
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)
和我的PHP代码在哪里发生分组的位置。这是在循环中采取$ sortedproducts的尝试,并且使用$ sortedproducts来输出此修改后的数据:
$sortedProducts = array();
foreach($fetchSamples as $samplekey => $sample)
{
$sortedProducts[$sample["threshold"]][] = $sample;
}
但是,当我在foreach循环之后丢弃$ sortedproducts时,我已经获得了最接近该解决方案的方法: $ 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",
}
]
}
]
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",
}
]
}
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你在寻找吗?
示例代码
输出:
is that you looking for ?
Example code
Output: