douyanyan1123 2017-01-07 10:30
浏览 3546

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

Sorry I don't know the actual title of my question. I have a problem with a php array. my array is 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
                    (
                        [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
                    )

            )

    ) 

Now I want to insert

[Subcat]=>'changed'

if the subcat_id is changed . How to check if the subcat_id is changed on the next key value.please help.

I have tried this :

    $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'];
    }
    }

But not working properly.

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

                    )

            )   


    )

Is this possible.

  • 写回答

3条回答 默认 最新

  • dos8410 2017-01-07 10:43
    关注

    This will do what you are looking for:

    $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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计