This question already has an answer here:
Why can't I access the array with index directly?
(1 answer)
Closed 6 years ago.
I have an array which var_dump($array) produces
array(7)
{
["*attributes"]=> array(4)
{
["type"]=> string(6) "hidden"
["name"]=> string(3) "hmo"
["class"]=> string(12) "form-control"
["id"]=> string(3) "hmo"
}
["*label"]=> NULL
["*labelAttributes"]=> array(0) { }
["*labelOptions"]=> array(0) { }
["*messages"]=> array(0) { }
["*options"]=> array(1)
{
["disable_inarray_validator"]=> bool(true)
}
["*value"]=> string(243) "{"My-Office":{"Floor":"New - ","Walls":"New - ","Door":"New - ","Switches":"New - ","Table":"New - ","Chair":"New - "},"Other office":{"Floor":"New - ","Walls":"New - ","Door":"New - ","Switches":"New - ","Table":"New - ","Chair":"New - "}}"
}
I am trying to access the json string in the last position (*value) but I cannot access it using $array['*value'] as I get nothing returned. If I var_dump($array['*value']) I get NULL. Has anyone any idea why $array['*value'] does not give me the string I require?
Use
array_values(array_slice($array, -1))[0];
to access the last element of the array $array.
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
I have the following code:
$response = $client->get_json($query);
var_dump($response);
which shows me this in a browser:
object(stdClass)#8 (7) {
["search_metadata"]=>
object(stdClass)#7 (8) {
["id"]=>
string(24) "5fa06b463ffd1f75b14c1b98"
["status"]=>
...
}
["search_parameters"]=>
object(stdClass)#9 (9) {
["engine"]=>
string(6) "google"
["q"]=>
...
}
["inline_images"]=>
array(10) {
[0]=>
object(stdClass)#11 (2) {
["link"]=>
string(231) "/search?q=796714619071&num=30&gl=us&hl=en&tbm=isch&source=iu&ictx=1&fir=iYsIHfZTBqNwZM%252Cfl2S346slZj2tM%252C_&vet=1&usg=AI4_-kRkH4vMP2OKxQ8Mz6SJlNoImL7gPg&sa=X&ved=2ahUKEwiCzK_n2OTsAhUKWa0KHQauCKkQ9QF6BAgHEAY#imgrc=iYsIHfZTBqNwZM"
["thumbnail"]=>
....
I'm trying to access inline_images with the following PHP code:
$inlineImages = $response['inline_images'];
But whenever I add this line I'm getting a HTTP 500 error. What am I doing wrong? Why can't I see to access inline_images? Commenting the line out makes the page appear properly.
It's a property on an object, so:
$inlineImages = $response->inline_images;
That's an array of objects, so you can loop through them like this:
foreach ($inlineImages as $inlineImage) {
$link = $inlineImage->link;
}
This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 3 years ago.
I need to write a foreach using these user's ids:
The array that the API returns:
{"users":[{"id":"14"},{"id":"19"}]}
I want to send a mail based on each user id, thats why I need a foreach statement. How do I do that?
Maybe, here we could first json_decode, then loop through the users and append a username maybe using id values to the array:
$str = '{"users":[{"id":"14"},{"id":"19"}]}';
$array = json_decode($str, true);
foreach ($array["users"] as $key => $value) {
$array["users"][$key]["username"] = "user_" . $value["id"];
}
var_dump($array);
Output
array(1) {
["users"]=>
array(2) {
[0]=>
array(2) {
["id"]=>
string(2) "14"
["username"]=>
string(7) "user_14"
}
[1]=>
array(2) {
["id"]=>
string(2) "19"
["username"]=>
string(7) "user_19"
}
}
}
This question already has answers here:
PHP how to retrieve array values
(3 answers)
Closed 4 years ago.
array(11) { ["statusCode"]=> string(2) "OK" ["statusMessage"]=> string(0) "" ["ipAddress"]=> string(13) "183.82.100.13" ["countryCode"]=> string(2) "IN" ["countryName"]=> string(5) "India" ["regionName"]=> string(9) "Telangana" ["cityName"]=> string(9) "Hyderabad" ["zipCode"]=> string(6) "500018" ["latitude"]=> string(7) "17.3753" ["longitude"]=> string(7) "78.4744" ["timeZone"]=> string(6) "+05:30" }
i want to display zipcode only
By the look of the output you might have used the var_dump statement. Any array item can be accessed in the below manner:
echo $array['zipCode'];
Not quite sure how your code looks but if $array is that array you have printed use
echo $array["zipCode"];
You can learn more about how arrays work here http://php.net/manual/en/language.types.array.php
Please try to do your own research before posting a question though as this is a fairly standard part of the language
This question already has answers here:
PHP foreach change original array values [duplicate]
(5 answers)
Closed 5 years ago.
I tried to add a key and its in a 2D array using a foreach. The problem is that this key isn't "saved". As soon as I try to look the first array, the key I added disappeared.
There is the code :
$Etapes=$this::getEtapes();
foreach($Etapes as $Etape){
$req = $this::getSuiviEtapes();
$Etape['Nom_Suivi'] = $req[0]['Nom_Suivi'];
if($Etape['ID_Etat_Etape']=="22")
{
var_dump($Etape);
var_dump($Etapes);
$this->Etapes=$Etapes;
var_dump($this->Etapes);
}
}
And there is the return
array(3) {
["ID_Etat_Etape"]=>
string(2) "22"
["Nom"]=>
string(36) "Comparatif"
["Nom_Suivi"]=>
string(8) "En_cours"
}
array(2) {
[0]=>
array(2) {
["ID_Etat_Etape"]=>
string(2) "21"
["Nom"]=>
string(12) "Etude"
}
[1]=>
array(2) {
["ID_Etat_Etape"]=>
string(2) "22"
["Nom"]=>
string(36) "Comparatif"
}
}
array(2) {
[0]=>
array(2) {
["ID_Etat_Etape"]=>
string(2) "21"
["Nom"]=>
string(12) "Etude"
}
[1]=>
array(2) {
["ID_Etat_Etape"]=>
string(2) "22"
["Nom"]=>
string(36) "Comparatif"
}
}
as you can see, the "Nom_Suivi" key do not appear in the second and third array.
I don't know if my issue is clear enough. Ask if it isn't.
Thank you for helping.
If you want to modify array you are iterating over with foreach, you either have to use reference - foreach($Etapes as &$Etape) or (prefered way) change the value by using the original array variable and key:
foreach($Etapes as $key => $Etape){
...
$Etapes[$key]['Nom_Suivi'] = $req[0]['Nom_Suivi'];
...
}
Try using
foreach ($fields as $key => $field)
Use the key
Check out this Duplicate of this question
This question already has answers here:
Return PHP object by index number (not name)
(5 answers)
Closed 7 years ago.
How do I access the following stdclass, and echo Transaction ID has already been used.
object(stdClass)#2 (1) {
["SendBulkSMS_PHPResult"]=>
object(stdClass)#3 (1) {
["string"]=>
array(3) {
[0]=>
string(1) "0"
[1]=>
string(37) "Transaction ID has already been used."
[2]=>
NULL
}
}
}
From the look of it, it would be:
$object->SendBulkSMS_PHPResult->string[1];