how to check if the element of an array is changed

Question

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.


Show source
| cakephp   | php   | arrays   2017-01-07 11:01 3 Answers

Answers to how to check if the element of an array is changed ( 3 )

  1. 2017-01-07 11:01

    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;
    }
    
  2. 2017-01-07 11:01

    save previous cat_id and subcat_id, check when cat_id added one. This suppose you cat_id is in sequence, added 0 or 1 each step.

    $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'];
    
    }
    
  3. 2017-01-07 12:01

    How about:

    $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);
    

    Output:

    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
                    )
    
            )
    
    )
    

Leave a reply to - how to check if the element of an array is changed

◀ Go back