问题
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.
回答1:
The format is in JSON , you need to decode it first by using json_decode()
Something like this...
$yourJSON = '{"name":"bedroom","rent":"sale","props":"","leases":""}';
$yourarray = json_decode($yourJSON,1);
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
来源:https://stackoverflow.com/questions/21212740/php-foreach-get-the-specific-data-by-key-name