Illegal string offset in foreach - php

I'm trying to loop in the second array with this code
<?php
foreach ($categories[1] as $category_cat) { ?>
<li><span><?php echo $category_cat['name']; ?></span></li>
<?php } ?>
And I get the "Illegal string offset in..." error. I know that addind the [1] the the foreach might be the error but how I could make the loop in the second array then ?
Array content
array (size=2)
0 =>
array (size=4)
'name' => string 'Brands' (length=7)
'children' =>
array (size=12)
0 =>
array (size=3)
...
1 =>
array (size=3)
...
2 =>
array (size=3)
...
3 =>
array (size=3)
...
4 =>
array (size=3)
...
5 =>
array (size=3)
...
6 =>
array (size=3)
...
7 =>
array (size=3)
...
8 =>
array (size=3)
...
9 =>
array (size=3)
...
10 =>
array (size=3)
...
11 =>
array (size=3)
...
'column' => string '1' (length=1)
'href' => string 'url here' (length=78)
1 =>
array (size=4)
'name' => string 'Catégories' (length=11)
'children' =>
array (size=7)
0 =>
array (size=3)
...
1 =>
array (size=3)
...

Just try to loop $categories[1]['children']:
foreach ( $categories[1]['children'] as $category_cat ) { }

Related

MongoDb get only value but not key using php

i am using mongodb and my query returns only a key value
the following is the format of the var_dump
<pre>array (size=471)
0 =>
array (size=1)
'sno' => string '162230' (length=6)
1 =>
array (size=1)
'sno' => string '165333' (length=6)
2 =>
array (size=1)
'sno' => string '181312' (length=6)
3 =>
array (size=1)
'sno' => string '181313' (length=6)
4 =>
array (size=1)
'sno' => string '181314' (length=6)
5 =>
array (size=1)
'sno' => string '181315' (length=6)
6 =>
array (size=1)
'sno' => string '181316' (length=6)
7 =>
array (size=1)
'sno' => string '181317' (length=6)
8 =>
array (size=1)
'sno' => string '181318' (length=6)
9 =>
array (size=1)
'sno' => string '181319' (length=6)
10 =>
array (size=1)
'sno' => string '181320' (length=6)</pre>
I do not want key . all i want is a array of values without a loop.
Solved it.
array_column($arr,"sno")

How do I retrieve the values from a multidimensional array?

Do you know a good method or tips on how do I retrieve the values (as string) from this multi dimensional array:
array (size=5)
0 =>
array (size=1)
'email' =>
array (size=1)
0 => string 'lavidabonita#gmail.com' (length=27)
1 =>
array (size=1)
'email' =>
array (size=1)
0 => string 'iancasillasbuffon#gmail.com' (length=27)
2 =>
array (size=1)
'email' =>
array (size=1)
0 => string 'eddynvg#hotmail.com' (length=19)
3 =>
array (size=1)
'email' =>
array (size=1)
0 => string 'dolphin23#dolphin.net' (length=21)
4 =>
array (size=1)
'email' =>
array (size=1)
0 => string 'dolphin#dolphin.org' (length=19)
try with this:
function fn($arg){
foreach ($arg as $key => $val){
if (is_array($val)){
fn($val);
}
else{
echo "$key : $val\n";
}
}
}
fn($data);

PHP renaming array and covert from string to integer

I have var_dump() variable $fruit_array as below:
array (size=3)
0 =>
array (size=1)
'fruit_id' => string '2' (length=1)
1 =>
array (size=1)
'fruit_id' => string '1' (length=1)
2 =>
array (size=1)
'fruit_id' => string '3' (length=1)
I need to rename fruit_id to id and convert array string value to integer, example result:
array (size=3)
0 =>
array (size=1)
'id' => int 2
1 =>
array (size=1)
'id' => int 1
2 =>
array (size=1)
'id' => int 3
How can I do that ? thanks
You can do something like this:
<?php
foreach ( $array as $k=>$v )
{
$array[$k] ['id'] = intval( $array[$k] ['fruit_id'] );
unset($array[$k]['fruit_id']);
}

Find Array With 'SELECTED' Value Inside Multidimensional Array

I've got a very large JSON file that I've converted into an array. The code you see below is the result of
var_dump($array['talents'])
(for those wondering, yes, this is me attempting to use Blizzard Entertainment's API)
Maybe I just missed that day at PHP school, but I just can't seem to remember or figure out how to get the values inside these deeper arrays. And perhaps more importantly, pay attention to the two 'spec' fields - ONE of them has a value of 'SELECTED: BOOLEAN TRUE', and the other does not. Basically, I need the NAME field from the array that has that SELECTED value.
To be honest, much as I love working in PHP, this multi-level thinking is a tad beyond me at the moment, I need some more experience. In the meantime, could anyone give me some tips, pointers, or just straight-up answers as to how I can get the value I need?
Thanks a bunch for your time!
array (size=2)
0 =>
array (size=6)
'talents' =>
array (size=6)
0 =>
array (size=3)
...
1 =>
array (size=3)
...
2 =>
array (size=3)
...
3 =>
array (size=3)
...
4 =>
array (size=3)
...
5 =>
array (size=3)
...
'glyphs' =>
array (size=2)
'major' =>
array (size=0)
...
'minor' =>
array (size=1)
...
'spec' =>
array (size=6)
'name' => string 'Frost' (length=5)
'role' => string 'DPS' (length=3)
'backgroundImage' => string 'bg-mage-frost' (length=13)
'icon' => string 'spell_frost_frostbolt02' (length=23)
'description' => string 'Freezes enemies in their tracks and shatters them with Frost magic.' (length=67)
'order' => int 2
'calcTalent' => string '120021.' (length=7)
'calcSpec' => string 'b' (length=1)
'calcGlyph' => string 'O' (length=1)
1 =>
array (size=7)
'selected' => boolean true
'talents' =>
array (size=6)
0 =>
array (size=3)
...
1 =>
array (size=3)
...
2 =>
array (size=3)
...
3 =>
array (size=3)
...
4 =>
array (size=3)
...
5 =>
array (size=3)
...
'glyphs' =>
array (size=2)
'major' =>
array (size=0)
...
'minor' =>
array (size=0)
...
'spec' =>
array (size=6)
'name' => string 'Fire' (length=4)
'role' => string 'DPS' (length=3)
'backgroundImage' => string 'bg-mage-fire' (length=12)
'icon' => string 'spell_fire_firebolt02' (length=21)
'description' => string 'Ignite enemies with balls of fire and combustive flames.' (length=56)
'order' => int 1
'calcTalent' => string '122012.' (length=7)
'calcSpec' => string 'Z' (length=1)
'calcGlyph' => string '' (length=0)
should be
print_r($array['talents'][0]);
print_r($array['glyphs']['major']);

How to combine/merge an multidimension array that has same keys in PHP?

So I have this array which the vardump looks like this:
array (size=4)
0 =>
array (size=1)
'field_4' =>
array (size=1)
0 => string 'item-1' (length=6)
1 =>
array (size=1)
'field_4' =>
array (size=1)
0 => string 'info-1' (length=6)
2 =>
array (size=1)
'field_5' =>
array (size=1)
0 => string 'item-2' (length=6)
3 =>
array (size=1)
'field_5' =>
array (size=1)
0 => string 'info-2' (length=6)
So I am trying to combine the array with the same key for example 'field_4' would be merge/combined into an array that has 2 items "item-1" and "info-1".
So the end result I would like is this:
array (size=2)
0 =>
array (size=1)
'field_4' =>
array (size=2)
0 => string 'item-1' (length=6)
1 => string 'info-1' (length=6)
1 =>
array (size=1)
'field_5' =>
array (size=1)
0 => string 'item-2' (length=6)
1 => string 'info-2' (lenght=6)
So is there a PHP convenience function to handle this or do I have to rebuild the array?
Thanks for looking.
Just iterate over the input array, building the merged array:
$merged = array();
foreach ($input as $a)
foreach ($a as $k => $v)
foreach ($v as $v2)
$merged[$k][] = $v2;
And then flatten it into your weird required output:
$flattened = array();
foreach ($merged as $k => $v)
$flattened[] = array($k => $v);
Input:
$input = array(
array('field_4' => array('item-1')),
array('field_4' => array('info-1')),
array('field_5' => array('item-2')),
array('field_5' => array('info-2')),
);
Output:
array(
array('field_4' => array('item-1', 'info-1')),
array('field_5' => array('item-2', 'info-2')),
)
When dumping output, print_r or var_export make for a much more readable example than var_dump

Categories