0

I want to find flag key from array and replace new value. for example if i found flag values == 2 then i'll put new value instead of old.

Following is my array.

Array
(
    [0] => Array
        (
            [id] => 863
            [flag] => 1
            [qty] => 2
            [size] => 8
            [fly_name] => Bead Headed Prince Nymphs size 18
        )

    [1] => Array
        (
            [id] => 861
            [flag] => 1
            [qty] => 2
            [size] => 8
            [fly_name] => Bead Headed Prince Nymphs size 14
        )

    [2] => Array
        (
            [id] => 671
            [flag] => 1
            [qty] => 4
            [size] => 8
            [fly_name] => Royal Wulff size 12
        )

    [3] => Array
        (
            [id] => 661
            [flag] => 1
            [qty] => 2
            [size] => 3
            [fly_name] => Hare's Ear Tan size 16
        )
)

I want to find if flag key == 2 then replace new value of flag key , i have tried following way but not work.

if(in_array(2, $fliesUserColumn)) { // search value in the array
    foreach($fliesUserColumn as $key => $val)
    {
            if ($val == '2') $fliesUserColumn[$key] = 'search4';
            }
    }else{
            echo "not";
    }
        echo "<pre>";
        print_r ($flyOfStores);
        echo "</pre>";
    die;

And out put like with new values in array

Array
(
    [0] => Array
        (
            [id] => 863
            [flag] => newvalue
            [qty] => 2
            [size] => 8
            [fly_name] => Bead Headed Prince Nymphs size 18
        )

    [1] => Array
        (
            [id] => 861
            [flag] => newvalue
            [qty] => 2
            [size] => 8
            [fly_name] => Bead Headed Prince Nymphs size 14
        )
   ...

)

2 Answers 2

2

Try below example

foreach ($fliesUserColumn as $key => $val) {
        if ($fliesUserColumn[$key]['flag'] == '2') {
            $fliesUserColumn[$key]['flag'] = 'Newvalue';
       } 
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try this

    foreach ($fliesUserColumn as $key => $val) {
        if ($fliesUserColumn[$key]['flag'] == '2') {
            $fliesUserColumn[$key] = 'search4';
        } else {
                echo "not";
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.