0

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
            )
    )
7
  • 3
    Works fine for me: 3v4l.org/FpMm1 where is the problem? Commented Mar 11, 2016 at 12:34
  • 2
    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. Commented Mar 11, 2016 at 12:35
  • @Yash So what is the solution? Commented Mar 11, 2016 at 12:36
  • 1
    @MujahedAKAS, you can either use '0' with condition or use == operator. Commented Mar 11, 2016 at 12:37
  • Works for me, Where is the issue? Commented Mar 11, 2016 at 12:37

1 Answer 1

2

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';
}
Sign up to request clarification or add additional context in comments.

7 Comments

Again: see 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.
|

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.