Invalid argument supplied for foreach() php warning - php

Iam newbie to php and codeigniter.
I have error in php,
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: controllers/search.php
Line Number: 44
Line 44 is: foreach ( $data as $key => $value )
There is some syntax error here,
inside this if statement, if (!empty($user['search'])), $data is printed correctly,whereas in this if statement,if(!empty($data)), $data is not printed. Can anyone help me resolve this issue.

The problem is you don't have any value inside your looping variable( $data ).
You can print their values for check whether the variable contains values.

Replace :
$data = json_decode($user['search'])->result;
By :
$data = json_decode($user['search'], true);
json_decode works that way :
var_dump(json_decode($json)); // returns object(stdClass)
var_dump(json_decode($json, true)); returns array
Your $data was an object, while foreach strictly wants an array.

Related

getting values out of json array in codeigniter

Hi am having a little from with json array. when i try to get the values out of json array.
Controller
$data['payment'] = $this->admin->get_payment_settings();
$value = $data['payment'][0]->json;
echo $value['username'];
Hi am having a json array in database. using my controller i am getting the filed json. When i do a var_dump($value) array look like this
{"username":"foodi1.lch.co","password":"FBUEWQH6X4D","signature":"AFcWxV21C7fd0v3bZnWKgr9on9AmTuhyd4MVq","currency":"CAD"}
i want to get each value out of this array.
echo $value['username'];
echo $value['password'];
When i try to do this i get the error
A PHP Error was encountered
Severity: Warning
Message: Illegal string offset 'username'
Filename: controllers/administrator.php
Line Number: 620
Can some one help me to get the values out of json array. tnx..
You need to decode the string to a valid array, use json_decode as follows:
$json = json_decode($value, true);
echo $json['username'];
It seems your $data['payment'][0] is json string.So you need to decode it.
Try this way
$value=json_decode($data['payment'][0]->json,true);//second parameter true will return as array.
echo $value['username'];

Errors with json_decode, array and foreach

I use this code for Virtuemart:
$product_id_to_remove = 3;
$cart = json_decode($_SESSION['__vm']['vmcart']);
foreach($cart->cartProductsData as $k => $v){
if($v->virtuemart_product_id == $product_id_to_remove) unset($cart->cartProductsData[$k]);
}
$_SESSION['__vm']['vmcart'] = json_encode($cart);
but I get the fatal error: Cannot use object of type stdClass as array in ... line 4. If I add true at json_decode($_SESSION['__vm']['vmcart']) I get the warning: Invalid argument supplied for foreach().
How to resolve the problem?
p.s. I'm beginner in php and don't know json_ at all. The code is suggested by the link: stackoverflow.com/questions/28691203/how-to-remove-a-single-product-from-mod-virtuemart-cart
$cart->cartProductsData behaves like an array but it's actually an object
try this:
change
unset($cart->cartProductsData[$k])
to
unset($cart->cartProductsData->$k)

PHP: Illegal string offset

I know there already are questions like this, but It didn't help me.
I get the follow error on my site:
Warning: Illegal string offset 'networkConnections' in
/var/www/bitmsg/templates/header.php on line 25 {
The line is
<?= $bmstatus["networkConnections"] ?> p2p nodes
if I print_r $bmstatus, then I get:
{
"numberOfBroadcastsProcessed": 2308,
"networkStatus": "connectedAndReceivingIncomingConnections",
"softwareName": "PyBitmessage",
"softwareVersion": "0.4.1",
"networkConnections": 52,
"numberOfMessagesProcessed": 22888,
"numberOfPubkeysProcessed": 8115
}
How to I fetch the information from this array?
I've tried both $bmstatus['networkConnections'] and $bmstatus->networkConnections
but both is returning that error?
$bmstatus contains a JSON string. You have to decode it first to be able to extract the required information out of it. For this purpose, you can use the built-in function json_decode() (with the second parameter set as TRUE to get an associative array, instead of an object):
$json = json_decode($bmstatus, true);
echo $json['networkConnections'];
It's a json string. You need to decode your json response using json_decode with second parameter true to get as an associative array.
$bmstatusArray = json_decode($bmstatus,true);
echo $bmstatusArray["networkConnections"];

Using IN with other conditions in Redbean

I am trying to find some beans by using "IN" as well as "=". I am currently using this code:
$ids = array(1,2,3,4);
$user = 1;
$things = R::find(
'thing',
'id IN ('.R::genSlots($ids).') AND user = ?',
array(
$ids,
$user
)
);
This gives me some errors:
PHP Notice: Array to string conversion in rb.php on line 217
Fatal error: Uncaught [HY093] - SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens thrown in rb.php on line 267
If I run this code with one item in $id I only get the notice but either way I get no results.
I'm assuming it's trying to treat $id as a single variable. What am I missing?
don't add the $ids to the array, merge the two into one array, otherwise this will end up as a nested array.

Trouble looping through an array created from a foursquare JSON feed

I'm working on a side project and at its core, I need to get a foursquare json feed into an array that i can loop through. My code is below and results in the following error:
Warning: Invalid argument supplied for foreach() in /homepages/7/d346835943/htdocs/dealrub/results.php on line 56
Here is an example of the json feed that i am correctly acquiring:
$jsonurl = "http://api.foursquare.com/v2/venues/search?ll=".$lat.",".$lon."&limit=100";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_encode($json, true);
foreach ( $json_output->response->groups[0]->items as $items )
{
echo "{$items->name}\n";
}
Any help as to what i'm doing wrong would be greatly appreciated. I left the jsonurl without my api key, but it is successfully returning the json results.
You have to use json_decode.
Check whether $json_ouput is not empty.
You are passing true as second argument to json_decode (assuming you have it right) which means that it returns an associative array.
Either omit that:
$json_output = json_decode($json);
or access items as array:
foreach ( $json_output['response']['groups'][0]['items'] as $items )
You're using json_encode on a string that is already in json. Try json_decode instead ;)

Categories