I've a database table called menu_short & I've insert json data in m_short column:
[{"id":1},{"id":2},{"id":3,"children":[{"id":4},{"id":5}]}]
I'm using nestable menu for parent child menu. I use this code to extract:
foreach ($menuDisplay as $menus) {
$json = $menus['m_short'];
$array = json_decode($json, true);
foreach ($array['id'] as $k => $v) {
echo $v;
}
}
I'm getting this 'Undefined index: id & Invalid argument supplied for foreach()' error. I'm sure I made some mistake.
DB Picture-
Something like this should work....
foreach ($menuDisplay as $menus){
$json = $menus['m_short'];
$arrayx = json_decode($json, true);
foreach ($arrayx as $k1 => $array) {
var_dump($array['id']);
/*foreach ($array['id'] as $k2 => $v) {
echo $v;
}*/
}
}
Related
I'm trying to parse JSON data in the format [{code:SE rate:1.294},{code:UK rate:2.353}] from this page:
http://www.mycurrency.net/service/rates
I have implemented an IP reader that detects the users location in a 2 letter country code. I want to pluck the correct data from that link with 'code' and return the value 'rate'. I was thinking I might have to do a foreach loop to iterate through all the countries?
This is my code, I hope this is what are you looking for.
First I create a new array $output to make it more easy to search
$string = file_get_contents("http://www.mycurrency.net/service/rates");
$json = json_decode($string, true);
foreach ($json as $key => $data) {
$output[$key]['code'] = $data['code'];
$output[$key]['rate'] = $data['rate'];
}
After that we use a function to search value in array and returning the key. I got it from here
function searchForRate($countryCode, $array) {
foreach ($array as $key => $val) {
if ($val['code'] === $countryCode) {
return $key;
}
}
return null;
}
and then I run the function with the first parameter as country code to get the keys of specific country code.
$find = searchForRate("BT", $output);
And then echo the rates from our $output array by key in $find variable
echo 'RATE = '.$output[$find]['rate'];
This is the complete codes
<?php
$string = file_get_contents("http://www.mycurrency.net/service/rates");
$json = json_decode($string, true);
foreach ($json as $key => $data) {
$output[$key]['code'] = $data['code'];
$output[$key]['rate'] = $data['rate'];
}
function searchForRate($countryCode, $array) {
foreach ($array as $key => $val) {
if ($val['code'] === $countryCode) {
return $key;
}
}
return null;
}
$find = searchForRate("BT", $output);
echo 'RATE = '.$output[$find]['rate'];
Example output:
RATE = 64.13
I want to do the same actions thru some variables. So I created the variables of variables. But it is throwing me error - "Invalid argument supplied for foreach()" when I am looping thru $$a. I have check the type of the variable. It is array. Then what is the error ?
$edu_data = Json::decode($model->education);
$exp_data = Json::decode($model->experience);
$avail_data = Json::decode($model->availability);
$docs_data = Json::decode($model->documents);
$model_edu = new \admin\models\ApplicantEducation();
$model_exp = new \admin\models\ApplicantExperience();
$model_avail = new \admin\models\Availability();
$model_cre = new \admin\models\Credential();
$all = array('edu_data' => 'model_edu', 'exp_data' => 'model_exp', 'avail_data' => 'model_avail', 'docs_data' => 'model_cre');
foreach ($all as $a => $s)
{
$arr = $$a;
foreach ($arr as $v)
{
$$s->applicant_id = $applicant_id;
foreach ($arr[1] as $k1 => $v1)
{
$$s->$k1 = $v[$k1];
}
$$s->save();
}
}
Your array does not contain your variables (e.g. $model_edu), but only their respective names as string values ('model_edu'). Edit: My bad, I didn't notice this is intentional.
I suggest using a function:
function process_data($model, $data, $applicant_id) {
foreach ($data as $v) {
$model->applicant_id = $applicant_id;
foreach ($data[1] as $k1 => $v1)
{
$model->$k1 = $v[$k1];
}
$model->save();
}
}
process_data($model_edu, $edu_data);
process_data($model_exp, $exp_data);
process_data($model_avail, $avail_data);
process_data($model_docs, $docs_data);
Your code will be more easily comprehendable.
Apart from that, you can debug your code like this to find out exactly where and when the error happens:
foreach ($all as $a => $s)
{
$arr = $$a;
var_dump($arr);
foreach ($arr as $v)
{
$$s->applicant_id = $applicant_id;
var_dump($arr[1]);
foreach ($arr[1] as $k1 => $v1)
{
$$s->$k1 = $v[$k1];
}
$$s->save();
}
}
See if this is the expected value and proceed from there on.
Find out if the reason is an unexpected value in one of your variables or if it is an error in the code logic.
I am trying to handle a situation with a nested multiple array that is received in PHP by $_POST from Javascript-Jquery (as Object not as Json)
. The nested Object looks like this:
{
"Videotheck":{
{
"Category":"Comedy",
"Title_Liste":[
{
"Title":"Millers",
"Year":"2014"
},
{
"Title":"Yogi",
"Year":"2012"
}
]
},
{
"Category":"Action",
"Title_Liste":[
{
"Title":"Rodulf",
"Year":"2014"
},
{
"Title":"Matrix",
"Year":"2000"
}
]
}
}
}
And now the information in this Object need to be splited. For example the title list of each category should be stored in a var
$comedy_title_liste = [];
$action_title_liste = [];
I tryed this:
if($_POST){
$arr1 = $_POST['Videotheck'];
foreach($arr1 as $vtk){
foreach($vtk as $data => $v){
foreach($v as $key => $value){
foreach($value as $k => $info){
echo $k.' '. $info;
}
}
}
}
}
Like this I can get only all title list from all categories, but is necessary to get for each category the list of titles separeted. I don't know really how to handle the situation.
Well this is what I have. I guest that there is something not correct.
Not 100% exact but you can give a try :
$result = array();
$parent = $_POST['Videotheck'];
foreach($parent as $key=> $child) {
$result[$child['Category']."_title_liste"] = array();
foreach($child['Title_Liste'] as $cKey => $val) {
$result[$child['Category']."_title_liste"][] = $val['Title'];
}
}
I have the following error in the code below.
Warning: Invalid argument supplied for foreach() in
/admin/controller/module/megamenu.php on line 263 The list was updated
15-05-27 23:25:14!
Line 263 : foreach ($jsonArray as $subArray) {
I checked on another server and does not appear this error (php5.4) but my server with php5.3, php5.5 appear. What's missing?
if (isset($_GET['jsonstring'])) {
if($this->validate()){
$jsonstring = $_GET['jsonstring'];
$jsonDecoded = json_decode(html_entity_decode($jsonstring));
function parseJsonArray($jsonArray, $parentID = 0) {
$return = array();
foreach ($jsonArray as $subArray) {
$returnSubSubArray = array();
if (isset($subArray->children)) {
$returnSubSubArray = parseJsonArray($subArray->children, $subArray->id);
}
$return[] = array('id' => $subArray->id, 'parentID' => $parentID);
$return = array_merge($return, $returnSubSubArray);
}
return $return;
}
$readbleArray = parseJsonArray($jsonDecoded);
foreach ($readbleArray as $key => $value) {
if (is_array($value)) {
$this->model_menu_megamenu->save_rang($value['parentID'], $value['id'], $key, $data['active_module_id']);
}
}
die("The list was updated ".date("y-m-d H:i:s")."!");
} else {
die($this->language->get('error_permission'));
}
}
Change
$jsonDecoded = json_decode(html_entity_decode($jsonstring));
to
$jsonDecoded = json_decode(html_entity_decode($jsonstring),true);
and $jsonDecoded will be an array
i think i some cases we have to use implode and explode functions when we work with json data in php, try this after decoding your json data to convert an string to an array.
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.