PHP遍历多维数组
如下是从json转成数组的多维数组如何能通过user1或者user2来取password的值?遍历试了很久Array([server]=>Array([path]=>[h...
如下是从json转成数组的多维数组 如何能通过user1或者user2来取password的值?
遍历试了很久
Array
(
[server] => Array
(
[path] =>
[hostname] =>
)
[config] => Array
(
[defaultUserID] => 0
[defaultRoomID] => 0
[eventRooms] => Array
(
[0] => 0
[1] => 0
)
)
[user] => Array
(
[0a8a5986a6c18920fd456fdfbb048913] => Array
(
[nickname] => user1
[userName] => username
[passWord] => password
[status] => 1
)
[0ffb201c2335a784abd19c2985797711] => Array
(
[nickname] => user2
[userName] => username
[passWord] => password
[status] => 1
)
我是想做一个用户输入nickname后 来查询password的功能 展开
遍历试了很久
Array
(
[server] => Array
(
[path] =>
[hostname] =>
)
[config] => Array
(
[defaultUserID] => 0
[defaultRoomID] => 0
[eventRooms] => Array
(
[0] => 0
[1] => 0
)
)
[user] => Array
(
[0a8a5986a6c18920fd456fdfbb048913] => Array
(
[nickname] => user1
[userName] => username
[passWord] => password
[status] => 1
)
[0ffb201c2335a784abd19c2985797711] => Array
(
[nickname] => user2
[userName] => username
[passWord] => password
[status] => 1
)
我是想做一个用户输入nickname后 来查询password的功能 展开
3个回答
展开全部
你的意思是用户输入nickname 比如输'user2' 然后你这边取user2对应的password吗?
$data = [
'user'=>[
"0a8a5986a6c18920fd456fdfbb048913"=>[
"nickname"=>"user1",
"userName"=>"username",
"passWord"=>"password",
"status"=>1,
],
"0ffb201c2335a784abd19c2985797711"=>[
"nickname"=>"user2",
"userName"=>"username",
"passWord"=>"password",
"status"=>1,
],
]
];
//第一种情况是user数组里有多个叫user2的nickname,这样查出的password也会有多个,所以就需要循环来处理了
$nickname='user2';//这里是用户输入的值
$password=[];
foreach ($data['user'] as $val){
if($val['nickname']==$nickname){
$password[]=$val['passWord'];
}
}
print_r($password);
//第二情况就是user数组里只会有一个叫user2的nickname,这样只会查出一个password
$nickname='user2';//这里是用户输入的值
$user=array_column($data['user'],null,'nickname');
$password=$user[$nickname]['passWord'];
echo $password;
2018-03-21
展开全部
仅供参考
<?php
$users = [
"0a8a5986a6c18920fd456fdfbb048913"=>[
"nickname"=>"user1",
"userName"=>"username",
"passWord"=>"password",
"status"=>1,
],
"0ffb201c2335a784abd19c2985797711"=>[
"nickname"=>"user2",
"userName"=>"username",
"passWord"=>"password",
"status"=>1,
]
];
$targetNickname = "user1";
foreach($users as $user)
{
if(isset($user['nickname']) && $user['nickname'] == $targetNickname)
{
echo isset($user['passWord'])?$user['passWord']:null;
break;
}
}
?>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
$arr = json获取的数组;
$nickname = '****';
$password = '';
foreach($arr as $key=>$v)
{
if(isset($v['nickname']) && $v['nickname']==$nickname){
$password = $v['password'];
}
}
$nickname = '****';
$password = '';
foreach($arr as $key=>$v)
{
if(isset($v['nickname']) && $v['nickname']==$nickname){
$password = $v['password'];
}
}
追问
感谢,虽然不知道为啥取不出user1的passWord ..我再研究一下。..
追答
哦哦 少看了个user键名
$arr = json获取的数组;
$nickname = 'user1';
$password = '';
foreach($arr as $key=>$value)
{
if($key!='user') continue;
foreach($value $as $k=>$v){
if($v['nickname']==$nickname){
$password = $v['password'];
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询