I've been looking into this problem for a couple of days now and I just can't seem to figure it out.
I'm trying to do something simple, I thought, just looping through an array.
This is a screenshot of the array: http://cl.ly/image/3j2J3x1C3B0j
I'm trying to loop through all the 'Skills' array, there the "Skill' array and inside that grabbing the "Icon".
For this I made 2 loops:
foreach ($hero_data['skills'] as $skills)
{
foreach ($skills as $skill)
{
//print_r($skill['skill']);
}
}
Unfortunaly this doesn't work, in laravel. I'm getting the "Undefined index: skill" error. It does work when I tried it outside , as a standalone script.
Out side both of the loops I can select the icon with:
print_r($hero_data['skills']['active'][0]['skill']['icon']);
I'm sure I'm overlooking something stupid...
Thanks a lot for the help,
Looking at what you've said from the other solutions posted here, it's clear that you are looping through the sub arrays and not all of those sub arrays contain the keys that your further loops are looking for.
Try this:
foreach ($hero_data['skills']['active'] as $skills) {
if (isset($skills['skill']['icon'])) {
print_r($skills['skill']['icon']);
}
}
Because, for example, if $hero_data['skills']['active'][8] doesn't actually have a skill array or a ['skill']['icon'] array further down, then the loop will throw the errors you have been reporting.
The nested array keys you are looking for must be found in every iteration of the loop without fail, or you have to insert a clause to skip those array elements if they aren't found. And it seems like your $hero_data array has parts where there is no ['skill'] or ['icon'], so therefore try inserting one or more isset() checks in the loops. Otherwise, you need to find a way of guaranteeing the integrity of your $hero_data array.
Your game looks interesting by the way!
Inside skills you have an 'active' attribute and it contains the array you need, so you need to change your code to this:
foreach ($hero_data['skills'] as $skills)
{
foreach ($skills['active'] as $skill)
{
//print_r($skill['skill']);
}
}
Try:
foreach ($hero_data['skills'] as $skills)
{
foreach ($skills as $skillState)
{
foreach ($skillState as $skill)
{
print_r($skill['skill']);
}
}
}
You simply need to iterate the active index of the array. this should work :
foreach ($hero_data['skills']['active'] as $skills) {
print_r($skills['skill']['icon']);
}
Related
Looking for some advise with regards to multidimensional Arrays, pushing the vars into a POST call within a foreach loop.
I currently have a foreach loop containing an IF clause:
foreach ($responseData['data'] as $key => $value) {
if (strtotime($value['attributes']['created_time']) > $currentDateMinusThirtyMinutes) {
array_push($emails, $value['attributes']['email']);
}
}
This is supposed to fetch the Email for all arrays which meet the condition of being created in the last 30 minutes.
However, now i am stuck, I need to fetch multiple other variables and call on a POST call similar to the below:
https://developer.example.co.za/{$PAGE}/create?reference={$ID}¤cy=ZAR&amount={$AMOUNT}&firstname={$FIRST}&lastname={$LAST}&email={$EMAIL}&sendmail=true
What i need to know is, is there a better way to treat the foreach loop?
Is foreach the right way to go?
In essence I would need to run through each of the $responseData['data'] returns and make a POST call to the above URL.
Any advise would be appreciated.
Fixed this by creating a Data_array which stored the values I required, then adding the POST call inside the foreach loop which then did the trick.
The foreach ended up looking like this:
foreach ($responseData['data'] as $key => $value) {
if (strtotime($value['attributes']['created_time']) > $currentDateMinusThirtyMinutes) {
$data_array[] = array(
"id" => $value['attributes']['id'],
***add rest of vars you want***
);
***Add POST call or whatever else you want to do***
}
}
Thanks #darklightcode for pointing me to the idea of splitting out just the variables I wanted.
I have this object containing one row and many keys/variables with numbered names.
I need to pass each of them one at a time to another function. How do I loop through the keys instead of the rows?
The code would look like this:
foreach ($object['id'] as $row):
$i++;
$data['myInfo'][$i] = $this->get_data->getInfo('data1', 'id', $row->{'info'.$i.'_id'});`
but this obviously won't work since it's looping through the rows/instances of an object, and I have only one row in my $object['id'] object (with info1_id, info2_id, info3_id, info4_id... etc keys), so the loop stops after just one cycle. And I really don't feel like typing all of that extra code by hand, there's gotta be solution for this. :)
You can just iterate through your object like an array :
foreach ($object['id'] as $row) {
foreach ($row as $k => $v) {
$id = substr($k, 4, strpos($k, '_')-4);
$data['myInfo'][$id] = $this->get_data->getInfo('data1', 'id', $v);`
}
}
Thanks for the directions Alfwed, I didn't know you could use foreach loop for anything other than instances of an object or arrays (only started learning php a week or so ago), but now it looks pretty straight forward. that's how I did it:
foreach ($object['id'] as $row):
foreach ($row as $k=>$v):
$i++;
if ($k == print_r ('info'.$i.'_id',true)){
$data['myinfo'][$i] = $this->get_db->getRow('products', 'id','info'.$i.'_id');
<...>
in my case, I knew how many and where were those values, so I didn't have to worry about index values too much.
I have a need to check if the elements in an array are objects or something else. So far I did it like this:
if((is_object($myArray[0]))) { ... }
However, on occasion situations dictate that the input array does not have indexes that start with zero (or aren't even numeric), therefore asking for $myArray[0] will generate a Notice, but will also return the wrong result in my condition if the first array element actually is an object (but under another index).
The only way I can think of doing here is a foreach loop where I would break out of it right on the first go.
foreach($myArray as $element) {
$areObjects = (is_object($element));
break;
}
if(($areObjects)) { ... }
But I am wondering if there is a faster code than this, because a foreach loop seems unnecessary here.
you can use reset() function to get first index data from array
if(is_object(reset($myArray))){
//do here
}
You could get an array of keys and get the first one:
$keys = array_keys($myArray);
if((is_object($myArray[$keys[0]]))) { ... }
try this
reset($myArray);
$firstElement = current($myArray);
current gets the element in the current index, therefore you should reset the pointer of the array to the first element using reset
http://php.net/manual/en/function.current.php
http://php.net/manual/en/function.reset.php
I've got a foreach statement that on an item that has both objects and arrays in it.
foreach($result as $data)
that contains both arrays and objects. how do i specify the foreach to only select to loop through one or the other? when it loops through them all it takes forever
I had tried foreach($result->data as $data) but then it errors on the arrays telling me it is trying to get property of an object, which is understandable. once I add an if statement to check if the first result is an object it almost triples the script run time since there are so many results.
Well you could just use is_object() and is_array() (both return a boolean):
if (is_object($var)) {
// do something
} else if (is_array($var)) {
// well then, do something else
}
I'm trying to check if a certain category is allready selected by looping through an array of categories also I want to add another element to the array whci is just a bit to indicate is the category selcated
my categories array looks like this
0=>array(category_id=>12,category_name=>"blogger")
1=>array(category_id=>13,category_name=>"dancer")
etc...
now the code i'm trying goes like that:
foreach ($userCategories as $key=>$category) {
if($category['category_id'] == $mediaDetails['currentCategory']) {
$category['current'] = 1;
} else {
$category['current'] = 0;
}
}
when executing
die(var_dump($userCategories));
I expect to get an array similar to
0=>array(category_id=>12,category_name=>"blogger",current=>0)
1=>array(category_id=>13,category_name=>"dancer",current=>1)
but instead I get the same array I had before the foreach loop
any ideas?
Thanks
It looks like $category is not getting passed by reference.
Try $userCategories[$key]['current']=1 instead, and see how that works.