如何按阈值价格将样本名称名称为子数组密钥进行分组?

发布于 2025-02-12 00:59:48 字数 2418 浏览 4 评论 0原文

我有一系列产品样本,我试图按阈值价格进行分组,其余样本将由样品阵列访问。

这是我要实现的目标:

[
  {
    "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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白日梦 2025-02-19 00:59:48

你在寻找吗?

示例代码

$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);

输出:

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
                    )

            )

    )

)

is that you looking for ?

Example code

$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
                    )

            )

    )

)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文