Stack Exchange
How to insert a new array element in parent array in foreach
I have an array $categories as follows;
Array
    (
        [0] => Array
            (
                [category_id] => 0
            )

        [1] => Array
            (
                [category_id] => 3
            )
    )

I want to apply some condition in foreach as follows;

Note: following condition is not working;

foreach ($categories as $key=> $category)
{
    if($category['category_id']===0)
    {
        $categories[$key]['category_name'] = 'NA';
    }
    else
    {
        $categories[$key]['category_name'] = 'something';
    }
}

so my expected result will become;
Array
    (
        [0] => Array
            (
                [category_id] => 0
                [category_name] => NA
            )

        [1] => Array
            (
                [category_id] => 3
                [category_id] => something
            )
    )


Works fine for me: https://3v4l.org/FpMm1 where is the problem?

Possible that 0 is string you are checking with === operator which will match string with its datatype also. Check it please. And use '0' in current condition.

@Yash So what is the solution?

@MujahedAKAS, you can either use '0' with condition or use == operator.

Works for me, Where is the issue?

@Yash Man you are really YASH (success)? I replace === with == its working. Please post the same in answer.

"Possible that 0 is string" good thinking but, no, you didn't take php's weirdness into account ;-) see https://3v4l.org/rQBKh

In the array there is possible that 0 could be string you are checking with === operator which will match string with its datatype also.
You can check its datatype if needed using gettype($value).
For current issue try this:
/* Compare value without checking its datatype */
if($category['category_id'] == 0)  /* replace === to == */
{
    $categories[$key]['category_name'] = 'NA';
}
else
{
    $categories[$key]['category_name'] = 'something';
}


Again: see https://3v4l.org/rQBKh , no, according to the output of print_r it's not possible that the key is a string, because php converts it to an integer. If you don't believe that ...show me ;-)

Yeaa @VolkerK, I still don't know, How 0 is is coming as string cuz in DB category_id's datatype is int(11).

@VolkerK if you declare string array such as $str_arr = array('0','1','2');. Kindly check the output. php array index first trying to treat numeric array then associative array.

Oh, no. I'm sorry. I completely misread the question...... you're comparing the value, not the key. Totally possible that you you get the value as a string, even though it's defined as an Integer in the database. mysqlnd can give you the correct datatype. But you'd have to use mysqlnd and then there was some config parameter/function call you have to make for mysqli ...forgot what it was.

Agree with your concept @VolkerK. Thanks for good solution anyways.

$mysqli->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, 1);  see https://dev.mysql.com/doc/apis-php/en/apis-php-mysqli.quickstart.statements.html "Example 3.10" and the text above the example. For PDO on the other hand that seems to be the dafault.

Yeah sorry guyz, I too misunderstood both of you, 0 is still coming as integer. I was comparing it as string by using ===
