2016-06-12 19 views
-1

Ich versuche, für jeden zurückgegebenen Datensatz eine bestimmte Spalte aus einem Array abzurufen.Abrufen eines bestimmten Werts aus einem Array

Das Array heißt Felder und eines der Arrays im Array ist Positionen. Ich suche nach einer bestimmten Spalte im Array namens name.

Hier ist, was ich habe:

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

ich an einen Webdienst bin verbindet diese Daten zu ziehen. Das obige ist der Code, den die Firma mir gegeben hat, aber sie haben es nicht getestet, soweit ich weiß. Es funktioniert nicht für mich.

Hier ist der Aufruf an die API

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

Hier ein Beispiel aus der API ist.

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

Irgendwelche Gedanken darüber, was ich falsch mache? Ich bin immer noch sehr neu in PHP, so dass mir etwas sehr offensichtlich fehlt.

+0

bitte formatieren Code richtig –

+0

Das Beispiel wie JSON mir aussieht und es enthält ein Objekt, nicht eine innere Anordnung. – arkascha

Antwort

0

Wie json mit curl bekommen: How to get JSON data from Rest API by PHP Curl?

Jetzt mit diesem Gedanken hier gibt es ein wenig tiefer in die Objektmanipulation in Bezug auf die Reaktion tauchen kann: Accessing JSON object elements in PHP.

//""" code from the first link 

$service_url = "http://127.0.0.1:8000/api/thesis/?format=json"; 
$curl = curl_init($service_url); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 
//execute the session 
$curl_response = curl_exec($curl); 
//finish off the session 
curl_close($curl); 
$curl_jason = json_decode($curl_response, true); 
print_r($curl_jason); 

//""" code from the first link 

&

//""" 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":"[email protected]","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 
Verwandte Themen