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.
Related
I have an array with with different code numbers, and in turn each code can refer to different items. However, every code can refer to many items, while every item can have many codes (i.e. many-to-many relation between codes and items).
I have the items and I have the item codes, and now I want to append the right codes to the right items via a loop in a code snippet looking this (each $itemCode object includes an $item.Id property):
foreach($itemCodes as $itemCode) {
$itemCodesForItem = [];
foreach($items as $item) {
if($itemCode->getItem()->getId() == $item->getId()) {
array_push($itemCodesForItem, $itemCode->getCode());
$item->setCode($itemCodesForItem); // Assign with array with all appended codes for a specific item
}
}
}
The issue here is that every matching code overwrites the previous matching one, so in the end it only assigns one code to each item. But if an item has several codes, I want all those codes appended in the array for the specific item, and not only the last matching one. Does anyone know how to catch and append the right codes to the right items here?
Ok, so finally I got it working with the following code:
foreach($items as $item) {
$itemCodesForItem = [];
foreach($itemCodes as $itemCode) {
while($itemCode->getItem()->getId() == $item->getId()) {
array_push($itemCodesForItem, $itemCode->getCode());
$item->setCode($itemCodesForItem);
break;
}
}
}
I have this
$homedata = $user->getbanks();
foreach ($homedata as $data)
echo json_encode(array("replies" => array(array("message" => $data->bankname))));
I have this put the result am getting is only one array from the list. I want to get all.
If I understand your requirement correctly, you need the banknames in an array replies within a key message. For that the below code will do the job. But you can optimize the query for better performance.
$homedata = $user->getbanks();
$banks['replies'] = [];
foreach ($homedata as $data) {
$banks['replies'][]['message'] = $data->bankname;
}
print_r(json_encode($banks)); exit;
Further, you were only getting the first content because you are overriding the previous content when you iterate using foreach.
I'm attempting to make 1 array out of 2 existing arrays (which cannot be modified). In order to do this I'm creating the array in a foreach which is nested in another foreach.
The code I used:
$language_option = array();
foreach(Languages::getFullSelectOptionsList() as $country_description_1 => $country_code){
foreach(Languages::getFullSelectOptionsList(TRUE) as $country_description_2 => $country_code){
$language_option[$country_code] = $country_description_1.' - '.$country_description_2;
}
}
In this code "Languages::getFullSelectOptionsList()" returns an array with the 1st country descriptions.
And "Languages::getFullSelectOptionsList(TRUE)" returns an array with the 2nd country descriptions.
This is what my code does:
dropdown results
But what I'd like it to do is:
dropdown wished results
As you can see in the first picture only the last array value of "country_description_1" is used instead of using them all.
Are there any errors in my code, is this not possible to do or is there an easier way of doing this?
Thanks.
Here you can get reference of this code.
But This will not work because you need to specify the values where $first_array[$i]
$language_option = array();
$first_array = Languages::getFullSelectOptionsList();
$second_array = Languages::getFullSelectOptionsList(TRUE);
for($i=0;$i<count($first_array); $i++){
$language_option[$country_code] = $first_array[$i].' - '.$second_array[$i];
}
Instead of $first_array[$i].' - '.$second_array[$i] put code according to your array structure to get description or code (key value).
i have an array result. i want to print 2 rows(product data) in first page.
next 2 rows in second page and so on. if anybody knows this,please help me to solve it
my array
$data['product_list']
foreach($data['product_list'] as $dat)
{
echo $dat->prd_id;
echo $dat->prd_name;
}
You are doing a foreach loop on an associative array and then trying to access the the contents as objects by using ->. I can only given assumption of what you might be doing. If your array is already populated with a name and id like you have described in your foreach loop this is how you would access the contents in the loop:
foreach($data['product_list'] as $dat)
{
echo $dat['prd_id'];
echo $dat['prd_name'];
}
That is how you would print out the contents providing you had the data stored in your array like so:
$data['product_list'][0] = array('prd_id'=>'id0','prd_name'=>'name0');
$data['product_list'][1] = array('prd_id'=>'id1','prd_name'=>'name1');
$data['product_list'][2] = array('prd_id'=>'id2','prd_name'=>'name2');
Better you try with array_slice();
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,2));
?>
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']);
}