php-foreach-通过键名获取特定数据 [英] php - foreach - get the specific data by key name

查看:140
本文介绍了php-foreach-通过键名获取特定数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从下面的foreach代码中按键名获取特定数据.

how to get specific data by key name from the following foreach code.

    <?php
    foreach ($_SESSION as $key=>$val)
    echo $key." ".$val."<br/>";
    ?>

数组看起来像这样想获取特定键的值.

array is look like this want to get value of specific key.

   {"name":"bedroom","rent":"sale","props":"","leases":""}

我尝试了以下代码,但不起作用

i have tried following code but doesn't work

echo "checking key sep. ".$key[rent];

如果可以的话,我可以通过名称或租金之类的键名进行访问.

if possible i can access by key name like name or rent.

推荐答案

格式为JSON,您首先需要使用 json_decode()

The format is in JSON , you need to decode it first by using json_decode()

类似这样的东西

$yourJSON = '{"name":"bedroom","rent":"sale","props":"","leases":""}';
$yourarray = json_decode($yourJSON,1);

然后您可以使用这样的 foreach 构造循环 $ yourarray .

You can then loop the $yourarray using the foreach construct like this.

foreach($yourarray as $key=>$val)
{
echo $key." ".$val."<br/>";
}

用于从中检索特定密钥.

For retrieving a specific key from it..

   echo $yourarray['rent']; //"prints" sale

这篇关于php-foreach-通过键名获取特定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆