Storing another array's values in an array using foreach - php

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

Related

Illegal string offset 'key'

I have this code:
// Champion name and splash art
$endpointChampion = file_get_contents("https://global.api.riotgames.com/api/lol/static-data/BR/v1.2/champion/".$championMastery."?api_key=MYKEY");
$jsonChampion = json_decode($endpointChampion, true);
foreach ($jsonChampion as $champion) {
if (isset($jsonChampion['key'])) {
$championKey = $champion['key'];
}
}
But this $championKey variable returns "o" and 3 warnings are prompted on screen:
Warning: Illegal string offset 'key' in E:\xampp\htdocs\riot\index.php on line 41
I also tried to validate the entry, using isset() but seems not work properly.
The $championMastery is retrieved here:
$endpointMastery = file_get_contents("https://br.api.riotgames.com/championmastery/location/BR1/player/8083198/champions?api_key=MYKEY");
$jsonMastery = json_decode($endpointMastery, true);
foreach ($jsonMastery as $mastery) {
$championMastery = $mastery['championId'];
$masteryLevel = $mastery['championLevel'];
}
You are getting error because API returns one dimensional array and $champion is string value in foreach ($jsonChampion as $champion). Following can be fix:
foreach ($jsonChampion as $champion) {
if (isset($jsonChampion['key'])) {
$championKey = $jsonChampion['key'];
}
}
BTW,
$jsonChampion is one dimensional Array so you can retrieve $championKey without writing foreach loop as follows:
if(is_array($jsonChampion) && isset($jsonChampion['key'])){
$championKey = $jsonChampion['key'];
}

PHP - Invalid argument supplied for foreach() warning message

$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;
}
}

Assigning values to associative array (multidimensional)

while($row = $champions_table_result->fetch_assoc()){
while($row3['Champion']==$row['Champion']){
$test[$row['Champion']]['General']['Change'][] =$row3['Stat_Change'];
$test[$row['Champion']]['General']['Type'][] = $row3['Stat_Change_Icon'];
}
foreach ($row as $column_name => $column) {
if ($column_name == 'Champion') {
continue; // These fields were already displayed above
}
if (!empty($column)) {
$test[$row['Champion']][$column_name] =$column;
while($row2['Champion']==$row['Champion']&&$row2['Spell_Type']
==$column_name&&!empty($row2['Spell_Change'])) {
$test[$row['Champion']][$column_name]['Change'][] = $spell_descriptions[ $champion_counter][$spell_counter][$change_counter];
$test[$row['Champion']][$column_name]['Type'][] = $spell_changes[
$champion_counter][$spell_counter][$change_counter];
$row2 = $spells_table_result->fetch_assoc();
}
}
}
}
To better showcase my problem here is an image with var_dump from $test - array http://i.imgur.com/xcox2df.png and next to Q W E it should show variable $column; I'm trying to achieve that in this line
$test[$row['Champion']][$column_name] =$column;
Without this line I get what I showed on imgur but with this line I get an error:
Warning: Illegal string offset 'Change' and Fatal error: Cannot use string offset as 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

Iterating over associative array in 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

Categories