开发者

Searching Arrays, array_search() issue

Is there any way I can search an array for a value and return it's key, I tried array_search() with no success... below is an example of my array

[0] => Array
    (
        [value] => 
        [text] => All Call Types
    )

[1] => Array
    (
        [value] => enquiry
        [text] => Renovation Enquiry
    )

[2] => Array
    (
        [value] => msg
        [text] => Message to Pas开发者_JAVA技巧s on
    ) ...

My ultimate goal is to convert

value to text.

Here's what I tried:

$key = array_search($row['call_type'], $type_list);
$call_type_name = $type_list[$key]['text'];

Thanks!


You could write a short function that provides this:

function findInArray($array, $needle)
{
    for ($i = 0; $i < sizeof($array); $i++)
    {
        if ($array[$i]['value'] == $needle) return $array[$i]['text'];
    }
}

Usage example:

$call_type_name = findInArray($type_list, 'msg');


is this what you are after? finding the position of the occurrence of a specific value?

function findKeyByField( $arr, $name, $val ){
$pos = 0;
foreach ($arr as $subArr ):

    foreach ($subArr as $key => $value):
        if( $key == $name and $value == $val ){
            return $pos;
        }
    endforeach;

$pos++;
endforeach;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜