I have an array $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);. How can i get (echo) all names and corresponding ages in a single instance (like foreach $value as $value)?? The array may have more data than shown here.
foreach ($ages as $name => $age) {
echo "$name = $age\n";
}
u have a lot options
like
print_r
print_r($array)
var_dump , foreach
print_r($array);
or
var_dump($array)
foreach ($ages as $key => $value) {
echo $key . "'s age is " . $value . "<br />";
}
Related
this is my code, I want to echo all the data inside this $json.
$json = '{"year":"2018","month":"4","name":"Apr 2018 - Sept 2018","description":null,"code":"2018\/04","session_from_at":{"date":"2018-04-16 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"session_to_at":{"date":"2018-09-21 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"registered_from_at":{"date":"2018-04-16 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"registered_to_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"added_dropped_from_at":null,"added_dropped_to_at":null,"withdrew_from_at":null,"withdrew_to_at":null,"attended_from_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"attended_to_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"cw_marked_from_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"cw_marked_to_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"fe_marked_from_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"fe_marked_to_at":{"date":"2018-09-21 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"created_by":1,"updated_by":1,"id":2}';
$arr = json_decode($json,true);
foreach($arr as $key=>$value){
echo $key . "<br>";
echo $value . "<br>"; // PHP Notice: Array to string conversion in /workspace/Main.php on line 11
// not displaying the value
}
?>
the problem come went the data loop on session_from_at which have three data in array inside.
I think you are not using blade. Your file name is Main.php
If you are using blade, I advise you to use #foreach instead of foreach.
And after json_decode, you still have an array inside array.
Try this code:
$json = '{"year":"2018","month":"4","name":"Apr 2018 - Sept 2018","description":null,"code":"2018\/04","session_from_at":{"date":"2018-04-16 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"session_to_at":{"date":"2018-09-21 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"registered_from_at":{"date":"2018-04-16 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"registered_to_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"added_dropped_from_at":null,"added_dropped_to_at":null,"withdrew_from_at":null,"withdrew_to_at":null,"attended_from_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"attended_to_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"cw_marked_from_at":{"date":"2018-04-20 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"cw_marked_to_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"fe_marked_from_at":{"date":"2018-04-29 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"fe_marked_to_at":{"date":"2018-09-21 00:00:00.000000","timezone_type":3,"timezone":"Asia\/Singapore"},"created_by":1,"updated_by":1,"id":2}';
$arr = json_decode($json, true);
function echoArray($arr) {
foreach($arr as $key=>$value){
echo $key . "<br>";
if (is_array($value)) {
echoArray($value);
} else {
echo $value . "<br>";
}
}
}
echoArr($arr);
You got an error because other $value is an array you cant echo it so try to check if its array or object like this :
foreach($arr as $key=>$value){
echo $key . "<br>";
if(is_object($value) || is_array($value)){
foreach($value as $key2=>$value2){
echo $key2 . "<br>";
echo $value2 . "<br>";
}
}else{
echo $value . "<br>";
}
}
try it:
$arr['year'];
$arr['month'];
you do not need to use foreach.
So I have this array that saves a json string.
Array
(
[0] => "2jDQoU9D2wu04wqkg0ImUI":{"date":"2016-08-02 14:08:49","type":"story","story_id":"2jDQoU9D2wu04wqkg0ImUI","series_id":"1RAv0uDbcIieYgYqywqYmk"}
)
I want to be able to access just the value "2jDQoU9D2wu04wqkg0ImUI" at the start of the json string and then also be able to do something like $value[0]['type'] to get the type from this json string object. I'm pretty new to PHP and struggling to get this working. I've tried JSON encoding/decoding and can't seem to get anything to work.
What's the proper way to go about this? Thanks in advance.
I hope this code will solve your problem.
$array[0] = '"2jDQoU9D2wu04wqkg0ImUI":{"date":"2016-08-02 14:08:49","type":"story","story_id":"2jDQoU9D2wu04wqkg0ImUI","series_id":"1RAv0uDbcIieYgYqywqYmk"}';
//print_r($arr);
$JsonString = '{' . $array[0] . '}';
$json = json_decode($JsonString);
foreach($json as $key => $value){
echo "Key : $key <br />";
echo "Type : ". $value->type."<br />";
echo "date : ". $value->date."<br />";
echo "story_id : ". $value->story_id."<br />";
echo "series_id : ". $value->series_id."<br />";
}
so you have array $jsonArray and you want to access just the key of it
if its single dimension array :
echo key($jsonArray); //prints 2jDQoU9D2wu04wqkg0ImUI
Otherwise if its multidimensional you can loop through it and do whatever you want with each key
foreach($jsonArray as $key => $value) {
echo $key; //prints 2jDQoU9D2wu04wqkg0ImUI
}
Try this:
$arrayOfObjects = [];
foreach($array as $key => $value) arrayOfObjects[] = json_decode($value);
Now you can loop through this new $arrayOfObjects
foreach($arrayOfObjects as $key => $object){
echo $object->date . '<br>';
echo $object->type . '<br>';
echo $object->story_id . '<br>';
echo $object->series_id . '<br> ==== >br> ';
}
I have two array output (using preg_match_all), for example: $name[1] and $family[1].
i need to put these arrays together, and i use foreach as this:
foreach( $name[1] as $name) {
foreach( $family[1] as $family) {
echo $name.$family.'<br />';
}
}
but it don't work.
(each foreach loop works separately)
Assuming they have matching keys for the loop:
foreach( $name as $key => $value) {
echo $value[$key] . $family[$key] . '<br />';
}
This will go through each match for $name and print it out, and then print out the corresponding $family with it. I don't think you want to hardcode [1].
If you do, I'm a little confused and would like to see a var_dump of both $name and $family for clarification.
$together= array();
foreach( $name as $key => $n) {
$tuple= array('name'=>$name[$key],'family'=>$family[$key]);
$together[$key]= $tuple;
}
foreach( $together as $key => $tuple) {
echo "{$tuple['name']} {$tuple['family']}<br />";
}
Use array_combine():
Creates an array by using the values from the keys array as keys and
the values from the values array as the corresponding values.
PHP CODE:
$nameFamilly=array_combine($name[1] , $family[1]);
foreach( $nameFamilly as $name=>$familly) {
echo $name.$family.'<br />';
}
below is a php object which is retrieving some values from mysql db through a php method
$query = "SELECT imgpath from images";
$oMySQL->ExecuteSQL($query);
Now when i use this
$result=$oMySQL->ExecuteSQL($query);
it prints "Array"
How to iterate through the array
Regards Jane
A simple foreach loop.
foreach ($result as $key => $val) {
foreach ($val as $label => $item) {
echo $label . " - " . $item;
}
}
$key will hold the associative name the array element, and $val will hold the value of the element.
You mean it print array when you do echo $result?
Then:
foreach($result as $index => $value) {
echo $index . '=' $value;
}
I have this array:
$json = json_decode('
{"entries":[
{"id": "29","name":"John", "age":"36"},
{"id": "30","name":"Jack", "age":"23"}
]}
');
and I am looking for a PHP "for each" loop that would retrieve the key names under entries, i.e.:
id
name
age
How can I do this?
Try it
foreach($json->entries as $row) {
foreach($row as $key => $val) {
echo $key . ': ' . $val;
echo '<br>';
}
}
In the $key you shall get the key names and in the val you shal get the values
You could do something like this:
foreach($json->entries as $record){
echo $record->id;
echo $record->name;
echo $record->age;
}
If you pass true as the value for the second parameter in the json_decode function, you'll be able to use the decoded value as an array.
I was not satisfied with other answers so I add my own. I believe the most general approach is:
$array = get_object_vars($json->entries[0]);
foreach($array as $key => $value) {
echo $key . "<br>";
}
where I used entries[0] because you assume that all the elements of the entries array have the same keys.
Have a look at the official documentation for key: http://php.net/manual/en/function.key.php
You could try getting the properties of the object using get_object_vars:
$keys = array();
foreach($json->entries as $entry)
$keys += array_keys(get_object_vars($entry));
print_r($keys);
foreach($json->entries[0] AS $key => $name) {
echo $key;
}
$column_name =[];
foreach($data as $i){
foreach($i as $key => $i){
array_push($column_name, $key);
}
break;
}
Alternative answer using arrays rather than objects - passing true to json_decode will return an array.
$json = '{"entries":[{"id": "29","name":"John", "age":"36"},{"id": "30","name":"Jack", "age":"23"}]}';
$data = json_decode($json, true);
$entries = $data['entries'];
foreach ($entries as $entry) {
$id = $entry['id'];
$name = $entry['name'];
$age = $entry['age'];
printf('%s (ID %d) is %d years old'.PHP_EOL, $name, $id, $age);
}
Tested at https://www.tehplayground.com/17zKeQcNUbFwuRjC