当我使用find('all')使用我的cakephp查询从数据库中获取数据时,它返回一个数组。好的,然后结果数组不舒服,我喜欢在页面上显示这些数据。意味着它生成多行。我在谷歌上查了但没有找到回复。
注意:数组过滤器应该符合Array ['DispensaryInventory'] ['product_id']
我的数组是:
Array
(
[0] => Array
(
[DispensaryInventory] => Array
(
[id] => 15
[dispensary_id] => 1
[product_id] => 9
[quantity] =>
[price] =>
)
[ProductOption] => Array
(
[value] => 2
[unit] => oz
)
[Product] => Array
(
[id] => 9
[name] => Bajaj
)
)
[1] => Array
(
[DispensaryInventory] => Array
(
[id] => 11
[dispensary_id] => 1
[product_id] => 9
[quantity] => 17
[price] => 12.00
)
[ProductOption] => Array
(
[value] => 1/8
[unit] => oz
)
[Product] => Array
(
[id] => 9
[name] => Bajaj
)
)
[2] => Array
(
[DispensaryInventory] => Array
(
[id] => 12
[dispensary_id] => 1
[product_id] => 9
[quantity] =>
[price] =>
)
[ProductOption] => Array
(
[value] => 1/4
[unit] => oz
)
[Product] => Array
(
[id] => 9
[name] => Bajaj
)
)
[3] => Array
(
[DispensaryInventory] => Array
(
[id] => 13
[dispensary_id] => 1
[product_id] => 9
[quantity] => 5
[price] => 123.00
)
[ProductOption] => Array
(
[value] => 1/2
[unit] => oz
)
[Product] => Array
(
[id] => 9
[name] => Bajaj
)
)
[4] => Array
(
[DispensaryInventory] => Array
(
[id] => 14
[dispensary_id] => 1
[product_id] => 9
[quantity] =>
[price] =>
)
[ProductOption] => Array
(
[value] => 1
[unit] => oz
)
[Product] => Array
(
[id] => 9
[name] => Bajaj
)
)
[5] => Array
(
[DispensaryInventory] => Array
(
[id] => 5
[dispensary_id] => 1
[product_id] => 8
[quantity] =>
[price] =>
)
[ProductOption] => Array
(
[value] => 2
[unit] => oz
)
[Product] => Array
(
[id] => 8
[name] => BMW
)
)
[6] => Array
(
[DispensaryInventory] => Array
(
[id] => 1
[dispensary_id] => 1
[product_id] => 8
[quantity] => 2
[price] => 123.00
)
[ProductOption] => Array
(
[value] => 1/8
[unit] => oz
)
[Product] => Array
(
[id] => 8
[name] => BMW
)
)
[7] => Array
(
[DispensaryInventory] => Array
(
[id] => 2
[dispensary_id] => 1
[product_id] => 8
[quantity] =>
[price] =>
)
[ProductOption] => Array
(
[value] => 1/4
[unit] => oz
)
[Product] => Array
(
[id] => 8
[name] => BMW
)
)
[8] => Array
(
[DispensaryInventory] => Array
(
[id] => 3
[dispensary_id] => 1
[product_id] => 8
[quantity] => 23
[price] => 222.00
)
[ProductOption] => Array
(
[value] => 1/2
[unit] => oz
)
[Product] => Array
(
[id] => 8
[name] => BMW
)
)
[9] => Array
(
[DispensaryInventory] => Array
(
[id] => 4
[dispensary_id] => 1
[product_id] => 8
[quantity] => 12
[price] => 232.00
)
[ProductOption] => Array
(
[value] => 1
[unit] => oz
)
[Product] => Array
(
[id] => 8
[name] => BMW
)
)
[10] => Array
(
[DispensaryInventory] => Array
(
[id] => 6
[dispensary_id] => 1
[product_id] => 3
[quantity] => 3
[price] => 21.00
)
[ProductOption] => Array
(
[value] => 1/8
[unit] => oz
)
[Product] => Array
(
[id] => 3
[name] => Yash product
)
)
[11] => Array
(
[DispensaryInventory] => Array
(
[id] => 7
[dispensary_id] => 1
[product_id] => 3
[quantity] => 12
[price] => 56.00
)
[ProductOption] => Array
(
[value] => 1/4
[unit] => oz
)
[Product] => Array
(
[id] => 3
[name] => Yash product
)
)
[12] => Array
(
[DispensaryInventory] => Array
(
[id] => 8
[dispensary_id] => 1
[product_id] => 3
[quantity] =>
[price] =>
)
[ProductOption] => Array
(
[value] => 1/2
[unit] => oz
)
[Product] => Array
(
[id] => 3
[name] => Yash product
)
)
[13] => Array
(
[DispensaryInventory] => Array
(
[id] => 9
[dispensary_id] => 1
[product_id] => 3
[quantity] => 15
[price] => 354.00
)
[ProductOption] => Array
(
[value] => 1
[unit] => oz
)
[Product] => Array
(
[id] => 3
[name] => Yash product
)
)
[14] => Array
(
[DispensaryInventory] => Array
(
[id] => 10
[dispensary_id] => 1
[product_id] => 3
[quantity] =>
[price] =>
)
[ProductOption] => Array
(
[value] => 2
[unit] => oz
)
[Product] => Array
(
[id] => 3
[name] => Yash product
)
)
)
我希望看起来像这个数组
Array
(
[0] => Array
(
[DispensaryInventory] => Array
(
[0] => Array
(
[id] => 15
[dispensary_id] => 1
[product_id] => 9
[quantity] =>
[price] =>
)
[1] => Array
(
[id] => 11
[dispensary_id] => 1
[product_id] => 9
[quantity] => 17
[price] => 12.00
)
[2] => Array
(
[id] => 12
[dispensary_id] => 1
[product_id] => 9
[quantity] =>
[price] =>
)
[3] => Array
(
[id] => 13
[dispensary_id] => 1
[product_id] => 9
[quantity] => 5
[price] => 123.00
)
[4] => Array
(
[id] => 14
[dispensary_id] => 1
[product_id] => 9
[quantity] =>
[price] =>
)
)
[ProductOption] => Array
(
[0] => Array
(
[value] => 2
[unit] => oz
)
[1] => Array
(
[value] => 1/8
[unit] => oz
)
[2] => Array
(
[value] => 1/4
[unit] => oz
)
[3] => Array
(
[value] => 1/2
[unit] => oz
)
[4] => Array
(
[value] => 1
[unit] => oz
)
)
[Product] => Array
(
[id] => 9
[name] => Bajaj
)
)
[1] => Array
(
[DispensaryInventory] => Array
(
[0] => Array
(
[id] => 5
[dispensary_id] => 1
[product_id] => 8
[quantity] =>
[price] =>
)
[1] => Array
(
[id] => 1
[dispensary_id] => 1
[product_id] => 8
[quantity] => 2
[price] => 123.00
)
[2] => Array
(
[id] => 2
[dispensary_id] => 1
[product_id] => 8
[quantity] =>
[price] =>
)
[3] => Array
(
[id] => 3
[dispensary_id] => 1
[product_id] => 8
[quantity] => 23
[price] => 222.00
)
[4] => Array
(
[id] => 4
[dispensary_id] => 1
[product_id] => 8
[quantity] => 12
[price] => 232.00
)
)
[ProductOption] => Array
(
[0] => Array
(
[value] => 2
[unit] => oz
)
[1] => Array
(
[value] => 1/8
[unit] => oz
)
[2] => Array
(
[value] => 1/4
[unit] => oz
)
[3] => Array
(
[value] => 1/2
[unit] => oz
)
[4] => Array
(
[value] => 1
[unit] => oz
)
)
[Product] => Array
(
[id] => 8
[name] => BMW
)
)
[2] => Array
(
[DispensaryInventory] => Array
(
[0] => Array
(
[id] => 6
[dispensary_id] => 1
[product_id] => 3
[quantity] => 3
[price] => 21.00
)
[1] => Array
(
[id] => 7
[dispensary_id] => 1
[product_id] => 3
[quantity] => 12
[price] => 56.00
)
[2] => Array
(
[id] => 2
[id] => 8
[dispensary_id] => 1
[product_id] => 3
[quantity] =>
[price] =>
)
[3] => Array
(
[id] => 9
[dispensary_id] => 1
[product_id] => 3
[quantity] => 15
[price] => 354.00
)
[4] => Array
(
[id] => 10
[dispensary_id] => 1
[product_id] => 3
[quantity] =>
[price] =>
)
)
[ProductOption] => Array
(
[0] => Array
(
[value] => 2
[unit] => oz
)
[1] => Array
(
[value] => 1/8
[unit] => oz
)
[2] => Array
(
[value] => 1/4
[unit] => oz
)
[3] => Array
(
[value] => 1/2
[unit] => oz
)
[4] => Array
(
[value] => 1
[unit] => oz
)
)
[Product] => Array
(
[id] => 3
[name] => Yash product
)
)
更新:
我试过了。
foreach($dispensary_inventory_data as $k1=>$a1){
foreach($dispensary_inventory_data as $k2=>$a2){
if($k1 < $k2 && $a1["DispensaryInventory"]["product_id"] == $a2["DispensaryInventory"]["product_id"]){
$dispensary_inventory_data[$k1]["ProductOption"][] = $a2["ProductOption"];
$dispensary_inventory_data[$k1]["Product"][] = $a2["Product"];
if(isset($dispensary_inventory_data[$k1]["Product"]["id"])){
$dispensary_inventory_data[$k1]["Product"][] = array(
"id"=>$dispensary_inventory_data[$k1]["Product"]["id"],
"name"=>$dispensary_inventory_data[$k1]["Product"]["name"],
"quantity"=>$dispensary_inventory_data[$k1]["Product"]["quantity"]
);
$dispensary_inventory_data[$k1]["ProductOption"][] = array(
"id"=>$dispensary_inventory_data[$k1]["ProductOption"]["id"],
"value"=>$dispensary_inventory_data[$k1]["ProductOption"]["value"],
"unit"=>$dispensary_inventory_data[$k1]["ProductOption"]["unit"]
);
unset($dispensary_inventory_data[$k1]["Product"]["id"]);
unset($dispensary_inventory_data[$k1]["Product"]["name"]);
unset($dispensary_inventory_data[$k1]["Product"]["city"]);
unset($dispensary_inventory_data[$k1]["Product"]["quantity"]);
unset($dispensary_inventory_data[$k1]["ProductOption"]["id"]);
unset($dispensary_inventory_data[$k1]["ProductOption"]["value"]);
unset($dispensary_inventory_data[$k1]["ProductOption"]["unit"]);
}
unset($dispensary_inventory_data[$k2]);
}
}
}
答案 0 :(得分:1)
您可以使用php array_column函数并将数组重新组织为具有所需结构和最小工作量的新数组:
$result = array();
$result['DispensaryInventory'] = array_column($data, 'DispensaryInventory');
$result['ProductOption'] = array_column($data, 'ProductOption');
$result['Product'] = array_column($data, 'Product');
答案 1 :(得分:1)
更新
使用此功能将数组转换为您需要的格式:
function my_format($data)
{
$data2 = array();
foreach($data as $val)
{
$keys = array_keys($val);
// you can also use $val['DispensaryInventory']['product_id'] here,
// but i take it these refer to the same thing
$product_id = $val['Product']['id'];
if (!isset($data2[$product_id])) $data2[$product_id] = array();
foreach($keys as $key)
{
if ( 'Product' === $key ) $data2[$product_id][$key] = $val[$key];
elseif ( !isset($data2[$product_id][$key]) ) $data2[$product_id][$key] = array($val[$key]);
else $data2[$product_id][$key][] = $val[$key];
}
}
return array_values($data2); // you can also return the $data2 here
}
像这样使用:
$new_data = my_format($data);
(预期)输出(未测试)
Array
(
[0] => Array
(
[DispensaryInventory] => Array
(
[0] => Array
(
[id] => 15
[dispensary_id] => 1
[product_id] => 9
[quantity] =>
[price] =>
)
[1] => Array
(
[id] => 11
[dispensary_id] => 1
[product_id] => 9
[quantity] => 17
[price] => 12.00
)
[2] => Array
(
[id] => 12
[dispensary_id] => 1
[product_id] => 9
[quantity] =>
[price] =>
)
[3] => Array
(
[id] => 13
[dispensary_id] => 1
[product_id] => 9
[quantity] => 5
[price] => 123.00
)
[4] => Array
(
[id] => 14
[dispensary_id] => 1
[product_id] => 9
[quantity] =>
[price] =>
)
)
[ProductOption] => Array
(
[0] => Array
(
[value] => 2
[unit] => oz
)
[1] => Array
(
[value] => 1/8
[unit] => oz
)
[2] => Array
(
[value] => 1/4
[unit] => oz
)
[3] => Array
(
[value] => 1/2
[unit] => oz
)
[4] => Array
(
[value] => 1
[unit] => oz
)
)
[Product] => Array
(
[id] => 9
[name] => Bajaj
)
)
[1] => Array
(
[DispensaryInventory] => Array
(
[0] => Array
(
[id] => 5
[dispensary_id] => 1
[product_id] => 8
[quantity] =>
[price] =>
)
[1] => Array
(
[id] => 1
[dispensary_id] => 1
[product_id] => 8
[quantity] => 2
[price] => 123.00
)
[2] => Array
(
[id] => 2
[dispensary_id] => 1
[product_id] => 8
[quantity] =>
[price] =>
)
[3] => Array
(
[id] => 3
[dispensary_id] => 1
[product_id] => 8
[quantity] => 23
[price] => 222.00
)
[4] => Array
(
[id] => 4
[dispensary_id] => 1
[product_id] => 8
[quantity] => 12
[price] => 232.00
)
)
[ProductOption] => Array
(
[0] => Array
(
[value] => 2
[unit] => oz
)
[1] => Array
(
[value] => 1/8
[unit] => oz
)
[2] => Array
(
[value] => 1/4
[unit] => oz
)
[3] => Array
(
[value] => 1/2
[unit] => oz
)
[4] => Array
(
[value] => 1
[unit] => oz
)
)
[Product] => Array
(
[id] => 8
[name] => BMW
)
)
[2] => Array
(
[DispensaryInventory] => Array
(
[0] => Array
(
[id] => 6
[dispensary_id] => 1
[product_id] => 3
[quantity] => 3
[price] => 21.00
)
[1] => Array
(
[id] => 7
[dispensary_id] => 1
[product_id] => 3
[quantity] => 12
[price] => 56.00
)
[2] => Array
(
[id] => 2
[id] => 8
[dispensary_id] => 1
[product_id] => 3
[quantity] =>
[price] =>
)
[3] => Array
(
[id] => 9
[dispensary_id] => 1
[product_id] => 3
[quantity] => 15
[price] => 354.00
)
[4] => Array
(
[id] => 10
[dispensary_id] => 1
[product_id] => 3
[quantity] =>
[price] =>
)
)
[ProductOption] => Array
(
[0] => Array
(
[value] => 2
[unit] => oz
)
[1] => Array
(
[value] => 1/8
[unit] => oz
)
[2] => Array
(
[value] => 1/4
[unit] => oz
)
[3] => Array
(
[value] => 1/2
[unit] => oz
)
[4] => Array
(
[value] => 1
[unit] => oz
)
)
[Product] => Array
(
[id] => 3
[name] => Yash product
)
)