add values in php based off first value php - php

I have an array as shown below which some rows will have the same name. I am getting nowhere fast trying to do what i want to achieve.
With a foreach loop how i would go through each one add the values of the same name up and put it into a new array?
Thanks :)
Array
(
[0] => Array
(
[0] => Name1
[1] => Value
)
[1] => Array
(
[0] => Name2
[1] => Value
)
[2] => Array
(
[0] => Name1
[1] => Value
)
[3] => Array
(
[0] => Name3
[1] => Value
)
[4] => Array
(
[0] => Name2
[1] => Value
)
This only a snippet and will change to length when the csv it comes from changes weekly Thanks

It's not the most efficient way yet but it's working the way you want it to happen.
<?php
$testArr = array(
array('Name1', 1),
array('Name2',2),
array('Name1',3)
);
$newArr = array();
$tmp = array();
foreach($testArr as $i=>$row)
{
if( in_array($row[0], $tmp) )
{
$key = array_search($row[0], $tmp);
$newArr[ $key ][1] += $row[1];
}
else
{
array_push( $newArr, array($row[0], $row[1]) );
array_push( $tmp, $row[0] );
}
}
print_r($newArr);
?>
OUTPUT
Array
(
[0] => Array
(
[0] => Name1
[1] => 4
)
[1] => Array
(
[0] => Name2
[1] => 2
)
)
Tested working, Check it here, phpFiddle

Do you want something like this?
$newData = [];
foreach ($array as $item) {
$newData[$item[0]][] = $item[1];
}
var_dump($newData);

You can get to the result with this approach.
$data_array = Array{};
$result_array = NULL;
foreach ($data_array as $value) {
$result_array[$value[0]] += $value[1];
}

Related

Change value inside an array

I'd like to change the values of an array.
Currently my array looks like this:
Array
(
[0] => Array
(
[0] => 12-Multi_select-customfield-retina-ready+Yes
[1] => 12-Multi_select-customfield-retina-ready+N/A
[2] => 12-Multi_select-customfield-retina-ready+No
)
)
I want to remove everything before the + symbol, so in the end the new array will looke like this
Array
(
[0] => Array
(
[0] => Yes
[1] => N/A
[2] => No
)
)
This is my code:
$new_array = array();
foreach( $array as $key => $value ) {
$split = explode("+", $value[0]);
$new_array[] = $split[1];
}
Hoping that it would worked, but when I check the new array, it only shows one value.
Array
(
[0] => Yes
)
Any help in putting me in the right direction is much appreciated.
Please, check it:
<?php
$array[0][0] = '12-Multi_select-customfield-retina-ready+Yes';
$array[0][1] = '12-Multi_select-customfield-retina-ready+N/A';
$array[0][2] = '112-Multi_select-customfield-retina-ready+No';
echo '<pre>';
print_r($array);
$new_array = array();
foreach( $array[0] as $key => $value ) {
$split = explode("+", $value);
$new_array[] = $split[1];
}
print_r($new_array);
echo '</pre>';
Try This this work even if you have multiple key in the original array $original_array[0], $original_array[1] ... :
$original_array[0] = [
0 => '12-Multi_select-customfield-retina-ready+Yes',
1 => '12-Multi_select-customfield-retina-ready+N/A',
2 => '12-Multi_select-customfield-retina-ready+No'
];
print_r($original_array);
$new_array = [];
foreach ($original_array as $key => $value) {
foreach ($value as $index => $val) {
$split = explode("+", $val);
$new_array[$key][] = $split[1];
}
}
print_r($new_array);
Example :
Original array
Array
(
[0] => Array
(
[0] => 12-Multi_select-customfield-retina-ready+Yes
[1] => 12-Multi_select-customfield-retina-ready+N/A
[2] => 12-Multi_select-customfield-retina-ready+No
),
[1] => Array
(
[0] => 12-Multi_select-customfield-retina-ready+Yes
[1] => 12-Multi_select-customfield-retina-ready+N/A
[2] => 12-Multi_select-customfield-retina-ready+No
)
)
New Array
Array
(
[0] => Array
(
[0] => Yes
[1] => N/A
[2] => No
),
[1] => Array
(
[0] => Yes
[1] => N/A
[2] => No
)
)

php array unique values using array_unique

Array
(
[0] => Array
(
[0] => abc#test.com
[1] => qwrt#sometest.com
[2] => haritha#elitesin.com
)
[1] => Array
(
[0] => Kanishka.Kumarasiri#elitesin.com
[1] => Haritha#elitesin.com
)
[2] => Array
(
[0] => Kanishka.Kumarasiri#elitesin.com
[1] => test#elitesin.com
)
)
I have an array like this and I want to get unique values from this array.
But my code is failing
for ($i = 0; $i < count($return_arr); $i++) {
$new_value[] = explode(",", $return_arr[$i]);
}
print_r (array_unique($new_value));
It says array to string conversion error
i want the array to be like this get only the unique email ids
Array
(
[0] => Array
(
[0] => abc#test.com
[1] => qwrt#sometest.com
[2] => haritha#elitesin.com
[3] => Kanishka.Kumarasiri#elitesin.com
[4] => test#elitesin.com
)
)
Because your code is wrong, you are trying to explode something which is not contained in your array, try this code:
<?php
$arr = array("0"=>array("abc#test.com","qwrt#sometest.com","haritha#elitesin.com"),
"1"=>array("Kanishka.Kumarasiri#elitesin.com,Haritha#elitesin.com"),
"2"=>array("Kanishka.Kumarasiri#elitesin.com,test#elitesin.com"));
$allEmails = array();
foreach($arr as $array){
foreach($array as $email){
$allEmails[] = explode(",",$email);
}
}
$new_value = array();
foreach($allEmails as $array){
foreach($array as $email){
$new_value[] = strtolower($email);
}
}
print_r (array_unique($new_value));
?>

How to Sort all data from array

how can i crawl this array with a for loop or foreach ,
this is the content of a single array:
Array ( [0] => 1 ) Array ( [0] => 1 [1] => 2 ) Array ( [0] => 1 [1] => 2 [2] => 3 )
If you mean what I think you mean...
try this:
$tempArray = array();
foreach($myarray as $k => $v){
foreach($v as $ke => $val ){
$tempArray[] = $val;
}
}
sort($tempArray);
Or if you want to avoid duplicates try:
$temp = array_unique($myArray);
sort($temp);

php multidimension array values to variables

I'm overlooking something but I've tried everything I can think of to get these values into variables.
Existing array is created like this:
$compdata[] = array($currentTime,$indiff,$outdiff,$totaldiff);
And this is the dump of the array:
Array (
[0] => Array ( [0] => 1385955600 [1] => 29749073 [2] => 116376416 [3] => 146125489 )
[1] => Array ( [0] => 1385956200 [1] => 2628480405 [2] => 18073170501 [3] => 20701650906 )
[3] => Array ( [0] => 1385957400 [1] => 2728527955 [2] => 16495107227 [3] => 19223635182 )
)
My question is how to get these values with a foreach or while loop into variables like:
$time = $value[0];
$inbound = $value[1];
$outbound = $value[2];
$total = $value[3];
I know I have to do something to get the nested values. . . .
Much appreciated.
You're literally just missing the loop:
foreach ($compdata as $value) {
$time = $value[0];
$inbound = $value[1];
$outbound = $value[2];
$total = $value[3];
}
You can do this using list, as
foreach ($compdata as $array)
{
list($time,$inbound,$outbound,$total)=$array;
}

php multidimensional array, implode twice?

I need some help trying to implode my multidimensional array twice. I'm using Joomla 2.5 and it's for a backend component..
Here's what the array is:
Array
(
[jform] => Array
(
[options] => Array
(
[colour] => Array
(
[0] => a
[1] => d
)
[size] => Array
(
[0] => b
[1] => e
)
[qty] => Array
(
[0] => c
[1] => f
)
)
)
)
I've tried using the following code:
$i=0;
$optchildArr = array();
$optchildArrX = array();
foreach ($this->options as $optArr) :
$j=0;
foreach ($optArr as $arr) :
$optchildArrX[] = $arr;
$j++;
endforeach;
$optchildArr[$i] = implode(',',$optchildArrX);
$i++;
endforeach;
$this->options = implode(';',$optchildArr);
But I'm getting these kind of results:
[options] => Array
(
[0] => a,d
[1] => a,d,b,e
[2] => a,d,b,e,c,f
)
When I'm after:
[options] => Array
(
[0] => a,b,c
[1] => d,e,f
)
Any help would be greatly appreciated!! :)
Assuming the main array is $A
function mergeArrays($colour, $size, $qty) {
$result = array();
for ($i=0; $i<count($colour); $i++) {
//Assuming three arrays have the same length;
$result[] = implode(',',array($colour[$i], $size[$i], $qty[$i]));
}
return $result;
}
$result_array = array_map(
'mergeArrays',
$A['jform']['options']['colour'],
$A['jform']['options']['size'],
$A['jform']['options']['qty']
);
//Check the output from this, should be the output you described.
var_dump($result_array);

Categories