1

So i am trying to search an array and it always gives me false for some reason even tho the letter exists in the name array. This is what i want it to do , search the array, if a match exist it should add the name to the $data array and send it to the AJAX by encode it to JSON. But my IF always gives false

Edit: The answer in the other question did not solve this since that will always cause my array to return false

    $user = array(


        0 => array(

            "id"=> 0,   
            "photo"=>"smily",
            "name"=>"Name One",
            ),

        1 => array(

            "id"=> 0,   
            "photo"=>"smily",
            "name"=>"Name Five",
            ),

        2 => array(

            "id"=> 0,   
            "photo"=>"smily",
            "name"=>"Name Four",
            ),

        3 => array(

            "id"=> 0,   
            "photo"=>"smily",
            "name"=>"Name Three",
            ),          
       );

$wordToSearch = "N";
$data = Array();

    for($i = 0; $i < count($user); $i++)
    {
        if(array_search($wordToSearch, array_column($user, 'name')) == false)
        {
        array_push($data, array(    
                        'name' => $user[$i]['name'],
                        'img' => $user[$i]['photo'],
                        'pos' => $user[$i]['position'],
                        'Id' => $user[$i]['id']             
                        )
                        );
        echo json_encode($data);
        }

    }
saman93
  • 157
  • 10
  • 3
    With `array_search` you have to use identity operator `===` because index 0 is evaluated as false – fusion3k Feb 24 '16 at 14:36
  • Possible duplicate of [array\_search() not finding 0th element](http://stackoverflow.com/questions/12870760/array-search-not-finding-0th-element) – fusion3k Feb 24 '16 at 14:37

2 Answers2

2

I could be wrong, but instead of

if(array_search($wordToSearch, array_column($user, 'name')) == false)

I think you want

if (strpos($user[$i]['name'], $wordToSearch) !== false)

array_column is used to get all the user names or all the user ids
array_search is used to search an array... but I think you're trying to search a string.

unrelated... instead of using array_push($data, array(...));, use $data[] = array(...); it's a bit more efficient as there's no function call.. and in my opinion, it's easier to code & read.

Brad Kent
  • 4,982
  • 3
  • 22
  • 26
0

You can use strpbrk - strpbrk — Search a string for any of a set of characters

foreach ($user as $val) {
    if (strpbrk($val['name'], $wordToSearch) != false) {
         array_push($data, array(    
                        'name' => $val['name'],
                        'img' => $val['photo'],
                        'pos' => $val['position'],
                        'Id' => $val['id']             
                        )
                        );
        echo json_encode($data);

        } 
    }
}
PhizoR
  • 36
  • 2
  • This worked kindoish, it would add anyone so for instance if my wordtosearch would be three it would add all the names – saman93 Feb 24 '16 at 15:57