如果数组值相同,则添加到现有数组

时间:2014-09-10 20:21:19

标签: php arrays

我一整天都在与这个斗争,并希望另一双眼睛可能会给我一些见解。我不确定我是否正确接近这个问题。我有一个数组列出了所购买的产品,但如果产品在数组中重复,我想将这些属性合并在一起。

$order = $order->getProducts();
$newArray = array();
foreach ($order as $order)
{
    $product = new Product($order['product_id']);

    $combination = new Combination($order['product_attribute_id']);
    $attribute = $combination->getAttributesName($context->language->id);
    foreach ($attribute as $attribute)
        $attributeName = $attribute['name'];

    $newArray[] = array(
        'id_product' => $order['product_id'],
        'name' => $product->name[$context->language->id],
        'combination' => array(
            'id_product_attribute' => $order['product_attribute_id'],
            'type' => $attributeName
        )
    );
}

我的阵列看起来像这样

Array (
[0] => Array
    (
        [id_product] => 117
        [name] => Sidewinder 1-Foot Extension
        [combination] => Array
            (
                [id_product_attribute] => 172
                [type] => Black
            )

    )

[1] => Array
    (
        [id_product] => 117
        [name] => Sidewinder 1-Foot Extension
        [combination] => Array
            (
                [id_product_attribute] => 173
                [type] => Black & Yellow
            )

    )

[2] => Array
    (
        [id_product] => 119
        [name] => ENV100
        [combination] => Array
            (
                [id_product_attribute] => 0
                [type] => Black & Yellow
            )

    )

)

我想尝试实现这个目标

Array (
[0] => Array
    (
        [id_product] => 117
        [name] => Sidewinder 1-Foot Extension
        [combination] => Array
            (
                [0] => Array
                    (
                         [id_product_attribute] => 172
                         [type] => Black
                    )
                 [1] => Array
                    (
                         [id_product_attribute] => 173
                         [type] => Black & Yellow
                    )
            )

    )
[1] => Array
    (
        [id_product] => 119
        [name] => ENV100
        [combination] => Array
            (
                [id_product_attribute] => 0
                [type] => 
            )

    )

)

1 个答案:

答案 0 :(得分:0)

可能会让它看起来好一点,但这应该有效:

foreach ($order as $order) {
    $product = new Product($order['product_id']);
    $combination = new Combination($order['product_attribute_id']);
    $attribute = $combination->getAttributesName($context->language->id);

    $newArray[$order['product_id']] = array(
            'id_product' => $order['product_id'],
            'name' => $product->name[$context->language->id],
    );

    foreach ($attribute as $attr) {        
        $newArray[$order['product_id']]['combination'][] => array(
                'id_product_attribute' => $attr['id'], // a guess
                'type' => $attr['name'],
        );
    }
}

数组键将是product_id所以如果你想要它们,那么在循环之后:

$newArray = array_values($newArray);

我不知道这是做什么就刺了一下:

foreach ($attribute as $attribute)
        $attributeName = $attribute['name'];