Foreach Loop Echo First Occurrence Only (First Element Only) - php

I've been trying to select the first occurrence in foreach loop. Here's the code I'm using:
<?php
$data = file_get_contents('data.json');
$data = json_decode($data,true);
foreach($data['screenshots'] as $values){
echo $values[0];
}
?>
It's echoing "hhhhhhhhhhhhhhhhhhhhhhhhh" only.
Here's the JSON Data: pastebin.com/WqyJBAbg

this should help out.
$data = file_get_contents('data.json');
$data = json_decode($data,true);
foreach($data['screenshots'] as $index => $value) {
if ($index == 0) {
echo $data['screenshots'][$index];
}
}

Related

How to prevent overriding in array?

I'm trying to add all the keys of different json available in a file to an array. What I did for the moment is this:
//Get the json file content
$jsonData = file(__DIR__ .'/../logs/error.json');
//Save all the json
$json = [];
//Iterate through the line of the file, each line is a json
foreach($jsonData as $line)
{
//Convert the json in an associative array
$array = json_decode($line, true);
//Iterate through the json keys
foreach($array as $k => $val)
{
$json[$k] = $val;
}
}
the json file is like this:
{"Timestamp":"2018-06-14T10:46:52.3326036+02:00","Level":"Error","MessageTemplate":"System.Exception"}
{"Timestamp":"2018-06-14T10:47:22.7493871+02:00","Level":"Error","MessageTemplate":"System.Exception"}
I'll get this:
{"Timestamp":"2018-06-14T10:47:22.7493871+02:00","Level":"Error","MessageTemplate":"System.Exception"}
because the $json[$k] override I guess the previous array, but $k is a new json so why the index of the array is replaced?
Thanks in advance for any help.
may be this one is your expected output.
//Get the json file content
$jsonData = file(__DIR__ .'/../logs/error.json');
//Save all the json
$json = [];
//Iterate through the line of the file, each line is a json
foreach($jsonData as $line)
{
//Convert the json in an associative array
$array = json_decode($line, true);
$temp = [];
//Iterate through the json keys
foreach($array as $k => $val)
{
$temp[$k] = $val;
}
$json[] = $temp;
}
change this line
foreach($array as $k => $val)
{
$json[$k] = $val;
}
to
foreach($array as $k => $val)
{
$json[][$k] = $val;
}
Well you're overwriting keys with the same names, so there's really nothing surprising in your output.
You probably meant to do this:
foreach($jsonData as $line) {
$tmp = []; //<-- set up a new array just for this iteration
$array = json_decode($line, true);
foreach($array as $k => $val) $tmp[$k] = $val;
$json[] = $tmp; //<-- log the array in the master $json array
}
//Get the json file content
$jsonData = file(__DIR__ .'/../logs/error.json');
//Convert the json in an associative array
$array = json_decode($jsonData, true);
//Save all the json
$json = [];
//Iterate through the line of the file, each line is a json
foreach($array as $k => $val)
{
$json[][$k] = $val;
}

get json value from php loop

how can I get the same results/values in one loop rather than two?
$json = file_get_contents($url)
$data = json_decode($json, true);
$desc = $data["descriptions"];
$assets = $data["assets"];
foreach ($assets as $assItem) {
echo $assItem["assetid"];
}
foreach($desc as $descItem) {
echo descItem["name"];
}
I've tried something like
$json = file_get_contents($url);
$data = json_decode($json, true);
foreach ($data as $item) {
echo $item["assets"]["assetid"];
echo $item["descriptions"]["name"];
}
pastebin to the json: https://pastebin.com/raw/uA9mvE2e
You could do something like:
$json = file_get_contents($url);
$data = json_decode($json, true);
foreach ($data['assets'] as $k => $item) {
echo $item["assetid"];
echo $data["descriptions"][$k]["name"];
}
This assumes that $data['assets'] and $data['descriptions'] share the same indices.

how to store values of foreach as array

<?php
$samgri = $this->crud_model->get_puja_samagri_by_puja_order($param1,$param);
foreach ($samgri as $row){
$puja_samagri = $row['puja_samagri'];
}
$puja_sam = explode(',', $puja_samagri);
foreach ($puja_sam as $samagri_id){
$samg = $this->crud_model->get_puja_samagri_by_id($samagri_id);
}
if(sizeof($samg) == 0){
echo '<h5 style = "text-align:center">No samagari available</h5>';
}else{
foreach ($samg as $row){
?>
but it was fetching only the last record not total records
You should add brackets after the array name like:
<?php
$samgri = $this->crud_model->get_puja_samagri_by_puja_order($param1,$param);
//Declare the arrays
$puja_samagri = new array();
$samg = new array();
foreach ($samgri as $row){
$puja_samagri[] = $row['puja_samagri'];
}
$puja_sam = explode(',', $puja_samagri);
foreach ($puja_sam as $samagri_id){
$samg[] = $this->crud_model->get_puja_samagri_by_id($samagri_id);
}
if(sizeof($samg) == 0){
echo '<h5 style = "text-align:center">No samagari available</h5>';
}else{
foreach ($samg as $row){
?>
It will use the automatic int position (from 0 going up untill you have all the results). Also it might be a good idea to declare them as arrays before using them.
You have to take array after every foreach veriable as like follows :
<?php
$samgri = $this->crud_model->get_puja_samagri_by_puja_order($param1,$param);
foreach ($samgri as $row){
$puja_samagri[] = $row['puja_samagri'];
}
$puja_sam = explode(',', $puja_samagri);
foreach ($puja_sam as $samagri_id){
$samg[] = $this->crud_model->get_puja_samagri_by_id($samagri_id);
}
if(sizeof($samg) == 0){
echo '<h5 style = "text-align:center">No samagari available</h5>';
}else{
foreach ($samg as $row){
?>
Hope this will help you :)

trying to get json field value with 3rd foreach inside foreach

I am trying to get value of subscriber id but i am getting php error on 3rd foreach.
$content = '{"12345":{"id":"123","data":{"sort":"desc","subcriber":{"id":321"}}}';
$json = json_decode($content, true);
foreach($json as $row => $val) {
echo $val['id'];
$jdata = $val['data'];
foreach($jdata as $data => $val2) {
echo $val2['sort'];
$jsubcriber = $val2['subcriber'];
foreach($jsubcriber as $subcriber => $val3) {
echo $val3['id'];
}
}
}
It should be:
echo $jsubscriber['id'];
$jsubscriber is an associative array, not an array of associative arrays.

JSON parsing with PHP - most efficient way

I send JSON object to server. On server side I have to parse this obj with PHP.
I'm stuck in the loop. I don't know how to proceed inside loops.
Im looking for most efficient way to parse this object and save all variables into DB.
[
{"ADI":{"id":-1,"danger":0}},
{"ADI":{"id":3,"danger":0}},
{"ADI":{"id":3,"danger":0}},
{"ALE":{"_id":1,"_name":"Milk","contain":false}},
{"ALE":{"_id":2,"_name":"cfg","contain":false}},
{"ALE":{"_id":4,"_name":"Lakt","contain":false}},
{"PRO":{"image":"","code":"123456","name":"jfbj"}},
{"USER":{"email":"spam#spam.com"}}
]
For now I have done this:
$string = file_get_contents('php://input');
$array = json_decode($string, true);
//print_r($array);
foreach ($array as $t => $index) {
foreach ($index as $vas => $r) {
//Here I'm stuck!!!
}
}
you can grab values from json
<?php
$string = '[
{"ADI":{"id":-1,"danger":0}},
{"ADI":{"id":3,"danger":0}},
{"ADI":{"id":3,"danger":0}},
{"ALE":{"_id":1,"_name":"Milk","contain":false}},
{"ALE":{"_id":2,"_name":"cfg","contain":false}},
{"ALE":{"_id":4,"_name":"Lakt","contain":false}},
{"PRO":{"image":"","code":"123456","name":"jfbj"}},
{"USER":{"email":"spam#spam.com"}}
]';
$array = json_decode($string, true);
echo "<pre>";
//print_r($array);exit;
for ($i=0;$i<=(count($array)-1);$i++){
//print_r($array[$i]);
if (array_key_exists("ADI",$array[$i])) {
$ArrVal = $array[$i]['ADI'];
$id = $ArrVal['id'];
$danger = $ArrVal['danger'];
echo "$id,$danger ";
}
}
?>
check this out :
echo "<pre>";
print_r($array);
for ($i=0; $i < count($array); $i++) {
if(isset($array[$i]["ADI"])){
print_r($array[$i]["ADI"]);
}
if(isset($array[$i]["ALE"])){
print_r($array[$i]["ALE"]);
}
}
echo "</pre>";

Categories