I have a multidimensional array as below:
$rows[] = $row;
Now I want to create variable from looping this array. This is how I tried it:
foreach ($rows as $k => $value) {
//echo '<pre>',print_r($value).'</pre>';
$id = $value['news_id'];
$title = $value['news_title'];
echo $title;
}
But it produce an error as below:
...... Illegal string offset 'news_id'
This is the output of - echo '<pre>',print_r($value).'</pre>';
Array
(
[news_id] => 1110
[news_title] => test
[news] => test des
)
1
Array
(
[news_id] => 1109
[news_title] => ශ්රී ලංකාවේ ප්රථම....
[news] => දහසක් බාධක....
)
1
Can anybody tell me what is the wrong I have done?
UPDATE
output for echo '<pre>',print_r($rows).'</pre>';
Array
(
[0] =>
[1] => Array
(
[news_id] => 1110
[news_title] => test
[news] => test des
)
[2] => Array
(
[news_id] => 1109
[news_title] => ශ්රී ලංකාවේ ප්රථම....
[news] => දහසක් බාධක....
)
)
1
use isset function because your 0 index is empty in $row
foreach ($rows as $k => $value) {
if(isset($value['news_id'])){
$id = $value['news_id'];
$title = $value['news_title'];
echo $title;
}
}
you should add check (condition) when you assign data to $rows
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
Here is the output of some complex multidimensional array:
Array (
[0] => Array (
[#attributes] => Array ( [ID] => 114037469 [Name] => MJBDESIGN.ORG [Traffic] => 4 [BidCount] => 0 [Price] => $8 USD [ValuationPrice] => - [TimeLeft] => 3H 27M [RowID] => 1 )
)
[1] => Array (
[#attributes] => Array ( [ID] => 114136929 [Name] => TRAININGPROGRAMMEDESIGN.COM [Traffic] => 11 [BidCount] => 0 [Price] => $8 USD [ValuationPrice] => $3 USD [TimeLeft] => 3H 27M [RowID] => 2 )
)
)
How to iterate through each item and get the 'ID', 'Name' and etc of individual item? Thanks
This array is not so much complex.
$simpleArray = __YOUR_COMPLEX_ARRAY__;
foreach ($simpleArray as $array) {
$id = $array['#attributes']['ID'];
$name = $array['#attributes']['Name'];
}
foreach ($array as $r) {
$name = $r['#attributes']['Name'];
$id = $r['#attributes']['ID'];
# code ....
}
If you would like all attributes of each row:
foreach ($array as $n=>$r){
echo "Row {$n}\n";
foreach ($r as $k=>$v){
// Echo the data
echo "{$k}=>${v}\n";
}
}
Just easy way:
$array = // your long array;
foreach($array as $row)
{
$id = $row['#attributes']['Id'];
$name = $row['attributes']['Name'];
// do something with these variables
}
Try it like,
foreach ($yourarray as $val) {
echo $id = $val['#attributes']['ID'];
echo $name = $val['#attributes']['Name'];
}
How can I loop through an array like this and retrieve the id and echo it to the screen? Also how can I do a loop and find the one with the highest id?
Array
(
[articles] => Array
(
[0] => Array
(
[id] => 650
)
[1] => Array
(
[id] => 649
)
[2] => Array
(
[id] => 645
)
[3] => Array
(
[id] => 399
)
);
You can do this with foreach
foreach ($array['articles'] as $value)
{
echo "Id is: ".$value['id'];
}
And you can get with max() function:
foreach($array['articles'] as $article)
{
$ids[] = $article['id'];
}
echo "Max Id is: ".max($ids);
Or you can do get value and max id with one foreach.
foreach($array['articles'] as $article)
{
echo "Id is: ".$article['id'];
$ids[] = $article['id'];
}
echo "Max Id is: ".max($ids);
Say $arr['articles'] contains your array.Then using a foreach you can loop through the array and just echo it.
$arr = array('articles' => array(
'0' => array('id' => 650),
'1' => array('id' => 649),
'2' => array('id' => 645),
'3' => array('id' => 399)
)
);
foreach($arr['articles'] as $val){
echo $val['id'].'</br>';
}
Try
foreach ($arrayvar['articles'] as $value)
{
echo $value['id']."<br>";
}