从索引的PHP数组中检索相同的值

从索引的PHP数组中检索相同的值,php,arrays,Php,Arrays,我正在做一个发票到采购订单模块。比如说在一张发票里有3种产品 Product a => Vendor A Product b => Vendor B Product c => Vendor A 我有一个从angularJS传递到PHP的数组 该数组称为 products 下面的代码在我的PHP页面中 $products = isset($post_data['products']) ? $post_data['products']:array(); if(count(

我正在做一个发票到采购订单模块。比如说在一张发票里有3种产品

Product a => Vendor A 
Product b => Vendor B
Product c => Vendor A
我有一个从angularJS传递到PHP的数组

该数组称为

 products
下面的代码在我的PHP页面中

$products = isset($post_data['products']) ? $post_data['products']:array();

if(count($products) > 0){
    foreach($products as $v){
         //following code is working, but it will create 3 new PO with 
         //the respective products. So if let's say there are 3 products, with 2 items with same vendor, and 1 with another vendor, 
         //it will insert 3 new PO. What I wanted is to create 1 PO with 2 items(same vendor), 1 another PO with different vendor
         $param = array(
            //po data to insert
         );
         $this->_model->add($param);
         $insert_id = $this->db->insert_id();

         $products_array = array(
           //purchased product
         );
         $this->generic_model->add($products_array);        
    }
}
下面是从数组传递到PHP页面的var_dump结果(从var_dump($v))

从var_转储结果中可以看到,有两个相同的供应商,它们是供应商2

所以我的问题是,如何在PHP中循环angularJS中的数组,然后基于同一供应商对其进行“分组”?在前面的示例中,如何使用以下数据将数据插入数据库

2 tables in my db, purchase_order and purchased_product
New PO with 2 items inside from Vendor 2, (1 purchase order row with 2 rows in purchased_product)
Another new PO with 1 item from Vendor 1, (1 purchase order row with 1 row in purchased_product)
已经处理了很长一段时间,仍然不知道如何进行数组循环


谢谢……)

您的代码示例没有说明从何处获得
PO 1
PO 2
密钥,因此我无法将其包含在解决方案中。请随时更新您的问题,以澄清这一点

要采用如上所述的JSON结构并将其转换为指定的“分组”字符串值数组,可以尝试以下操作:

<?php

// firstly, just some boilerplate for this example

$json = <<<JSON
[
    {
       "id": "35",
       "vendor": "Vendor 1",
       "vendor_id": "5"
    },
    {
       "id": "33",
       "vendor": "Vendor 2",
       "vendor_id": "7"
    },
    {
       "id": "34",
       "vendor": "Vendor 2",
       "vendor_id": "7"
    }
]
JSON;

$data = json_decode($json, true); 

// now we can start extracting and grouping data

// assuming you are using >= 5.5 here
// `array_column()` extracts the values of all 
// sub-arrays with the `vendor` key into an array
$vendors = array_column($data, 'vendor');

// `array_count_values()` aggregates a count
// for each instance of each string in the array
// with the vendor as the key and a count as its corresponding value
$vendorCounts = array_count_values($vendors);

// map over the keys and values, creating an array
// of strings in the specified format
$vendorGroups = array_map(function ($vendor, $count) {
    return sprintf('%d items from %s', $count, $vendor);
}, array_keys($vendorCounts), $vendorCounts);

print_r($vendorGroups);
文件参考:


希望这有帮助:)

您可以在JS(客户端)端对供应商进行分组。此外,展示如何查看将数据插入数据库的//代码从何处获得
PO 1
PO 2
?更新的说明和代码:)。如果我不能很好地解释,请原谅我,我是一名初级程序员,仍在研究如何编写代码..先生,更新了问题并删除了JSON。我用($v)中的var_dump替换它,希望它能更清楚地理解
<?php

// firstly, just some boilerplate for this example

$json = <<<JSON
[
    {
       "id": "35",
       "vendor": "Vendor 1",
       "vendor_id": "5"
    },
    {
       "id": "33",
       "vendor": "Vendor 2",
       "vendor_id": "7"
    },
    {
       "id": "34",
       "vendor": "Vendor 2",
       "vendor_id": "7"
    }
]
JSON;

$data = json_decode($json, true); 

// now we can start extracting and grouping data

// assuming you are using >= 5.5 here
// `array_column()` extracts the values of all 
// sub-arrays with the `vendor` key into an array
$vendors = array_column($data, 'vendor');

// `array_count_values()` aggregates a count
// for each instance of each string in the array
// with the vendor as the key and a count as its corresponding value
$vendorCounts = array_count_values($vendors);

// map over the keys and values, creating an array
// of strings in the specified format
$vendorGroups = array_map(function ($vendor, $count) {
    return sprintf('%d items from %s', $count, $vendor);
}, array_keys($vendorCounts), $vendorCounts);

print_r($vendorGroups);
Array
(
    [0] => 1 items from Vendor 1
    [1] => 2 items from Vendor 2
)