This is my json Output. i want to echo 'resolution' only. How is that possible?
Array
(
[uploader] => CoversDamian
[formats] => Array
(
[0] => Array
(
[preference] => -50
[resolution] => 720p
)
[1] => Array
(
[preference] => -100
[resolution] => 1080p
)
)
)
In the case you want to echo all resolutions you need a loop like this:
for ($i = 0; $i < sizeof($array["formats"]); $i++){
echo $array["formats"][$i]["resolution"];
}
Hope it helps!
Assuming the json is in object notation, stored as $json you can loop through the 'formats' as follows :
foreach($json->formats as $key=>$value) {
echo $value->resolution . "\n";
}
If it is not in object notation, you can loop through the sub-keys in the array (assuming it is store in the variable $json) as follows :
foreach($json['formats'] as $key=>$value) {
echo $value['resolution'] . "\n";
}
Notice the subtle difference in the way you can access sub-keys/elements in an object vs in an array.
$arr = array(
'uploader' => 'CoversDamian',
'formats' => array
(
0 => array
(
'preference' => '-50',
'resolution' => '720p'
),
1 => array
(
'preference' => -'100',
'resolution' => '1080p'
)
)
);
foreach ($arr['formats'] as $key=>$val) {
echo $val['resolution'];
}
$myarray = Array
(
[uploader] => CoversDamian
[formats] => Array
(
[0] => Array
(
[preference] => -50
[resolution] => 720p
)
[1] => Array
(
[preference] => -100
[resolution] => 1080p
)
)
)
echo $myarray["formats"][1]["resolution"];
if you have more array in formats key then you can use foreach loop based on formats key. Becuase you want to print all resolution under the formats key.
So
foreach($myarray["formats"] as $key => $value){
echo $value["resolution"]."<br>";
}
Related
I am parsing A JSON file in PHP using PHP Decode
$json= file_get_contents ("resultate.json");
$json = json_decode($json, true);
and then I am trying to loop through the results and display them
foreach($json as $zh){
$name = $zh[0]["VORLAGEN"][0]["JA_PROZENT"];
$JA = $zh[0]["VORLAGEN"][0]["VORLAGE_NAME"];
$kreis = $zh[0]["NAME"];
$kreis1 = $zh[22]["NAME"];
echo $name;
echo $JA;
echo $kreis;
echo $kreis1;
...}
but I receive only one element e.g. position 0 or position 22. I would like to receive all the results and display them in a list. Below you can see the array
Array
(
[GEBIETE] => Array
(
[0] => Array
(
[VORLAGEN] => Array
(
[0] => Array
(
[VORLAGE_NAME] => Kantonale Volksabstimmung über die Vorlage Steuergesetz (StG) (Änderung vom 1. April 2019; Steuervorlage 17)
[JA_STIMMEN_ABSOLUT] => 205
[STIMMBETEILIGUNG] => 28.11
[NEIN_STIMMEN_ABSOLUT] => 183
[VORLAGE_ID] => 2491
[JA_PROZENT] => 52.84
)
)
[NAME] => Aeugst a.A.
[WAHLKREIS] => 0
[BFS] => 1
)
[1] => Array
(
[VORLAGEN] => Array
(
[0] => Array
(
[VORLAGE_NAME] => Kantonale Volksabstimmung über die Vorlage Steuergesetz (StG) (Änderung vom 1. April 2019; Steuervorlage 17)
[JA_STIMMEN_ABSOLUT] => 1000
[STIMMBETEILIGUNG] => 26.15
[NEIN_STIMMEN_ABSOLUT] => 851
[VORLAGE_ID] => 2491
[JA_PROZENT] => 54.02
)
)
[NAME] => Affoltern a.A.
[WAHLKREIS] => 0
[BFS] => 2
)
[2] => Array
(
[VORLAGEN] => Array
(
[0] => Array
(
[VORLAGE_NAME] => Kantonale Volksabstimmung über die Vorlage Steuergesetz (StG) (Änderung vom 1. April 2019; Steuervorlage 17)
[JA_STIMMEN_ABSOLUT] => 661
[STIMMBETEILIGUNG] => 30.98
[NEIN_STIMMEN_ABSOLUT] => 454
[VORLAGE_ID] => 2491
[JA_PROZENT] => 59.28
)
)
[NAME] => Bonstetten
[WAHLKREIS] => 0
[BFS] => 3
)
can you please tell me how to print all the elements of this array?
There is GEBIETE layer in your json, so change foreach($json as $zh) to foreach($json["GEBIETE"] as $zh) will works.
You can nest multiple foreach:
$root = $json['GEBIETE'];
foreach ($root as $elem) {
foreach ($elem as $key => $value) {
// VORLAGEN is an array of its own so if you want to print the keys you should foreach this value as well
if(is_array($value)) {
foreach ($value[0] as $k => $v) {
echo $k."\t".$v;
}
}
else {
echo $value;
}
}
}
I have this Array but i don't know how to get the [discount_amount] based on the [object_ids].
For example i would like to get the 93 value if my [object_ids] contain 81.
Array
(
[0] => Array
(
[id] => rule_5b0d40cd1408a
[membership_plan_id] => 106
[active] => yes
[rule_type] => purchasing_discount
[content_type] => post_type
[content_type_name] => product
[object_ids] => Array
(
[0] => 81
)
[discount_type] => amount
[discount_amount] => 93
[access_type] =>
[access_schedule] => immediate
[access_schedule_exclude_trial] =>
)
[1] => Array
(
[id] => rule_5b0d4e0f3b0b4
[membership_plan_id] => 106
[active] => yes
[rule_type] => purchasing_discount
[content_type] => post_type
[content_type_name] => product
[object_ids] => Array
(
[0] => 110
)
[discount_type] => amount
[discount_amount] => 50
[access_type] =>
[access_schedule] => immediate
[access_schedule_exclude_trial] =>
)
)
You could use a foreach and use in_array to check if the array object_ids contains 81.
foreach ($arrays as $array) {
if (in_array(81, $array["object_ids"])) {
echo $array["discount_amount"];
}
}
Demo
Try with this code .
Assuming $dataArray is the array you have printed.
foreach ($dataArray as $key => $value){
if($value['object_ids'][0] == 83){
$discount_amount = $value['discount_amount'];
}
}
echo $discount_amount
The way I mostly do this is as followed:
# Save retrieved data in array if you want to.
$test = array();
foreach($array as $line){
# Count the row where discount_amount is found.
$test[] = $line['9'];
echo "Result: ".$line['9']. "<br \>\n";
# OR another method is
$test[] = $line['discount_amount'];
echo "Result: ".$line['discount_amount']. "<br \>\n";
}
This has probably been asked many times but I can't find a solution for my case.
This is my array :
$request=Array (
[0] => Array ( [staName] => Auditorium Stravinsky 2m2c )
[1] => Array ( [staName] => Geneva Arena )
[2] => Array ( [staName] => Les Docks )
[3] => Array ( [staName] => Kheops )
)
And i need an output as follows as JSON:
"Auditorium Stravinsky 2m2c ","Geneva Arena","Les Docks","Kheops"
My current code is as follows:
foreach($request as $value)
{
$names[]=$value;
}
$jsonValue = json_encode(array_values($names));
print_r($jsonValue);
And my current output is as follows in JSON format:
[{"staName":"Auditorium Stravinsky 2m2c "},{"staName":"Geneva Arena"},{"staName":"Les Docks"},{"staName":"Kheops"}]
How can i stop "staName " from being outputed?
Many thanks in advance and please be considerate of my post as this is only the second one I make on this site.
<?php
$request=Array (
0 => Array ( 'staName' => 'Auditorium Stravinsky 2m2c' ) ,
1 => Array ( 'staName' => 'Geneva Arena' ) ,
2 => Array ( 'staName' => 'Les Docks' ) ,
3 => Array ( 'staName' => 'Kheops' )
);
$newArray=array();
for($i=0;$i<count($request);$i++){
$newArray[$i]=$request[$i]['staName'];
}
$newArray=json_encode($newArray,true);
print_r($newArray);
And the output is a merged json:
["Auditorium Stravinsky 2m2c","Geneva Arena","Les Docks","Kheops"]
You can achieve this from,
Code
$a = array();
foreach($request as $key =>$val){
foreach($val as $k => $v){
$a[] = $v;
}
}
print_r(json_encode($a));
Check this Demo link
Output
["Auditorium Stravinsky 2m2c","Geneva Arena","Les Docks","Kheops"]
fist of all your array definition are not correct. and your output is simple string not a JSON
<?php
$request=Array (
0 => Array ( 'staName' => 'Auditorium Stravinsky 2m2c' ),
1 => Array ( 'staName' => 'Geneva Arena' ) ,
2 => Array ( 'staName' => 'Les Docks' ) ,
3 => Array ( 'staName' => 'Kheops' )
);
$name = '';
foreach($request as $value)
{
foreach($value as $value2)
{
$name = $name . ' ' . $value2;
}
}
echo $name;
Output
Auditorium Stravinsky 2m2c Geneva Arena Les Docks Kheops
I have array like this:
Array
(
[Tarik Oil] => Array
(
[Eurodizel] => 5
)
[INA] => Array
(
[Eurodizel] => 10
)
[HIFA] => Array
(
[Eurodizel] => 15
)
[Selex] => Array
(
[Eurodizel] => 1.96
)
)
I want to get min and max values of 'Eurodize'.
How I can do this at easiest way in PHP.
Thanks
I assume, that in reality you have something like
Array
(
[Tarik Oil] => Array
(
[Eurodizel] => 5,
[Super] => 6,
[LNG] => 4,
)
...
)
And will want to run the same query for the other products. If this is the case, you should create an orthognal array like
$newarray=array(
[Eurodizel] => array(),
[Super] => array(),
[LNG] => array(),
);
foreach ($yourarray as $k=>$v) {
foreach ($v as $kk=>$vv)
$newarry[$kk][$k]=$vv;
}
foreach ($newarray as $k=>$v)
asort($newarry[$k]);
Now you can find the cheapest Eurodizel with
$eurodizel=array_values($newarray['Eurodizel']);
$cheapest=$eurodizel[0];
Here is a function that may help with what you're looking for:
<?php
function getMax($array){
foreach($array as $key=>$value){
if(!isset($max)||$value['Eurodizel']>$max){$max=$value['Eurodizel'];}
}
return $max;
}
?>
I have array like this. This array is created dynamically
Array
(
[Data NotUploading] => Array
(
[items] => Array
(
[0] => Array
(
[date] => 2013-04-02
[issue_id] => 1
[phone_service_device] => A
[phone_service_model] =>
[phone_service_os] =>
[phone_service_version] =>
[issue_name] => Data NotUploading
)
[1] => Array
(
[date] => 2013-04-02
[issue_id] => 1
[phone_service_device] => I
[phone_service_model] =>
[phone_service_os] =>
[phone_service_version] =>
[issue_name] => Data NotUploading
)
)
)
[Battery Problem] => Array
(
[items] => Array
(
[0] => Array
(
[date] => 2013-04-03
[issue_id] => 3
[phone_service_device] => I
[phone_service_model] =>
[phone_service_os] =>
[phone_service_version] =>
[issue_name] => Battery Problem
)
)
)
)
What I need to do is to use 2 foreach or 1 foreach & 1 for loop so that I can get each value of date I did like this
foreach($gResultbyName as $key1 => $rec){
for($j = 0;$j<count($rec);$j++ ){
echo $rec['items'][$j]['date'];
}
}
but its only retrieving 2013-04-02 & 2013-04-03 that is 0 index date of data NotUploading & 0 index date of Battery Problem. Basically I need to compare each value and others stuff but I just cant get each date value.
Forgive me for ignorance but I tried a lot :(
try this:
foreach ($data as $d)
{
foreach($d['items'] as $item)
{
echo $item['date'];
}
}
Your for-loop loops count($rec) times, but should count($rec['items']).
This should load the dates into an array sorted by the issue_name. The you can step through them for further processing.
$dates= array();
foreach ($gResultbyName AS $events){
foreach ($events['items'] AS $item){
$dates[$item['issue_name']][] = $item['date'];
}
}
var_dump($dates);