1

让我解释一下我的问题,我应该在显示联系表单之前调用 Web 服务,Web 服务的返回是:

$items = json_decode('[{"location":[{"building":["Building1"],"name":"Location1"}],"name":"Organization1"},{"location":[{"building":["Building2"],"name":"location2"}],"name":"Organisation2"},{"location":[{"building":["Building3"],"name":"Location3"}],"name":"Organization3"}]');

在这里,我使用以下代码仅提取了组织、位置和建筑物:

foreach( $items as $each ){
    echo $each->location[0]->building[0];
    echo $each->location[0]->name;
    echo $each->name;
}

我想以这种格式获取不同数组中的组织、建筑物和位置的值:

("building1", "building2", "building3")
("organisation1", "organisation2", "organisation3")
("location1", "location2", "location3")
4

2 回答 2

1

获取您现有的代码并对其进行一点点修改;

$buildings = array( );
$organisations = array( );
$locations = array( );
foreach( $items as $each ){
    $buildings[] = $each->location[0]->building[0];
    $organisations[] = $each->location[0]->name;
    $locations[] = $each->name;
}

结果现在位于顶部定义的变量中。[]只是告诉它在数组末尾附加一个值。

于 2013-03-23T03:28:06.483 回答
0

这是你如何做到的:

$arrays = array();
$arrays['buildings'] = array(); // buildings
$arrays['organisations'] = array();
$arrays['locations'] = array();
foreach( $items as $each ){
    $arrays['buildings'][] = $each->location[0]->building[0];
    $arrays['organisations'][] = $each->location[0]->name;
    $arrays['locations'][] = $each->name;
}
于 2013-03-23T03:29:10.973 回答