简体   繁体   中英

how can i get index array php from condition below

$lang_result = array();

function f_language($lang)
{
    if (is_array($lang) === true)
    {
        foreach ($lang as $key => $value)
        {
            $temp[] = "'$value'";
        }
        $lang = implode(',',$temp);
    }    
    $qryLang = mssql_query ("
                SELECT LANG_CODE,LANG_TEXT
                FROM TLANGUAGE
                WHERE LANG_ID = 'EN' --coocie.lang_id
                AND LANG_CODE in ($lang) 
            ");

    global $lang_result;
    while ($row = mssql_fetch_array($qryLang))
    {
        array_push($lang_result,array("$row[LANG_CODE]" => "$row[LANG_TEXT]"));
    }
}
$lang_code = array();
array_push($lang_code,'ofc','sch');
#array_push($lang_code,'sch');
f_language($lang_code);
echo "<hr/>";
var_dump($lang_result);

Output:

array(2) { [0]=> array(1) { ["ofc"]=> string(6) "office" }
       [1]=> array(1) { ["sch"]=> string(6) "school" } 
     }

I tried with (array_search('office',$lang_result)); then i didn't get the index value. somebody kindly please help me?

It's because $lang_result is an array that has array elements. I'd suggest you do foreach on $lang_result (or even better for , so you get a counter right away) and then use array_search to find if there is a match, and then mark that element using a counter.

It doesn't work, because $lang_result is a multidimensional array. You need to seach in each of the subarrays. Try this function:

<?php
$lang_result = array(
    array("ofc" => "office"),
    array("sch" => "school")
    );

function search_text(&$array, $text)
// {{{
{
    foreach($array as $key => $value)
    {
        $result = array_search($text, $value);
        if ($result != FALSE)
        {
            return $result;
        }
    }
    return FALSE;
}
// }}}

print search_text($lang_result, "office")."\n";
var_dump(search_text($lang_result, "crap"));
?>

This code prints:

ofc 
bool(false)

I think, that's what you need.

Or replace

while ($row = mssql_fetch_array($qryLang))
{
  array_push($lang_result,array("$row[LANG_CODE]" => "$row[LANG_TEXT]"));
}

With

while ($row = mssql_fetch_array($qryLang))
{
  $lang_result[$row[LANG_CODE]] = $row[LANG_TEXT];
}

This will prevent the array becoming multi dimensional.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM