[英]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));
我尝试了(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. 这是因为$lang_result
是具有数组元素的数组。 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. 我建议你foreach
在$lang_result
(甚至更好for
,所以你得到一个计数器马上), 然后使用array_search
找到,如果有匹配,然后标记使用计数器元素。
It doesn't work, because $lang_result is a multidimensional array. 它不起作用,因为$ lang_result是多维数组。 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. 这将防止阵列变为多维。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.