php二维数组的有关问题,求大神解答

WBOY
Release: 2016-06-13 12:09:30
Original
1166 people have browsed it

php二维数组的问题,求大神解答
我现在有个页面searchFangxiang.php定义了一个二维数组:
$fangxiang=array('fid101'=>array("fid"=>"101","Name"=>"自动化研究所","professor"=>"张教授"),
                                      'fid102'=>array("fid"=>"102","Name"=>"实验室","professor"=>"李教授"),
                  'fid103'=>array("fid"=>"103","Name"=>"科学研究所","professor"=>"王教授")););
 
?>


然后在我的应用页面引用了这个二维数组的页面


include 'searchFangxiang.php';

echo  "";
?>

现在应用页面有个id   我如何遍历这个二维数组,找到二维数组中fid与页面id相同的那一组,然后在标签中输出这一组的信息
------解决思路----------------------
按你的数据组织方式,不需要遍历
只需 echo $fangxiang["fid$id"]['Name']; 即可
------解决思路----------------------
搜索的。
<br /><?php<br />$fangxiang = array(<br />  'fid101' => array("fid"=>"101","Name"=>"自动化研究所","professor"=>"张教授"),<br />  'fid102' => array("fid"=>"102","Name"=>"实验室","professor"=>"李教授"),<br />  'fid103' => array("fid"=>"103","Name"=>"科学研究所","professor"=>"王教授"),<br />);<br />?><br /><meta http-equiv="content-type" content="text/html; charset=utf-8"><br /><script type="text/javascript"><br />var fangxiang = <?php echo json_encode($fangxiang); ?>;<br /><br />function search(){<br />    var key = document.getElementById("key").value;<br />    var type = document.getElementById("type").value;<br /><br />    if(key==''){<br />        alert('请输入要搜寻的关键字');<br />        return false;<br />    }<br /><br />    var result = [];<br /><br />    for(var row in fangxiang){<br />        var tmp = fangxiang[row][type];<br />        if(tmp.indexOf(key)!=-1){<br />            result.push(row);<br />        }<br />    }<br /><br />    var response = '';<br /><br />    if(result.length>0){<br />        for(var i=0,len=result.length; i<len; i++){<br />            response += fangxiang[result[i]].fid + ' ' + fangxiang[result[i]].Name + ' ' + fangxiang[result[i]].professor + ' <br>';<br />        }<br />    }<br /><br />    document.getElementById('result').innerHTML = response;<br />}<br /></script><br /><br /><p>search key:<input type="text" id="key"></p><br /><p>type:<select id="type"><br />    <option value="Name">Name</option><br />    <option value="professor">professor</option><br /></select><br /></p><br /><p><input type="button" value="search" onclick="search()"></p><br /><div id="result"></div><br />
Copy after login


Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template