I'm trying to fill an array with my foreach loop, but I don't get it to work. What am I doing wrong?
$a = array();
$activities = Project::getProjectnames($_DB, $projectnaam);
if(!empty($activities)) {
foreach($activities as $k => $v) {
$a .= array_fill($v['name']);
}
}
All I get back is the string Array...
you're concatenating a string there. You need to push the item into the array.
foreach($activities as $k => $v) {
$a[] = $v['name'];
}
Related
I want to remove or hide duplicate values in loop , It's not array.
You can see in picture 1/3, & 2/2 is repeating so I want only once using loop. It's not return any array it's simple data so we can't use array_unique
$i=1;
$result = array();
foreach ($boxes as $key => $value) {
foreach ($result as $k => $val) {
echo $i."/".count($value).'</br>';
}
$i++;
}
Expected Output
REG-Pre-Cut Short 1/3
REG-Pre-Cut Long -
PREM-Pre-Cut Short -
PREM-Pre-Cut Short 2/2
PREM-Pre-Cut Long -
Try something like this.
First you create an array in the expected format.
Then use array_unique to remove duplicates.
Then you print out the array.
$i=1;
$result = array();
$outputResult = array();
foreach ($boxes as $key => $value) {
foreach ($result as $k => $val) {
$outputResult[] = $i."/".count($value).'</br>';
}
$i++;
}
$outputResult = array_unique($outputResult);
foreach ($outputResult as $result) {
echo $result;
}
its easy for sure..
i have code like this:
$indeks = 0;
foreach ($list as $k => $v)
{
$data['fname'] = $customer->firstname;
$data['lname'] = $customer->lastname;
$data['code'] = $code['code'];
$tablica[$indeks] = $data;
$indeks++;
and i want to read only 'code' value for each array.
i try:
foreach($tablica as $k => $v){
foreach ($v as $key => $value ) {
echo $value
}
}
but i get all arrays values.
when i try
foreach($tablica as $k => $v){
foreach ($v['code'] as $key => $value ) {
echo $value
}
}
i have nothing...
thx for help
You can use array_column function to get all values of column, for example:
foreach (array_column($tablica, 'code') as $value) {
echo $value;
}
I think a For loop should help
for($i=0;$i<count($tablica);$i++){
echo $tablica[$i]['code'];
}
or get all Codes into an Array
$code = array();
for($i=0;$i<count($tablica);$i++){
$code[$i] = $tablica[$i]['code'];
}
You don't need nested loops.
foreach ($tablica as $value) {
echo $value['code'];
}
DEMO
Tried using array_values but it only temporary.
controller
foreach($rows as $key => $value)
{
array_values($value);
//dd shows the key changes to [0], [1], [2] and so on
}
You can do it like this,
$rows = array_map(function($v){return array_values($v);}, $rows);
Something like this should work:
$new = [];
foreach($rows as $key => $value)
{
array_values($value);
$sub = [];
foreach ($value as $subKey => $subValue) {
$subKey = $key;
$sub[$key] = $subValue;
}
$new[$key] = $sub;
//dd shows the key changes to [0], [1], [2] and so on
}
Then return $new instead of $rows.
Since you're using laravel you can also do:
$rows = collect($rows)->map(function ($value) {
return Arr::accessible($value)?collect($value)->values()->all():$value;
})->all();
If you are trying to change the associative array to an indexed array, do this:
$array = array_values($array);
I have foreach loop like:
foreach($attributes as $key => $value)
{
$option[] =["$value->name"=>"$value->value"]; //it is like ["color"=>"red"]
}
I want to merge $option[0], $option[1] and so on.... How to merge that ?
I tried:
for($i=1;$i<$count;$i++)
{
$option = array_merge($option[0],$option[$i]);
}
If you want a merged version, try this (you need only 1 loop):
$merged_options = array();
foreach($attributes as $key => $value)
{
$option[] =["$value->name" => "$value->value"];
$merged_options[$value->name] = $value->value;
}
This code should hopefully loop through each of your current arrays and reconstruct it to a multi-dimensional array.
foreach($attr as $k=>$v):
$temp = array();
$i = 0;
while(count($k) != $i):
array_push($temp, $k[$i] => $v[$i]);
$i++;
endwhile;
array_push($attr, $temp);
endforeach;
Hope it helped.
Why not you use something like this:
foreach($attributes as $key => $value)
{
$option[$value->name] =$value->value;
}
I have two array.
1st array is $newarray = ('489289', '536516', '332833', '536516')
2nd array is
$rockin = array(
'489289' => array('536516','value1'),
'332833' => array('536516'),
);
I want to delete some value of $newarray.
Suppose we are looping from $newarray
Initially 489289 is assigned value.
I want to check whether the value associated to 489289 from $rockin array (i.e. value1 or 536516) also exist in $newarray.
If there is exist 'value1' or '536516' in $newarray then, delete 489289 from array!
So in above case 489289 would be deleted (from $newarray)
AS 536516 is associated value of 489289 in $rockin array AND 536516 also exist in $newarray
Till now I have tried this code
foreach ($newarray as $group_id) {
foreach ($rockin as $myfrcikingcl) {
foreach ($myfrickingcl as $myfrickingleader) {
if($group_id==$myfrickingleader)
{
unset($newarray[$group_id]);
}
}
}
}
This is what I understood you want to do:
$newarray = array('489289', '536516', '332833', '536516');
$rockin = array(
'489289' => array('536516','332833'),
'332833' => array('536516'),
);
foreach ($rockin as $array) {
foreach ($array as $value) {
if (in_array($value, $newarray)) {
$key = array_search($array, $rockin);
$newarray = array_diff($newarray, array($key));
}
}
}
foreach ($newarray as $k => $v) {
if(is_array($rockin[$v])){
foreach ($rockin[$v] as $key => $value) {
if(in_array($value, $newarray)){
unset($newarray[$k]);
}
}
}
}
You're using $group_id as a key, but it's a value. You have to unset by key, like this:
foreach ($i = 0; $i < count($newarray); $i++) {
foreach ($rockin as $myfrcikingcl) {
foreach ($myfrickingcl as $myfrickingleader) {
if ($newarray[$i] == $myfrickingleader) {
unset($newarray[$i]);
}
}
}
}