如何检查数组的元素是否已更改

时间:2017-01-07 10:30:42

标签: php arrays cakephp

抱歉,我不知道问题的实际标题。我有一个PHP数组的问题。 我的数组就像:

 $Array =
    (
        [0] => Array
            (
                [Product] => Array
                    (
                        [cat_id] => 7
                        [subcat_id] => 1
                    )

            )

        [1] => Array
            (
                [Product] => Array
                    (
                        [cat_id] => 7
                        [subcat_id] => 1
                    )

            )


        [2] => Array
            (
                [Product] => Array
                    (
                        [cat_id] => 8
                        [subcat_id] => 2
                    )

            )


        [3] => Array
            (
                [Product] => Array
                    (
                        [cat_id] => 9
                        [subcat_id] => 3
                    )

            )

        [4] => Array

            (
                [Product] => Array
                    (
                        [cat_id] => 9
                        [subcat_id] => 3
                    )

            )

    ) 

现在我要插入

[Subcat]=>'changed'

如果subcat_id已更改。如何检查下一个键值是否更改了subcat_id。请帮助。

我试过这个:

    $sub_cat_last = '';
    foreach ($Array as $key => $p) {   
    $sub_cat = $p['Product']['subcat_id'];
    if($sub_cat != $sub_cat_last){    
    $Array[$key]['Subcat'] = 'change';
    $sub_cat_last = $p['Product']['subcat_id'];
    }
    }

但工作不正常。

I want my array like :



 Array
    (
        [0] => Array
            (
                [Product] => Array
                    (
                        [cat_id] => 7
                        [subcat_id] => 1

                    )
            )

        [1] => Array
            (
                [Product] => Array
                    (
                        [cat_id] => 7
                        [subcat_id] => 1

                    )

            )

         [2] => Array
            (
                [Product] => Array
                    (
                        [Subcat] => change

                    )

            )   



        [3] => Array
            (
                [Product] => Array
                    (
                        [cat_id] => 8
                        [subcat_id] => 2

                    )


            )


        [4] => Array
            (
                [Product] => Array
                    (
                        [Subcat] => change

                    )

            )


        [5] => Array
            (
                [Product] => Array
                    (
                        [cat_id] => 9
                        [subcat_id] => 3

                    )

            )

        [6] => Array
            (
                [Product] => Array
                    (
                        [cat_id] => 9
                        [subcat_id] => 3
                    )

            )

         [7] => Array
            (
                [Product] => Array
                    (
                        [Subcat] => change

                    )

            )   


    )

这可能。

3 个答案:

答案 0 :(得分:1)

怎么样:

$Array = Array(
    0 => Array(
        'Product' => Array(
            'cat_id' => 7,
            'subcat_id' => 1,
        )
    ),
    1 => Array(
        'Product' => Array(
            'cat_id' => 7,
            'subcat_id' => 1,
        )
    ),
    2 => Array(
        'Product' => Array(
            'cat_id' => 8,
            'subcat_id' => 2,
        )
    ),
);

$prev_id = $Array[0]['Product']['subcat_id'];
$result = array(0 => $Array[0]);
for($i = 1; $i < count($Array); $i++) {
    if ($Array[$i]['Product']['subcat_id'] != $prev_id) {
        $result[]['Product']['subcat'] = 'change';
    }
    $result[] = $Array[$i];
    $prev_id = $Array[$i]['Product']['subcat_id'];
}
$result[]['Product']['subcat'] = 'change';
print_r($result);

<强>输出:

Array
(
    [0] => Array
        (
            [Product] => Array
                (
                    [cat_id] => 7
                    [subcat_id] => 1
                )

        )

    [1] => Array
        (
            [Product] => Array
                (
                    [cat_id] => 7
                    [subcat_id] => 1
                )

        )

    [2] => Array
        (
            [Product] => Array
                (
                    [subcat] => change
                )

        )

    [3] => Array
        (
            [Product] => Array
                (
                    [cat_id] => 8
                    [subcat_id] => 2
                )

        )
    [4] => Array
        (
            [Product] => Array
                (
                    [subcat] => change
                )

        )

)

答案 1 :(得分:0)

这将满足您的需求:

$old_subcat_id = null;
$newArray = Array();

foreach ($Array as $item) {
    $subcat_id = $item['Product']['subcat_id'];

    if ($subcat_id != $old_subcat_id) {
        //do something here
        echo "subcat_id has changed from [$old_subcat_id] to [$subcat_id]<br>";

        if ($old_subcat_id != null) {
            $newArray[]['Product']['Subcat'] = 'change';
        }
        $old_subcat_id = $subcat_id;
    }

    $newArray[] = $item;
}

答案 2 :(得分:0)

保存以前的cat_id和subcat_id,检查cat_id何时添加一个。这假设你cat_id是顺序的,每步加0或1。

$preValue = array('cat_id' => 0, 'subcat_id' => 0);
$changed = false;
foreach ($Array as $item) {
    $product = $item['Product'];
    if($preValue['cat_id'] != 0 && $product['cat_id'] == $preValue['cat_id'] + 1 && $product['subcat_id'] != $preValue['subcat_id'])
    {
       $changed = true;
       break;
    }
    $preValue['cat_id'] = $product['cat_id'];
    $preValue['subcat_id'] = $product['subcat_id'];

}