从数组中获取特定值

时间:2016-06-12 06:07:55

标签: php arrays

我试图从返回的每条记录中获取数组中的特定列。

该数组称为字段,数组中的一个数组是位置。我正在寻找名为name的数组中的特定列。

以下是我所拥有的:

foreach ($new_results as $result):?>
$locations = array_map($result->locations->location,function($obj) {   return $obj->location; });
echo implode(",",$locations);
endforeach;

我正在连接到网络服务以提取此数据。以上是公司给我的代码,但就我所知,他们还没有对它进行过测试。它对我不起作用。

这是对API的调用

$results = $connection->call('groups/getAll', $params=array("suspended" => "no","fields" =>"locations"));
$new_results = $results->groups->group;

以下是API的一个示例。

{
"id": "xxxx",
"fields": {
    "locations": [
        "North",
        "Central"
    ]
}
}

对我做错了什么的任何想法?我对PHP仍然很陌生,所以我可能会遗漏一些非常明显的东西。

1 个答案:

答案 0 :(得分:0)

如何使用curl获取json: How to get JSON data from Rest API by PHP Curl?

现在考虑到这一点,您可以根据响应方式深入了解对象操作:Accessing JSON object elements in PHP

//""" code from the second link

/*Variable passed in from the ExtJS interface as JSON object*/
$json = $_POST["newUserInfo"];
//$json = '{"USER":{"ID":"","FULL_USER_NAME":"Some Guy","ENTERPRISE_USER_NAME":"guyso01","USER_EMAIL":"Some.Guy@Email.com","USER_PHONE":"123-456-7890"},"PERMISSIONS":{"ID":"","USER_ID":"","IS_ADMIN":"true"},"SETTINGS":{"ID":"","USERS_ID":"","BACKGROUND":"default"}}';

//Test to view the decoded output
//var_dump(json_decode($json));

//Decode the $json variable
$jsonDecoded = json_decode($json,true);

//Create arrays for each table from the $jsonDecoded object
$user_info = array($jsonDecoded['USER']);
$permissions_info = array($jsonDecoded['PERMISSIONS']);
$settings_info = array($jsonDecoded['SETTINGS']); 

// """ code from the first link 

&安培;

>>> import boto
>>> conn=boto.connect(<credentials>)
>>> src = conn.get_bucket("srcs3bck")
>>> src.get_all_keys()
printing my key list properly.
>>> dst = conn.get_bucket("dsts3bck")
>>>
>>> src.get_all_keys()
[<Key: srcs3bck,Test2.txt>]
>>> 
>>> src.list()
<boto.s3.bucketlistresultset.BucketListResultSet object at 0x03768310>
>>> x=src.list()
>>> for i in x: print i
<Key: srcs3bck,Test2.txt>
>>> for i in x: print i.key
Test2.txt
>>> for i in x: dst.copy_key(i.key,src,i.key)
.......
ignoring other lines
....
S3ResponseError: S3ResponseError: 404 Not Found
<Error><Code>NoSuchBucket</Code><Message>The specified bucket does not exist</Message><BucketName>&lt;Bucket: srcs3bck&gt;</BucketName><RequestId>358C4C2420BB91E9</RequestId><HostId>ws6wagGIZqquStqisfGuYHeMMscmmR2Iu8LL4jCv9KpLBzxieiryI/mRwxwz4aQ=</HostId></Error>
>>>