This question already has answers here:
Summing of all elements in the sub array - PHP
(3 answers)
Closed 1 year ago.
I have a mulit demensional array like this ...
** note the array has closing brackets which is not shown in this image. so theres no issue in the syntax.
I want to add the values in each key (Openness, Conscientiousness) together so that i have a array like :
Array{
[Openness]=> Array(
[0] => 16
)
[Conscientiousness]=>Array (
[0]=> 10
)
}
When i tried this code after looking through existing questions :
$sumArray = array();
foreach ($finalarr as $k=>$subArray) {
foreach ($subArray as $id=>$value) {
//$sumArray[$id]+=$value;
array_key_exists( $id, $sumArray ) ? $sumArray[$id] += $value : $sumArray[$id] = $value;
}
}
print_r($sumArray);
I get :
which is not what i want. ANy ideas how to fix the array?
You can do it with array_sum() and one loop:
$sumArray = array();
foreach ($finalarr as $k => $subArray) {
$sumArray[$k] = array_sum($subArray);
}
If you really need the elements of $sumArray to be arrays rather than just the sums, it becomes:
$sumArray = array();
foreach ($finalarr as $k => $subArray) {
$sumArray[$k] = array(array_sum($subArray));
}
But I'm not sure why you would want that.
Okay as suggested in the comments i used the array_sum and it worked. I changed the foreach to :
**removed the inner loop which was unnecessary
foreach ($finalarr as $k=>$subArray) {
$finalarr[$k]=array_sum($subArray);
}
and it gave me the output :
Array{
[Openness]=> 16
[Conscientiousness]=> 10
}
Thanks for the comments !!
Related
I need help for showing result in multidimensional array.
On first array I want to show 8 results, then And next result I want to display 4 items
Here's my code :
foreach ($collection as $co) {
$value = 8;
$items[] = $this->ProductModel->Products($co->id, $value, 'product_date');
}
The result I expected, $value on first array is 8, but the others is 4.
How to make conditions for $value?
Thank you
Presuming your $collection is a plain list:
foreach ($collection as $i => $co) {
$value = ( $i ? 4 : 8 );
$items[] = $this->ProductModel->Products($co->id, $value, 'product_date');
}
This question already has answers here:
Array Foreach Loop Prints Last Item Only [closed]
(4 answers)
Closed 9 months ago.
I'm attempting to make a foreach loop to iterate over each item in an array, but it only captures the last item and does not iterate over the first one. I've stripped down the code to only show the relevant parts and added some commands to identify the problem as described above.
$message == "kk,ll";
$myArray = explode(',', $message);
print_r ($myArray);
foreach ($myArray as $value);
{
echo "$value <br>";
$array[] = $value;
}
print_r ($array);
The output is:
Array ( [0] => kk [1] => ll ) ll
Array ( [0] => ll )
You can see that when I use print_r() the array contains two items. But the foreach loop only loops over the last item. Adding the array elements into a new array inside the loop also ends up with an array containing only the last element. What am I doing wrong?
You have two mistakes in you code:
In your first line you have two equal signs which should only be one.
In your foreach loop, you have by mistake put an semicolon at the end:
foreach ($myArray as $value);
Doing this, the foreach loop will run, but the code inside the {} is actually placed outside the foreach loop, and thereby causing $value only to store the last element of the array.
The code should look like this:
$message = "kk,ll";
$myArray = explode(',', $message);
print_r ($myArray);
foreach ($myArray as $value) {
echo "$value <br>";
$array[] = $value;
}
print_r ($array);
your foreach just assigned the $value, but output nothing. This is caused by the ; after foreach, same as
foreach ($myArray as $value)
{}
And after this, the $value have the last element of $myArray, then
{
echo "$value <br>";
$array[] = $value;
}
only output the last element.
remove the ; after the Foreach like in the follow code
foreach ($myArray as $value)
{
echo "$value <br>";
$array[] = $value;
}
In Laravel Framework Use Code into Controller:
$dd = $categories->pluck( 'title' )->toArray();
foreach ( $dd as $key => $value ) {<br />
$array[$key] = '.' . $value;<br />
} <br />
$cat = implode( ',' , $array );
<br />
// Result Display : James,Mark,Helmet.....
Only remove the semicolon after foreach ($myArray as $value) or used it
im trying to mix my multi dimension arrays, and it iterates fine, but the output isnt what im trying to accomplish, i need to mix the values.
array= [ [ p ,t ,j ] , [ 9 , 3 , 6 ] ];
foreach($array as $value) {
foreach($value as $key => $val) {
echo $val;
}
}
}
array output: p,9,t,3,j,6 //should be
Mine is: p,t,j,9,3,6
Simplest approach
foreach($array[0] as $key => $value) {
echo $value, $array[1][$key];
}
if i understand you this is your answer:
//creat an empty array to save the new result
$result= array();
//do this for incrementing
$i=0;
//your arrays here and looping it
$array=array(array( p ,t ,j ) , array( 9 , 3 , 6 ) );
foreach($array as $a){
if(is_array($a)){
foreach($a as $b){
$result[$i]= $b;
}//end foreach
}else{
$result[$i]= $a;
}//end else
$i++;
}//end foreach
//then print_r to show your array
print_r($result);
have a nice day ^_^
by 'mix' do you mean trying to combine the secondary arrays into one long array? If that is the case:
$finalArray = array();
foreach($array as $value) {
$finalArray = array_merge($finalArray, $value);
}
edit: now that I look at it, I didn't quite echo the output like you needed, but the output should be in the correct order in the $finalArray and this should work with any amount of inner arrays.
This question already has answers here:
Finding the subsets of an array in PHP
(5 answers)
Closed 7 years ago.
I will do my best to explain this idea to you. I have an array of values, i would like to know if there is a way to create another array of combined values. So, for example:
If i have this code:
array('ec','mp','ba');
I would like to be able to output something like this:
'ec,mp', 'ec,ba', 'mp,ba', 'ec,mp,ba'
Is this possible? Ideally i don't want to have duplicate entries like 'ec,mp' and 'mp,ec' though, as they would be the same thing
You can take an arbitrary decision to always put the "lower" string first. Once you made this decision, it's just a straight-up nested loop:
$arr = array('ec','mp','ba');
$result = array();
foreach ($arr as $s1) {
foreach ($arr as $s2) {
if ($s1 < $s2) {
$result[] = array($s1, $s2);
}
}
}
You can do it as follows:
$arr = array('ec','mp','ba', 'ds', 'sd', 'ad');
$newArr = array();
foreach($arr as $key=>$val) {
if($key % 2 == 0) {
$newArr[] = $val;
} else {
$newArr[floor($key/2)] = $newArr[floor($key/2)] . ',' . $val;
}
}
print_r($newArr);
And the result is:
Array
(
[0] => ec,mp
[1] => ba,ds
[2] => sd,ad
)
Have you looked at the function implode
<?php
$array = array('ec','mp','ba');
$comma_separated = implode(",", $array);
echo $comma_separated; // ec,mp,ba
?>
You could use this as a base for your program and what you are trying to achieve.
This question already has answers here:
php looping through multiple arrays [duplicate]
(8 answers)
Closed 9 years ago.
How can I iterate through two arrays at the same time that have equal sizes ?
for example , first array $a = array( 1,2,3,4,5);
second array $b = array(1,2,3,4,5);
The result that I would like through iterating through both is having the looping process going through the same values to produce a result like
1-1
2-2
3-3
4-4
5-5
I tried to do it this way below but it didn't work , it keeps going through the first loop again
foreach($a as $content) {
foreach($b as $contentb){
echo $a."-".$b."<br />";
}
}
Not the most efficient, but a demonstration of SPL's multipleIterator
$mi = new MultipleIterator();
$mi->attachIterator(new ArrayIterator($a));
$mi->attachIterator(new ArrayIterator($b));
$newArray = array();
foreach ( $mi as $value ) {
list($value1, $value2) = $value;
echo $value1 , '-' , $value2 , PHP_EOL;
}
Use a normal for loop instead of a foreach, so that you get an explicit loop counter:
for($i=0; $i<count($content)-1; $i++) {
echo $content[$i].'-'.$contentb[$i];
}
If you want to use string based indexed arrays, and know that the string indexes are equal between arrays, you can stick with the foreach construct
foreach($content as $key=>$item) {
echo $item.'-'.$contentb[$key];
}
If they're the same size, just do this:
foreach($a as $key => $content){
$contentb = $b[$key];
echo($content."-".$contentb."<br />");
}