PHP - Invalid argument supplied for foreach() warning message - php

$data = file_get_contents($file);
$fexplode = explode(PHP_EOL, $data);
foreach ($fexplode as $uline) {
foreach (unserialize($uline) as $item => $value){
echo $item . " : ";
echo $value;
}
}
Getting a "Warning: Invalid argument supplied for foreach() " warning on my page when running this code

use below way
$data = file_get_contents($file);
$fexplode = (!empty($data)?explode(PHP_EOL, $data):array()); // use !empty otherwise make empty array
foreach ($fexplode as $uline) {
foreach (unserialize($uline) as $item => $value){
echo $item . " : ";
echo $value;
}
}

Related

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

Storing another array's values in an array using foreach

This is my code:
$videos_key = array();
foreach($result[$x]["videos_key"] as $videos_key )
{
$videos_key[] = $result[$x]["videos_key"];
}
print $videos_key;
Here $result[$x]["videos_key"]is an array, which have values. It is inside an forloop, so [$x] is an number like: 0,1,2,3...
I want to transfer it's value to $videos_key
Errors:
Warning: Invalid argument supplied for foreach()
2nd error
Notice: Array to string conversion
Invalid argument supplied for foreach()?? $result[$x]["videos_key"] is an array???
$videos_key = array();
if (is_array($result[$x]["videos_key"])){
foreach($result[$x]["videos_key"] as $key => $value )
{
array_push($videos_key,$value);
}
}else{
echo "no array";
}
echo print_r($videos_key, true);

Invalid argument supplied for foreach() in file 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.

PHP- confirmed I am using an array, but still getting Invalid argument supplied for foreach()

Using this code:
$brands = json_decode($data, true);
echo json_encode($brands);
echo "<br>";
echo gettype($brands);
echo "<br>";
foreach ($brands['brand_name'] as $brand) {
echo $brand, '<br>';
}
I am getting a Warning: Invalid argument supplied for foreach(). If I add in these lines:
$brands = json_decode($data, true);
echo json_encode($brands);
echo "<br>";
echo gettype($brands);
echo "<br>";
foreach ($brands['brand_name'] as $brand) {
echo $brand, '<br>';
}
I can see the data that I'm working with and also confirm that $brands is an array. Here is the output on the page when viewed in a browser:
{"result":1,"results":[{"brand_id":"1","brand_name":"Gildan"},{"brand_id":"2","brand_name":"American Apparel"},{"brand_id":"3","brand_name":"Rabbit Skins"},{"brand_id":"4","brand_name":"Anvil"},{"brand_id":"5","brand_name":"Bella / Canvas"},{"brand_id":"6","brand_name":"Alternative"},{"brand_id":"8","brand_name":"Hanes"},{"brand_id":"9","brand_name":"ALO"},{"brand_id":"10","brand_name":"Augusta"},{"brand_id":"11","brand_name":"Precious Cargo"},{"brand_id":"12","brand_name":"Other"},{"brand_id":"13","brand_name":"Jerzees"},{"brand_id":"15","brand_name":"Liberty Bags"},{"brand_id":"16","brand_name":"Port Authority"},{"brand_id":"17","brand_name":"Next Level"}]}
array
Warning: Invalid argument supplied for foreach() in -- on line 36
Since $brands is an array, I'd think this should work. Any ideas why it is not?
You should iterate through $brands['results'] instead:
foreach ($brands['results'] as $brand) {
echo $brand['brand_name'], '<br>';
}
$brands is an array, but $brands['brand_name'] isn't, and that's what you're looping on. e.g.
$foo = array('bar' => 'baz');
^^^^--array ^^^^^-key ^^^^---string
foreach($foo as $value) { } // this is ok. $foo is an array
foreach($foo['bar'] as $value) { } // this is wrong. $foo['bar'] is a STRING

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