Array
(
[0] => stdClass Object
(
[price_with_discount] => 2025.00
[sales_date] => 2014-09-02
[sales_type] => 1
[part_id] => 6
[part_number] => SN14FW-KU027OR03
[part_rate] => 2025.00
[sales_status] => 2
[product_name] => Ladies Fashion Wear
[model_number] => Bamboo Shawl Natural
)
[1] => stdClass Object
(
[price_with_discount] => 1700.00
[sales_date] => 2014-09-02
[sales_type] => 1
[part_id] => 23
[part_number] => SN14FW-KU015PL01
[part_rate] => 1700.00
[sales_status] => 2
[product_name] => Ladies Fashion Wear
[model_number] => Allo Cotton Shawl
)
[2] => stdClass Object
(
[price_with_discount] => 1850.00
[sales_date] => 2014-09-02
[sales_type] => 1
[part_id] => 31
[part_number] => SN14FW-KU022GR06
[part_rate] => 1850.00
[sales_status] => 2
[product_name] => Ladies Fashion Wear
[model_number] => Bamboo Stole
)
[3] => stdClass Object
(
[price_with_discount] => 1840.00
[sales_date] => 2014-09-03
[sales_type] => 1
[part_id] => 49
[part_number] => SN13FW-KU113BR02
[part_rate] => 1840.00
[sales_status] => 2
[product_name] => Accessories
[model_number] => Bangle
)
)
我想将给定数组格式化为json,如下所示,我希望它们按照数组中给出的product_name字段进行分类。我做过尝试但是我没有接近那个结果。 我试过我的代码如下:
foreach ($reports as $report) {
$product_name = str_replace(' ', '_', trim($report->product_name));
$r[$product_name][] = array(
'part_number' => $report->part_number,
'part_rate' => $report->part_rate,
);
}
$reports = json_encode($r,JSON_PRETTY_PRINT);
但我想如下:
{
"Data":[
{
"ProductName":"Ladies_Fashion_Wear",
"ProuuctItems": [
{
"part_number": "SN14FW-KU027OR03",
"part_rate": "2025.00"
},
{
"part_number": "SN14FW-KU015PL01",
"part_rate": "1700.00"
},
]
},
{
"ProductName":"Accessories",
"ProuuctItems": [
{
"part_number": "SN14FW-KU030GR02",
"part_rate": "2040.00"
}
],
}
]
}
答案 0 :(得分:1)
重要的是,你只需要使用$report->product_name
作为你的密钥,并在设置新数组时再次使其成为多维数据,然后最后编码它。例如:
$r['Data'] = array();
foreach($reports as $report) {
if(!isset($r['Data'][$report->product_name])) {
// initial insertion
$r['Data'][$report->product_name] = array(
'ProductName' => $report->product_name,
'ProductItems' => array(),
);
}
// push it inside once created
$r['Data'][$report->product_name]['ProductItems'][] = array(
'part_number' => $report->part_number,
'part_rate' => $report->part_rate
);
}
// simple reindex
$r['Data'] = array_values($r['Data']);
echo '<pre>';
$final = json_encode($r, JSON_PRETTY_PRINT);
echo $final;
答案 1 :(得分:0)
这是非常不言自明的
编辑: 在编码为JSON之前,您可以合并阵列。您需要使用所需的值作为密钥。
代码示例:
$result = array();
foreach($source as $item) {
$product_name= key($item);
$other_attributes = current($item); // repeat for all other attributes
if(!isset($result[$product_name])) {
$result[$product_name] = array();
}
$result[$product_name][] = $other_attributes ;
}
答案 2 :(得分:0)
json_encode 会将任何给定值(资源除外)转换为JSON值:
json_encode(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5));
将返回:
{"a":1,"b":2,"c":3,"d":4,"e":5}
在您的情况下,您希望转换StdClass类型的对象数组
你可能想考虑让你的类实现 JsonSerializable (php 5.4向上) 并创建一个方法 jsonSerialize ,它将以所需的数组格式输出每个对象。
示例:
public function jsonSerialize() {
return array('ProductName'=>'myName','productItems'=>array('part_number'=>'123456'))
}
更多信息:http://php.net/manual/en/jsonserializable.jsonserialize.php