I am wondering why this code wont work and was hoping to get some help:

$ids_to_check = array('rsm' => array('00540000000yHehAAE', '00530000000ter0AAA',
                    '00540000000llhVAAQ', '00540000000lllcAAA', '00540000000lllmAAA',
                    '00540000000llptAAA', '00540000000lltWAAQ', '00540000000lltgAAA',
                    '00540000000lm20AAA', '00540000000lm2EAAQ', '00540000000lm2JAAQ',
                    '00540000000lm2OAAQ', '00540000000lm2TAAQ', '00540000000lm2YAAQ',
                    '00540000000lm2dAAA'), 'fae' => array('00540000000lm2nAAA', '00540000000lm32AAA',
                    '00540000000lm37AAA', '00540000000lm3CAAQ', '00540000000lm3HAAQ',
                    '00540000000lm3gAAA', '00540000000lm3hAAA', '00540000000lm3iAAA',
                    '00540000000lm3jAAA', '00540000000lm3lAAA', '00540000000lm3mAAA',
                    '00540000000lm3nAAA', '00540000000lm3qAAA', '00540000000lm3rAAA',
                    '00540000000lm3vAAA'));
$checked = "00540000000yHehAAE"; 

if ($checked == $ids_to_check)
                    {
                        echo 'We found a match';
                    }
                    else
                    {
                        echo 'we aint found nothing';
                    } 

$checked does match one of the values in the $ids_to_check array and the IF statement is not finding a match. I am sure it is a simple coding error on my part, but i am for a loss here

Any help would be appreciated

TIA,
Mike

    In the if-statement $checked is compared to the array, not to the values inside the array. And the string is not the same as the array.

    You could try [man]array_search[/man] to solve your problem. I think that this page is good to get an easy description of different functions.

      I tired the array_search:

      $find = array_search($checked, $ids_to_check);
                          if ($checked == $find)
                          {
                              echo 'We found a match';
                          }
                          else
                          {
                              echo 'we aint found nothing';
                          } 

      and it does not do what it is supposed to, or am I coding this wrong (thought I was from the examples that are posted)

        Piranha wrote:

        And the string is not the same as the array.

        I don't understand what you mean, the value of $checked = $ids_to_check['rsm'][0]

          msimonds wrote:

          I don't understand what you mean, the value of $checked = $ids_to_check['rsm'][0]

          Yes, it is the same as $ids_to_check['rsm'][0]. But it is not the same as the whole array $ids_to_check, which is what you are comparing with. The computer doesn't know that you didn't want to compare with the whole array, that you only wanted to compare to see if the string existed in one of the elements in the array.

            ahh okay sir, thanks that is true, sorry my fault

            but the array_search does not do what it is supposed to

            Do you think that I should split this up into two separate arrays?

              About your problem: It is clear that you haven't read the whole manual page and the links in it. Please do that to solve your problem (maybe you should use another function and not array_search).

                Thanks Piranha!!! You were right, I needed to read more, sorry about that. I found what I needed and this works!! Does the following look okay to you?

                $ids_to_check = array('rsm' => array('00540000000yHehAAF', '00530000000ter0AAA',
                                        '00540000000llhVAAQ', '00540000000lllcAAA', '00540000000lllmAAA',
                                        '00540000000llptAAA', '00540000000lltWAAQ', '00540000000lltgAAA',
                                        '00540000000lm20AAA', '00540000000lm2EAAQ', '00540000000lm2JAAQ',
                                        '00540000000lm2OAAQ', '00540000000lm2TAAQ', '00540000000lm2YAAQ',
                                        '00540000000lm2dAAA'), 'fae' => array('00540000000yHehAAE', '00540000000lm32AAA',
                                        '00540000000lm37AAA', '00540000000lm3CAAQ', '00540000000lm3HAAQ',
                                        '00540000000lm3gAAA', '00540000000lm3hAAA', '00540000000lm3iAAA',
                                        '00540000000lm3jAAA', '00540000000lm3lAAA', '00540000000lm3mAAA',
                                        '00540000000lm3nAAA', '00540000000lm3qAAA', '00540000000lm3rAAA',
                                        '00540000000lm3vAAA'));
                                    $checked = $r->Id;
                
                                if (in_array($checked, $ids_to_check['rsm']))
                                {
                                    echo 'We found a match in RSM Array';
                                }
                                else
                                    if (in_array($checked, $ids_to_check['fae']))
                                    {
                                        echo 'We found a match in FAE Array';
                                    }
                                    else
                                    {
                                        echo 'we aint found nothing';
                                    }
                
                                exit;
                            }

                  Yep. Maybe you could change to have if-elseif-else instead of nested if-statements, but other than that it looks ok.

                  Sorry for being hard on you, it's just that I think it is better to teach how to find an answer than to give it straight away. For the record, I haven't worked much with arrays in PHP and had to search myself to find array_search. I would not give the wrong function just to teach someone how to search. And I didn't look close enough to see that it was nested arrays, I thought it was just one one-dimensional array. Sorry for that.

                    No that is totally cool, You did nothing wrong, in fact you totally got me off my A$$ and made me look deeper (which I need to do)!

                    I truly appreciate the help and guidance sir, really!

                    All the best,
                    Mike

                      Write a Reply...