drqvr26084 2009-12-19 10:22 采纳率: 100%
浏览 58

PHP递归函数显示数组值的有效组合

I have a PHP array that looks like this:

Array
(
[340] => Array
    (
        [1] => 1
        [2] => 18
        [3] => 23
    )

[341] => Array
    (
        [1] => 1
        [2] => 17
        [3] => 23
    )

[342] => Array
    (
        [1] => 1
        [2] => 16
        [3] => 23
    )

[343] => Array
)

The array is actually longer and contains about 40 elements. There will be other arrays that contain a different number of child elements. The array is formatted as

productID => array (
    $attributeID => attributeValueID,
    $attributeID => attributeValueID,
    $attributeID => attributeValueID
)

What I need is an array that shows the valid values for all other attributes. The output array might looks something like

Array
(
18 => array(
    1 => array(
        11, 12, 13, 14, 15
    )
    2 => array(
        19, 20, 21, 22
    )
)
19 => array(
    1 => array(
        11, 13, 14, 15
    )
    2 => array(
        21, 22
    )
)

The format of this array is

attributeValueID => array(
    attributeID => attributeValues,
    attributeID => attributeValues
)

I've been working with some recursion functions but I've only been able to get a list of every possible combination between all the values which isn't what I need. Any help would be greatly appreciated.

Clarification on what I mean by "valid" values: What the 1, 2, and 3 values represent here are color, size, and length. The ultimate goal here is to create a series of javascript arrays I'll use to update options on the page when a user selects values. For example when they select the color black I need to update the sizes and lengths on the page to reflect only sizes and lengths available in black. I can create the arrays manually but this is not a good solution.

  • 写回答

3条回答 默认 最新

  • douzhicai7148 2009-12-19 11:18
    关注

    Assuming the output should actually be:

    attributeValueID => array(
        attributeID => productID,
        attributeID => productID
    )
    

    (If not where do the attribute values come from?)

    Recursion would not be needed, just something along these lines to re-jig the array:

    $products = array(   340 => array(
                                        1 => 1,
                                        2 => 18,
                                        3 => 23
                                        ),
    
                            341 => array(
                                        1 => 1,
                                        2 => 17,
                                        3 => 23
                                        ),
    
                            342 => array(
                                        1 => 1,
                                        2 => 16,
                                        3 => 23
                                        ),
    
                            343 => array()
                        );
    $output = array();
    
    foreach($products as $product_id=>$attributes){
        $attribute_id;
        foreach($attributes as $attribute_id=>$attribute_value){
            $output[$attribute_value][$attribute_id][] = $product_id;
        }
    
        // optionaly filter for dup product ids
        //$output[$attribute_value][$attribute_id] = array_unique($output[$attribute_value][$attribute_id], SORT_NUMERIC);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题