Iterating over associative array in PHP - php

I have an array $pedidos which I fill by:
$pedido[$nombreProducto['nombre']] = $cantidad;
So, when U try to iterate over it with this loop:
foreach ($pedidos as $key => $value) {
echo $key."-->".$value;
}
I'm getting the error log message:
Invalid argument supplied for foreach()
How should I iterate over it?

Your array is called $pedido change the for each with this:
foreach ($pedido as $key => $value) {
echo $key."-->".$value;
}

In foreach you have $pedidos, but you are filling array $pedido

Related

Passing an object keys value to array

Language : PHP,
Framework : Laravel,
I have a collection of array.
I want to create a new array for each key and push all the variables to the array.
I have done the following code which looks ugly.
Is there some better way to do this.
I have created an new array for key using foreach loop and then passed the value to the array again using foreach loop.
$resultLabels = [];
foreach ($results as $result) {
foreach($result as $key => $value){
array_push($resultLabels,'ward_no ' .$value);
}
}
foreach ($results as $result){
foreach($result as $key => $value){
if($key != 'ward_no'){
array_push($arrays[$key],$value);
}
}
}
You don't need another nested loop. Have a look here:
$resultLabels = [];
foreach ($results as $result){
foreach($result as $key => $value){
array_push($resultLabels,'ward_no ' .$value);
if($key != 'ward_no'){
array_push($arrays[$key],$value);
}
}
}

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);

PHP foreach with exif_read_data keep saying Warning: Invalid argument supplied for foreach()

i tried several variant of getting exif info written out. but in every case i had similar problem:
"Warning: Invalid argument supplied for foreach()" --
in: foreach ($section as $name => $val)
$exif = #exif_read_data($result['path'], 'IFD0');
if(is_array($exif))
foreach ($exif as $key => $section) {
foreach ($section as $name => $val) {
echo "$key.$name: $val<br />\n";
}
}
place if (is_array($section))
before the second foreach statement..
It is possible that some or all of the $section values are not array so you can't use foreach loop to traverse through the array.
big thanks to Maximus2012

How to parse Json data in php?

[{"name":"se","value":"test1"},{"name":"model","value":"test2"},{"name":"filter_preference","value":"test3"},{"name":"seved","value":"test4"}]
I have json, and I would like to parse it, and to get for "segment" => "test1"
and so on.
i have done this
$json2= json_encode($loadFilter);
$json2 = json_decode($json2, true);
foreach ($json2->$key as $value)
{
echo $key ."=>".$value;
}
always getting Invalid argument supplied for foreach() !!!
I am doing it WP ajax callback.
Your foreach syntax is wrong to access the $key.
foreach ($json2 as $key => $value) {
echo $key ."=>".$value;
}
Edit from your comments:
You didn't give the "real" format in your question, your array is contained in 'filter_preference', so you have to iterate over $json2['filter_preference'].
foreach ($json2['filter_preference'] as $key => $value) {
echo $key ."=>".$value;
}
you need to map key value for sub array.
try this:
foreach ($json2 as $key=>$value)
{
echo $key ."=>".$value;
}
One odd suggestion here:
If you want to use array for this then you can convert object to array using following code:
function objectToArray($d) {
if (is_object($d)) {
// Gets the properties of the given object
// with get_object_vars function
$d = get_object_vars($d);
}
if (is_array($d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return array_map(__FUNCTION__, $d);
}
else {
// Return array
return $d;
}
}
$array_new = objectToArray($json2);
http://wonderphp.wordpress.com/2014/03/20/convert-object-to-array-and-vice-versa/

Undefined offset NOTICE when extracting array

When I run this code, it works, but it gives me NOTICE 'Undefined offset 1'. Can someone tell me how can I fix this? $this->_data is a multidimensional array.
foreach ($this->_data as $key => $values) {
$this->_param[] = explode(",", $values[1]);
}
You need to check if there is any data in $values[1]
Use this:
foreach ($this->_data as $key => $values) {
if (isset($values[1]){
$this->_param[] = explode(",", $values[1]);
}
}

Categories