3

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()?

1
  • in_array() is possible.. i have used to same problem.. Commented Dec 24, 2013 at 11:19

3 Answers 3

2

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

1 Comment

this function can also be modified to check other keys like textId, Id , just introduce a 3rd paramter in the function for array key , the value you want to check is already being passed
0

You can check by isset the key and match the value in php.

$dataArray =  array(
    0 => array(0 => 10 ,'Id' => 10, 1 => 10, 'TextId' => 10, 2 => 1, 'LanguageId' => 1),1 => array(0 => 10 ,'Id' => 10, 1 => 10, 'TextId' => 10, 2 => 1, 'LanguageId' => 1)

);

foreach($dataArray as $value) {
    if(isset($value['LanguageId']) && $value['LanguageId'] == 2) {
        echo 'language ID 2 is available';
    }
}

Working Demo

1 Comment

I feel like a complete idiot to not have thought about isset(). Thank you so much
0

After giving this some more thought I came up with a maybe not so elegant solution. Instead of getting an array back I modified the SQL Query to give one row back with the default language (English) and a user selected language (Japanese). It uses two left joins. This shows that I received (some) training in SQL but am really not at ease with multidimensional arrays.

Query def_text = text in default language usr_text = text in user chosen language

    $table01 = "text";
    $query="SELECT $table01.TextId, 
            text_def.Text as def_text,
            text_usr.Text as usr_text
            FROM $table01 
            LEFT JOIN $table01 as text_def ON $table01.TextId = text_def.TextId AND text_def.LanguageId = $_SESSION[default_language]
            LEFT JOIN $table01 as text_usr ON $table01.TextId = text_usr.TextId AND text_usr.LanguageId = $_SESSION[language]
            WHERE $table01.TextId=$mess;";

after getting back the results it is easy to check with isset() or empty() to see if the text is available in the user selected language

Comments

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.