简体   繁体   English

php 产品类别数组

[英]php array of categories from products

I have an array我有一个数组

$catname =  Array
(
[0] => Array
    (
        [id] => 39
        [name] =>  Football
        [votes] => 3
        [ndate] => 01-2021
    )

[1] => Array
    (
        [id] => 38
        [name] => Rugby 
        [votes] => 2
        [ndate] => 01-2021
    )

[2] => Array
    (
        [id] => 39
        [name] =>  Football
        [votes] => 1
        [ndate] => 02-2021
    )

[3] => Array
    (
        [id] => 38
        [name] => Rugby 
        [votes] => 1
        [ndate] => 02-2021
    )
 )

I want to group them by name, so the desired JSON output wants t to be like this我想按名称对它们进行分组,因此所需的 JSON output 希望 t 像这样

array =[{
  name: football,
  id:39,
  data: [
   {date: 01-2021, votes: 3},
  {date: 02-2021, votes: 2},
  {date: 03-2021, votes: 1},
  ]
},
name: Rugby,
  id:38,
  data: [
   {date: 01-2021, votes: 2},
  {date: 02-2021, votes: 2},
  {date: 03-2021, votes: 0},
  ]
 }
]

So far I did this到目前为止,我这样做了

$itemsByCategory = array();
foreach($catname as $item){
    $optionname = $item['name'];
    $vote = $item['votes'];
    $dates = $item['ndate'];
    if (!array_key_exists($optionname, $itemsByCategory))
     $itemsByCategory[$item['name']][] = array(
    'vote' =>$vote,
    'date' =>$dates
  );
}

But doesn't produce the desired result.但不会产生预期的结果。 I also tried with if (array_key_exists but still not been able to think, what's wrong with thsi code. Any suggestions?我也试过 if (array_key_exists 但仍然无法思考,这个代码有什么问题。有什么建议吗?

You forgot about 2nd level of the array.您忘记了阵列的第二级。 Try the following:尝试以下操作:

$itemsByCategory = [];
foreach($catname as $item) {
    $optionname = $item['name'];
    $vote = $item['votes'];
    $dates = $item['ndate'];
    if (!array_key_exists($optionname, $itemsByCategory)) {
        $itemsByCategory[$optionname][] = [
            'name' => $optionname,
            'id' => $item['id'],
            'data' => [],
        ];
    }
    $itemsByCategory[$optionname]['data'][] = [
        'vote' => $vote,
        'date' => $dates,
    ];
}
<?php

$catname = [
    [
        'id' => 39,
        'name' => 'Football',
        'votes' => 3,
        'ndate' => '01-2021',
    ],
    [
        'id' => 38,
        'name' => 'Rugby',
        'votes' => 2,
        'ndate' => '01-2021',
    ],
    [
        'id' => 39,
        'name' => 'Football',
        'votes' => 1,
        'ndate' => '02-2021',
    ],
    [
        'id' => 38,
        'name' => 'Rugby',
        'votes' => 1,
        'ndate' => '02-2021',
    ],
];

$grouped = [];
foreach($catname as $cat) {
    $id = $cat['id'];
    $name = $cat['name'];
    $votes = $cat['votes'];
    $ndate = $cat['ndate'];

    if(!isset($grouped[$id])) {
        $grouped[$id] = [
            'name' => $name,
            'id' => $id,
            'data' => []
        ];
    }

    $grouped[$id]['data'][] = [
        'votes' => $votes,
        'date' => $ndate,
    ];
}

echo json_encode(array_values($grouped));

-> ->

[
    {
        "name": "Football",
        "id": 39,
        "data": [
            {
                "votes": 3,
                "date": "01-2021"
            },
            {
                "votes": 1,
                "date": "02-2021"
            }
        ]
    },
    {
        "name": "Rugby",
        "id": 38,
        "data": [
            {
                "votes": 2,
                "date": "01-2021"
            },
            {
                "votes": 1,
                "date": "02-2021"
            }
        ]
    }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM