简体   繁体   English

如何创建多维关联数组

[英]How to create multi-dimensional associative array

I have this example table data, it actually has ids, prices, more stuff, but i think i can ask with this data, im using PHP as my language: 我有这个示例表数据,它实际上具有ID,价格,更多内容,但是我想我可以使用此数据进行查询,即时通讯使用PHP作为我的语言:

id   category  product       complement_type   option
 1   Pizza     Large Pizza   Bread             Brown bread
 2   Pizza     Small Pizza   Bread             White bread
 3   Pizza     Small Pizza   Ingredients       Olives
 4   Salads    Green Salad   Extras            Bacon
 5   Salads    Cesars Salad  Extras            Lettuce

i already have the query for this table, 我已经有查询此表,

now i want to convert ir to an array of this type 现在我想将ir转换为这种类型的数组

array(
 [Pizza] => array(
             [Large Pizza] => array(
                                [Bread] => Brown Bread
                              )
            ),
            array(
             [Small Pizza] => array(
                                [Bread]        => White Bread
                                [Ingredients]  => Olives
                              ),
            )
 ),
 [Salads] => array(
             [Green Salad] => array(
                               [Extras]         => Bacon
                              )
             [Cesar Salad] => array(
                               [Extras]         => Lettuce
                              )
 )

that array is what i am trying to achieve, but i just cant get it right 该数组是我想要实现的,但是我无法正确完成

i know i have to loop it with a for loop, maybe add a while loop to get the changes in the categories, but i just dont seem to find the way to do it, any ideas on how to achieve this? 我知道我必须使用for循环来循环它,也许添加一个while循环来获取类别中的更改,但是我只是似乎没有找到实现它的方法,关于如何实现这一点的任何想法?

i appreciate any idea 我很欣赏任何想法

Again, im using PHP 再次,即时通讯使用PHP

thanks 谢谢

Edit: heres the link to the data im getting, its a little different, but its the same principle 编辑:这是指向数据即时获取的链接,虽然有点不同,但原理相同

https://dobleslash.com/TakeEatEasy/API/getRest?id=1 https://dobleslash.com/TakeEatEasy/API/getRest?id=1

First of all: your original array — as posted is comment — if an array of objects: this preliminar note is to understand the syntax -> , otherwise obscure simply reading your main question. 首先:您的原始数组(已发布,是注释),如果是对象数组:此初步注意事项是理解语法-> ,否则仅阅读您的主要问题就难以理解。

Assuming your table results are stored in array $array , you can try in this way: 假设您的表结果存储在数组$array ,则可以按以下方式尝试:

$result = array();
foreach( $array as $row )
{
    if( !isset( $result[ $row->category ] ) ) 
    {
        $result[ $row->category ] = array();
    }
    if( !isset( $result[ $row->category ][ $row->product ] ) )
    {
        $result[ $row->category ][ $row->product ] = array();
    }
    $result[ $row->category ][ $row->product ][ $row->complement_type ] = $row->option;
}

At beginning, I init an empty array. 首先,我初始化一个空数组。 Then — through a foreach loop, I process each element of $array and — if primary key (the product) doesn't exist in the result array, I create it as empty array; 然后-通过一个foreach循环,我处理$array每个元素,并且-如果结果数组中不存在主键(产品),则将其创建为空数组; then I perform same action for category subkey (Large Pizza, etc). 然后我对类别子项(大披萨等)执行相同的操作。 At last, I add the complement type. 最后,我添加补码类型。

eval.in demo 评估演示

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

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