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
}
}
}
Related
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:
How to check if a specific value exists at a specific key in any subarray of a multidimensional array?
(17 answers)
Closed 4 years ago.
array(7) {
[0]=>
array(2) {
["name"]=>
string(14) "form[username]"
["value"]=>
string(1) "1"
}
[1]=>
array(2) {
["name"]=>
string(15) "form[is_active]"
["value"]=>
string(1) "1"
}
[2]=>
array(2) {
["name"]=>
string(8) "form[id]"
["value"]=>
string(1) "9"
}
}
I want to get the id from an array. The output I like to achive is 9.
My approach:
echo $array['form[id]'];
But I don't get an output.
When you use $array['form[id]']; you are looking for the key called 'form[id]' which will not work because the keys of your array are 0, 1 and 2. You can get your desired value by using $array[2]['value']. However this will always call the 2nd element of your array, which might not be what you want. A more dynamic solution would be something like this:
foreach ($array as $element) {
if ($element['name'] == 'form[id]') {
echo $element['value'];
break;
}
}
This will loop through your whole array and check the names of each element. Then when it matches your desired name it will print the value for that exact element.
The easiest way might be to just first re-index the array using array_column. Then you can use the name field as the key:
$array = array_column($array, null, 'name');
echo $arr['form[id]']['value'];
// 9
See https://3v4l.org/L1gLR
You could use a foreach and check for the content .. but the content for index 'name' is just a string form[id]
anyway
foreach( $myArray AS $key => $value){
if ($value['name'] == 'form[id]' ) {
echo $key;
echo $value;
}
}
You are trying to get the value as if it's an associative array (sometimes called a dictionary or map), however it's a plain or indexed array.
Get the value you want by calling $array[2]["value"]
You can also use some of the higher level functions such as array_search; then you could use:
$id = array_search(function($values) {
return $values['name'] == 'form[id]';
}, $array)["value"];
So I think you need to filter the array to find the element you need first, then output that element's value:
$filtered_array = array_filter($your_array, function(element){
return element['name'] == 'form[username]';
});
if (!empty($filtered_array)) {
echo array_pop($filtered_array)['value'];
}
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
I have this JSON response:
object(stdClass)#1 (2) {
["data"]=>
array(47) {
[0]=>
object(stdClass)#2 (4) {
["id"]=>
int(341)
["competition_id"]=>
int(1)
["name"]=>
string(9) "2015/2016"
["active"]=>
bool(true)
But I don't know how to parse the data.
I tried it with this PHP code:
echo $json->data[0]->id;
But it doesn't work. How can I get the ID for example?
here is my solution:
$json = file_get_contents($url);
$json2 = json_decode($json);
echo $json2->data[0]->id;
sorry for complication, the goal was the json_decode() function, after that I can get the data with "->"
greetings!
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.
Using PHP & codeigniter.
I posting below to my PHP api
submitting from front end - > {"academicYear":"2015","subjectClassification":"Primary","subjectId":"55","teacherId":[10,16,17]}
I need to find or print teacherId values in my PHP code.
Basically my target is to print "HELLO" 3 time if there is 3 Id in teacherId array.
My code look like below,
function subjectTeacherAllocation_post(){
$data = remove_unknown_fields($this->post() ,$this->form_validation->get_field_names('subjectTeacherAllocation_post'));
$this->form_validation->set_data($data);
var_dump($data);
$teacherList = array($data['teacherId']);
echo $teacherList[0];
echo array_values($teacherList);
var_dump output --> array(3) { ["academicYear"]=> string(4) "2014" ["subjectId"]=> string(2) "55" ["teacherId"]=> array(3) { [0]=> int(9) [1]=> int(15) [2]=> int(32) } }
You are needlessly wrapping $data['teacherId'] in an extra array, instead just do:
$teacherList = $data['teacherId'];
echo $teacherList[0]; //9
Specifically, to produce hello x number of times, where x is the number of elements in the above $teacherList array:
foreach($teacherList as $unused){
echo 'hello';
}