-3

I'm calling this php type inside cakephp which is "in_array". Basically I'm checking whether both fields are available inside the array. The problem is that by this method it should result in outputting only one statement by checking if the fields are in the array. The result is like skipping the array check and outputting both statements which is incorrect.

This is my call in the View.ctp,

foreach($types as $type)
{
    if(in_array(array($carId, $type->type_id), $types))
    {
        echo $this->Html->link(
            'Remove',
            ['controller' => 'cars', 'action' => 'removeType'],
            ['class' => 'btn btn-success']
        );
    }else
    {
        echo $this->Html->link(
            'Add',
            ['controller' => 'cars', 'action' => 'addType'],
            ['class' => 'btn btn-success']
        );
    }

This is how I'm calling my database:

$typesTable = TableRegistry::getTableLocator()->get("Types");
$types = $typesTable->find('all')->toArray();
$this->set('types', $types);

The output result should be a button Remove if $carId is equal to $typesId in the database, if not equal to Add button should be displayed.

  • 2
    It is **impossible** for the if _and_ the else branch to be executed at the same time. If you get output from both - then you are running through this more than once, with different sets of input data. – 04FS Jan 14 '20 at 12:29
  • @04FS I'll edit my code since it's in a foreach but still, the result isn't as suppose – DontStopLearn Jan 14 '20 at 12:30
  • in_array() seems to be working https://3v4l.org/A58fF – nice_dev Jan 14 '20 at 12:30
  • @vivek_23 I just edited my code to be more clear, sorry – DontStopLearn Jan 14 '20 at 12:32
  • 1
    Are you trying to see if both `$carId` & `$type->type_id` are present in your $types array? Is your `$types` array actually supposed to contain an array which has both of those variables in them? Or are you trying to find out which (if any) of those two variables are in your `$types` array? – Campiotti Jan 14 '20 at 12:35
  • @Campiotti Yes I'm trying to see if both $carId & $type->type_id are both present within the same id example of a friends type. Yes $types has both values stored in it which are retrieved from the DB as toArray() – DontStopLearn Jan 14 '20 at 12:38
  • @DontStopLearn Can you post the actual values? It's hard to say anything. – nice_dev Jan 14 '20 at 12:39
  • The values of $types are id, carId and typeId in the DB. – DontStopLearn Jan 14 '20 at 12:41
  • You didn't post your data as `var_export()` like I requested, so I had to manually construct your data to prepare it for testing. Do you have predetermined variable to check against? Is this what you want? https://3v4l.org/oecd3 @Dont – mickmackusa Jan 14 '20 at 20:20
  • @mickmackusa I didn't really understand this answer. I checked the data(array) by using a 'pr($types); die;' since I'm using CakePHP 3.0x. In the given link, it's not what I did in my project since I'm fetching the value from the database (SQL). Thus let me re-explain my scenario. I'm getting an array of all information in the SQL Table named $types, then I'm making a foreach in the View.ctp which it checks and prints each row in the database. The array I want is to check $carId and compare it to $typeId. – DontStopLearn Jan 15 '20 at 04:50
  • So if there is a row in the database which has both of those fields it will print (they are in array) else prints (they are not in array). – DontStopLearn Jan 15 '20 at 04:56
  • @Dont so you just want the most basic form of a conditional expression? https://3v4l.org/t97Pu I don't understand where you are stuck. – mickmackusa Jan 15 '20 at 05:15
  • I'm stuck because as I said, it's printing both of the statements. It's like completely ignoring the 'in_array' @mickmackusa – DontStopLearn Jan 15 '20 at 05:32
  • @Dont I am ready to give up trying to help you. Last try... show me all of the data for all variables in this scenario then show me your expected output. – mickmackusa Jan 15 '20 at 05:52
  • @mickmackusa I've just edited it, hope now it clearer? – DontStopLearn Jan 15 '20 at 06:28
  • @Dont Does this work as desired in your script? https://3v4l.org/isfsP – mickmackusa Jan 15 '20 at 06:40
  • @mickmackusa Yes kind off, but it's printing 2 remove buttons.. while it should print 1 add and 1 remove since one of the Cars has that type and the other hasn't.. – DontStopLearn Jan 15 '20 at 06:43
  • @Dont I cannot replicate your issue because I don't know your exact variable data. Show me `var_dump($carId);` and `var_dump($types);`. With clearly displayed project data, this could have been fixed yesterday. – mickmackusa Jan 15 '20 at 06:46
  • @mickmackusa So var_dump($carId) is in this chase 5 since the $carId is associated with a user.. var_dump($types) = array(1) { [0]=> object(Cake\ORM\Entity)#200 (12) { ["car_id"]=> int(5) ["type_id"]=> int(7) ["[new]"]=> bool(false) ["[accessible]"]=> array(1) { ["*"]=> bool(true) } ["[dirty]"]=> array(0) { } ["[original]"]=> array(0) { } ["[virtual]"]=> array(0) { } ["[hasErrors]"]=> bool(false) ["[errors]"]=> array(0) { } ["[invalid]"]=> array(0) { } ["[repository]"]=> string(5) "Types" } } – DontStopLearn Jan 15 '20 at 06:53
  • @Dont So then, you mean to check `if ($carId == $type->car_id) {` and completely ignore the type_id? – mickmackusa Jan 15 '20 at 07:05
  • @mickmackusa Yes – DontStopLearn Jan 15 '20 at 11:27
  • @mickmackusa Thanks for the responses, I figured another way and it's working. Appreciate your effort! – DontStopLearn Jan 15 '20 at 12:16

2 Answers2

0

As PHP docs for the in_array() function states:

Searches for needle in haystack using loose comparison unless strict is set.

Meaning that doing

return in_array(['foo', 'bar'], $arr);

is equivalent to

foreach($arr as $element) {
    if ($element == ['foo', 'bar']) {
        return true;
    }
}
return false;

Coming back to your code, what you probably want to do is

foreach($types as $type){
   if(in_array($carId, $types) && in_array($type->type_id, $types))
   {
       //both $carId and $type->type_id are in the $types array
   }else
   {
       //either one or both of them are not in the array
   }
}
Jen
  • 1,206
  • 9
  • 30
  • 1
    This is recommended in the duplicate provided. – mickmackusa Jan 14 '20 at 12:51
  • @mickmackusa The duplicated provided is not even what I asked for. I'm not searching for a duplicate value inside the array, I'm searching for specific values inside the array. – DontStopLearn Jan 14 '20 at 12:55
  • 1
    I am well aware of your requirements. "_I'm trying to see if both $carId & $type->type_id are both present within the same id example of a friends type._" Read the answers at the dupe page. Closing your question with a correct and pre-existing page is not a punishment, it is leveraging the massive knowledge base that is Stack Overflow to show you how to achieve your goal. – mickmackusa Jan 14 '20 at 12:56
  • @mickmackusa Thanks, yes of course but as I'm reading answers provided inside the other question they are using array_intersect which probably isn't what I was looking for. – DontStopLearn Jan 14 '20 at 13:02
  • @mickmackusa Same id meaning that the $carId has the $typeId in the DB for example ($carId = 2 & $typeId = 4, 2 has 4), not because they have both the Id 2 for example. – DontStopLearn Jan 14 '20 at 13:11
  • Show your actual project data in your question as an edit. Use `var_export($types)`. – mickmackusa Jan 14 '20 at 13:16
0

You should pass String instead of array here

    $people = array("Peter", "Joe", "Glenn", "Cleveland");
    $searchStrings = array("Joe","Glenn");

    if(in_array('Joe', $people))
    {
        //Outputs if they are in the array...
    }else
    {
       //Outputs that they are not in the array...
    }

If you want to check into array then you should iterate a loop like this

foreach($searchStrings as $string){
    if(in_array($string, $people))
    {
        //Outputs if they are in the array...
    }else
    {
       //Outputs that they are not in the array...
    }
}
  • This is not exactly what the OP requires. The OP needs to know that both strings exist in the array - at the same time - not indivual checks over different iterations. – mickmackusa Jan 14 '20 at 12:53