How to get array values from a JSON file in PHP? [duplicate] - php

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"
}
}
}

Related

How do I access a specific key in this PHP object? [duplicate]

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;
}

add 2D array key php [duplicate]

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

Cannot get value in associative array [duplicate]

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.

Trouble getting data from an array in PHP

I've got this array:
array(1) {
["sensory-evaluation"]=> array(6) {
["name"]=> string(18) "Sensory Evaluation"
["value"]=> string(43) "10/22/2015 at 6:00pm | 11/25/2015 at 6:00pm"
["position"]=> string(1) "3"
["is_visible"]=> int(1)
["is_variation"]=> int(1)
["is_taxonomy"]=> int(0)
}
}
I need to be able to get the data from ["value"]. If I do $arr["sensory-evaluation"]["value"] I can get it but the problem is ["sensory-evaluation"] will be different for each element in my array so I need a way to abstract that part but I haven't been able to figure it out.
If you have only one item in the array as you show then:
echo current($arr)['value'];
If you don't know if it exists:
if(isset(current($arr)['value'])) {
echo current($arr)['value'];
}
You could also do:
echo array_values($arr)[0]['value'];
why not use a for each
function getValue($arr){
foreach ($arr as $key => $value) {
var $subArray=$arr[$key];
if( array_key_exists ('value' ,$subArray))
return $arr[$key]['value'];
}
}

Get Value From JSON decode in PHP [duplicate]

This question already has answers here:
How to loop through an array of objects that have been decoded from JSON in PHP, and echo the values
(6 answers)
Closed 7 years ago.
I have tried but still not working fine. I have one array that i get from JSON decode in PHP file, I use Ajax for send this array from javascript, this is how i get array.
$q = json_decode($_GET['q'], true);
I do var_dump this variable and this is the result:
array(2)
{ [0]=> array(1)
{ ["data"]=> array(2)
{ ["Text1"]=> string(1) "Car 1" ["Text2"]=> string(1) "Car 2" }
}
[1]=> array(1)
{ ["data"]=> array(2)
{ ["Text1"]=> string(1) "Car 3" ["Text2"]=> string(1) "Car 4" }
}
}
My question is, how do i get value like "Car 1" or "Car 2" etc from this array? this array like 2 dimensional array, i difficult to get this value. i have found many post related it, i try, but still not solved. Really need help please..
try this,
foreach($pass_your_array as $ta)
{
if(isset($ta['data']))
{
foreach ($ta['data'] as $text)
{
$txt1 = $text['Text1']; // gives car1
$txt2 = $text['Text2']; // gives car2
}
}
}

Categories