沃梦达 / 编程问答 / php问题 / 正文

php - foreach - 通过键名获取具体数据

php - foreach - get the specific data by key name(php - foreach - 通过键名获取具体数据)

本文介绍了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 - 通过键名获取具体数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:php - foreach - 通过键名获取具体数据

基础教程推荐