Home > php教程 > php手册 > body text

查找数组中指定键名的值

WBOY
Release: 2016-06-13 10:44:45
Original
1574 people have browsed it

HTML







Ajax传值


   

        输入要查找的值:
       
   

    查找的结果为:
   

   
   

   
   
<br>
	//示例数组 搜索 depth<br>
	$src_array = array(<br>
	   'test' => '123',<br>
	    array(<br>
	        'has' => 'OK',<br>
	        'find'=> true,<br>
	        array(<br>
	            'depth' => 'soso',<br>
	array(<br>
	'123' => 'wangbin',<br>
	'wangbin' => 'wangbin456',<br>
	array(<br>
	'wangbin' => 'wangbin123',<br>
	),
<p>
	),<br>
	        ),<br>
	    ),<br>
	);<br>
	</p>
Copy after login


php

function find_array_key($find, $src_array)
{
    if (is_array($src_array))
    {
$src = '';
        if (isset($src_array[$find]))
        {
$src .= $src_array[$find];
#可以完善 删除 查找到的值 在查找
return $src;
        }else
        {
            foreach ($src_array as $key => $value)
            {
                if (is_array($value))  #判断是否是数组 如果是进行循环递归调用
                {
                    return find_array_key($find, $value);
                }
            }
            return false;
        }
    }else{
        return false;
    }
}

$src_array = array(
   'test' => '123',
    array(
        'has' => 'OK',
        'find'=> true,
        array(
            'depth' => 'soso',
array(
'123' => 'wangbin',
'wangbin' => 'wangbin456',
array(
'wangbin' => 'wangbin123',
),

),
        ),
    ),
);

$username = $_REQUEST['username'];
if($username){
echo find_array_key($username, $src_array);
}else{
echo '查找失败';
}
?>

作者“wbgod_1987”
 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template