douli4852 2015-11-09 16:31
浏览 23

通过标题在php数组中列出数据

I have a rather complex set of data which is available in an array, specifically different options for a single product.

In this example, the product is a cable and is available in different lengths and different colours, so you can get a 1m yellow cable but also a 1m green cable and so on. I want to be able to list these options in accordions but group them by length, so if the customer wanted a 2m cable in yellow, they would click the "2m" accordion and select Yellow from inside the accordion.

Here is an example of one record in the array:

[3775] => Array
        (
            [id] => 3775
            [product_id] => 4928
            [active] => 0
            [var] => 37:63
            [price] => 0.0000
            [opts] => Array
                (
                    [37] => 0.3m
                    [38] => 0.5m
                    [39] => 1.0m
                    [40] => 1.5m
                    [41] => 2.0m
                    [42] => 3.0m
                    [43] => 4.0m
                    [44] => 5.0m
                    [45] => 6.0m
                    [46] => 7.0m
                    [47] => 8.0m
                    [48] => 10.0m
                    [49] => 15.0m
                    [50] => 20.0m
                    [51] => 25.0m
                    [52] => 30.0m
                    [53] => 50.0m
                    [59] => Grey
                    [63] => Red
                    [58] => Green
                    [55] => Blue
                    [65] => Yellow
                    [54] => Black
                    [62] => Purple
                    [64] => White
                    [60] => Orange
                    [61] => Pink
                    [56] => Brown
                )
        )

The [var] key represents the two IDs of the [opts] array, so 37:63 pertains to 0.3m Red.

The rest of the array looks exactly the same (in the sense that the opts array remains the same) but the var, combi and ID will change.

Is there an easy way to group these options by length and then list each available colour underneath?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • doudou130216 2015-11-09 18:14
    关注

    Use array_filter on the options array

    The callback function should take a string as an argument and return true if the string starts with a number. You can use preg_match for that

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值