Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if a value exist in array from database query

Tags:

arrays

php

mysql

I am trying to build a website that will display the text in multiple languages. I have a table 'text' with all the languages. If the text does not exist in the chosen language it has to display the default language.

query SELECT * FROM text WHERE TextId = 10 results in

Id  TextId  LanguageId  Text
10  10      1           first name
13  10      2           名前

If I r_print this result I get something like this

Array ( [0] => Array ( [0] => 10 [Id] => 10 [1] => 10 [TextId] => 10 [2] => 1 [LanguageId] => 1 [3] => first name [Text] => first name ) 
        [1] => Array ( [0] => 13 [Id] => 13 [1] => 10 [TextId] => 10 [2] => 2 [LanguageId] => 2 [3] => 名前 [Text] => 名前 ) )

How can I check that LanguageId 2 exist in this array ? the problem is that it is possible that TextId 2 and Id 2 can also exist in this array.

Is this possible to do with in_array()?

like image 654
anatak Avatar asked Sep 20 '26 15:09

anatak


1 Answers

Here is a function that can check if LanguageId equals a special value .

function isLanguageIdExists($yourArray , $LanguageId){
    $exists=false;
    foreach($yourArray as $array){
     if(isset($array['LanguageId'])&& $array['LanguageId'] == $LanguageId){
        $exists=true;break;
      }

    }
    return $exists;
}

$exist = isLanguageIdExists($yourArray , 2);//return true or false
like image 93
Charaf JRA Avatar answered Sep 22 '26 08:09

Charaf JRA