Invalid argument supplied for foreach() in file php - php

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.

Related

syntax error in postman after json_encode()

this is my contoller
$pid = json_decode(file_get_contents('php://input'), true);
foreach ($pid as $key => $value) {
foreach ($value as $keys => $values) {
$c_res = $this->Model_master->get_sup($values);
echo json_encode($c_res, 200);
}
}
this is my model
$q = $this->db->select('contact_number')->from('shopxie_people')
->where("p_id = ".$value)
->get();
return $q->result_array();
this shows me result in html form but when i change it to json it gives me
SYntax error
also in postman content-type is showing as text/html;
why does it happen even after encoding in json in my code?
Hope this will help you :
Do something like this in your controller :
foreach ($pid as $key => $value)
{
foreach ($value as $keys => $values)
{
$c_res[] = $this->Model_master->get_sup($values);
}
}
echo json_encode($c_res);
exit;

Invalid argument supplied for foreach() error for array of json objects

I am decoding the array of json objects into html lists . i have tried with some demo it worked but when i deal with this array of json this gives error as : Invalid argument supplied for foreach(). what is i am missing ?
<?php
$json_string = '{"error":false,"data":[{"jb_product_category_id":"1","jb_product_category_name":"Mother","jb_product_category_prefix":"jbpm","jb_product_category_delete_status":"0","jb_product_category_created_on":"1501500876531","jb_product_category_updated_on":"1501500876531","subCategory1":[{"jb_product_subcategory1_1_id":"1","jb_product_subcategory1_2_category_id":"1","jb_product_subcategory1_3_name":"Cloths","jb_product_subcategory1_4_delete_status":"0","jb_product_subcategory1_5_created_on":"1501563015164","jb_product_subcategory1_6_updated_on":"1501563015164","subCategory2":[{"jb_product_subcategory2_1_id":"1","jb_product_subcategory2_2_category_id":"1","jb_product_subcategory2_3_subcategory1_id":"1","jb_product_subcategory2_4_name":"Pregnancy wear","jb_product_subcategory2_5_delete_status":"0","jb_product_subcategory2_6_created_on":"1501574226464","jb_product_subcategory2_7_updated_on":"1501574226464"}]}]},{"jb_product_category_id":"2","jb_product_category_name":"Child Wear","jb_product_category_prefix":"jbpc","jb_product_category_delete_status":"0","jb_product_category_created_on":"1502429483534","jb_product_category_updated_on":"1502429483534","subCategory1":[{"jb_product_subcategory1_1_id":"2","jb_product_subcategory1_2_category_id":"2","jb_product_subcategory1_3_name":"Girls","jb_product_subcategory1_4_delete_status":"0","jb_product_subcategory1_5_created_on":"1502429606169","jb_product_subcategory1_6_updated_on":"1502429606169","subCategory2":[{"jb_product_subcategory2_1_id":"2","jb_product_subcategory2_2_category_id":"2","jb_product_subcategory2_3_subcategory1_id":"2","jb_product_subcategory2_4_name":"Western","jb_product_subcategory2_5_delete_status":"0","jb_product_subcategory2_6_created_on":"1502429794573","jb_product_subcategory2_7_updated_on":"1502429794573"}]},{"jb_product_subcategory1_1_id":"3","jb_product_subcategory1_2_category_id":"2","jb_product_subcategory1_3_name":"Boys","jb_product_subcategory1_4_delete_status":"0","jb_product_subcategory1_5_created_on":"1505105190176","jb_product_subcategory1_6_updated_on":"1505105190176","subCategory2":[]}]}]}';
$array = json_decode($json_string, true);
function build_list($array) {
$list = '<ul>';
foreach($array as $key => $value) {
foreach($value as $key => $index) {
if(is_array($index)) {
$list .= build_list($index);
} else {
$list .= "<li>$index</li>";
}
}
}
$list .= '</ul>';
return $list;
}
echo build_list($array);
?>
Simply use only one foreach, the nested one seems useless :
foreach($array as $key => $index) {
if(is_array($index)) {
/* ... */
Just add a condition to check if $value is a valid array or not. This way it will not process $value if it's not an array and warnings will go away.
if (!is_array($value)) {
continue;
}
Use this condition inside foreach before the looping $array.
foreach($array as $key => $value) {
if (!is_array($value)) {
continue;
}
foreach($value as $k => $index) {
if(is_array($index)) {
$list .= build_list($index);
} else {
$list .= "<li>$index</li>";
}
}
}
Ideone link : Code

Parse JSON data from multiple options

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

get COUNT(*) from mysql_magic

The following code retrieve the result from queries. Usually, I ask for SELECT... and I get a two-dimensional array but this time I need to get count from a table.
if(isset($_GET['query']))
{
$results = mysql_magic($_GET['query']);
$response = array();
//ERROR : Invalid argument supplied for foreach()
foreach($results as &$row)
{
$rowArray = array();
foreach($row as &$column)
{
$rowArray[] = $column;
}
$response[] = $rowArray;
}
$jsonData = json_encode($results);
}
This is a part of the function mysql_magic. In most cases, it returns mysql_fetch_all($req_result). In this case, it returns a row.
else if (startsWith($req_sql, 'select count(*)'))
{
$line = mysql_fetch_row($req_result);
return $line[0];
}
Why do I get an error "Invalid argument supplied for foreach()" since my result, a count, contains one row and one column?
I think that you are using mysql_magic($_GET['query']) should be mysql_query($_GET['query']). I see no reference in the code for a $_GET case. So $results is undefined due to php error?
if(isset($_GET['query']))
{
$results = mysql_query($_GET['query']);
$response = array();
//ERROR : Invalid argument supplied for foreach()
foreach($results as &$row)
{
$rowArray = array();
foreach($row as &$column)
{
$rowArray[] = $column;
}
$response[] = $rowArray;
}
$jsonData = json_encode($results);
}

foreach() error when using wordpress wp_get_sidebars_widgets()

function getWidgets($position = null) {
if (empty($this->widgets)) {
foreach (wp_get_sidebars_widgets() as $pos => $ids) {
$this->widgets[$pos] = array();
foreach ($ids as $id) { // error is here
$this->widgets[$pos][$id] = $this->getWidget($id);
}
}
}
}
These are lines 305-314.
I'm getting this error:
" Warning: Invalid argument supplied for foreach() in /home/content/73/9889573/html/wp-content/themes/yoo_spark_wp/warp/systems/wordpress.3.0/helpers/system.php on line 310 "
Can someone tell me how do i fix it
wp_get_sidebars_widgets() returns a 1-dimensional array.
Reference: http://codex.wordpress.org/Function_Reference/wp_get_sidebars_widgets
$ids is not an array. You cannot traverse it in a foreach loop.
Try this:
$widgets = array();
foreach (wp_get_sidebars_widgets() as $pos => $id) {
$widgets[$pos] = $this->getWidget($id);
}

Categories