suppose I have an array like this :
Array
(
[0] => Array
(
[0] => A
[1] => 20
)
[1] => Array
(
[0] => B
[1] => 10
)
[2] => Array
(
[0] => G
[1] => 5
)
[3] => Array
(
[0] => A
[1] => 15
)
)
I would like to remove duplicate values and sum just a row of array :
What I want :
Array
(
[0] => Array
(
[0] => A
[1] => 35 // <= sum : 20 + 15
)
[1] => Array
(
[0] => B
[1] => 10
)
[2] => Array
(
[0] => G
[1] => 5
)
)
I've read this question before.
updated
while($row = $stmt->fetch()){
$arr = array(
'GoodMainCode'=>persian_sql_to_php($row['GoodMainCode']), // <= like A in the example
'title'=> persian_sql_to_php($row['GoodName']),
'author'=>persian_sql_to_php($row['moalef']),
'publisher'=>persian_sql_to_php($row['Nasher']),
'translator'=>persian_sql_to_php($row['Motarjem']),
'price'=>persian_sql_to_php($row['SellPrice1']),
'isbn'=>persian_sql_to_php($row['ISBN']),
'amount'=>persian_sql_to_php($row['Amount']), // <= if GoodMainCode is same key, I must sum it.
'year_of_publish'=>persian_sql_to_php($row['SaleChap']),
'period_print'=>persian_sql_to_php($row['NobateChap'])
);
array_push($mjson,$arr);
}
//added
foreach($mjson as $v){
if(!isset($result[$v['GoodMainCode']]))
$result[$v['GoodMainCode']] = $v;
else
$result[$v['GoodMainCode']]['amount'] += $v['amount'];
}
This should work for you:
Just loop through your array and check if in your $result array is a key with the letter of the current inner Array from $arr. If not add it to the $result array and initialize the second key with the number.
If there is already a key with this letter you can simply add the numbers together in this array. At the end I simply use array_values() to reindex the entire array.
<?php
foreach($arr as $v) {
if(!isset($result[$v[0]]))
$result[$v[0]] = $v;
else
$result[$v[0]][1] += $v[1];
}
$result = array_values($result);
print_r($result);
?>
output:
Array
(
[0] => Array
(
[0] => A
[1] => 35
)
//...
[2] => Array
(
[0] => G
[1] => 5
)
)
Related
I have an array in PHP as below:
`
Array (
[0] => Array
(
[0] => Some text
[1] => 6230.C3
)
[1] => Array
(
[0] => Some text
[1] => 6230.C3
)
[2] => Array
(
[0] =>
[1] =>
)
[3] => Array
(
[0] => Some text
[1] =>
)
[4] => Array
(
[0] => Some text
[1] =>
)
[5] => Array
(
[0] => Some text
[1] =>
)
[6] => Array
(
[0] =>
[1] =>
)
[7] => Array
(
[0] => Some text
[1] =>
)
[8] => Array
(
[0] => Some text
[1] =>
)
)
`
Array[2] and Array[6] are empty arrays. Please help me split my array to three arrays array1, array2, array3 as below (split at position of Array[2] and Array[6]).
array1 = [ Array[0], Array[1] ]
array2 = [ Array[3], Array[4], Array[5] ]
Aarray3 = [ Array[7], Array[8] ]
Thank you for your help.
This is a little more flexible than your original question because it is not limited to exactly 3 arrays as separate variables. The result of the following would be an multidimensional array, split in the way that you want but for an unlimited number of splits. I can rework it if necessary to fit exactly 3 variables if that is really what your input will always be.
<?php
$final = array();
$counter = 0;
$outerEmpty = false;
foreach($originalArray as $outer) {
$innerEmpty = true;
foreach($outer as $inner) {
if (!empty($inner)) {
$innerEmpty = false;
break;
}
}
if (!$innerEmpty)
$final[$counter][] = $outer;
else
$counter++;
}
I have a multi-dimensional array. Since the value of the string "volvo" is present twice, I want to combine those keys. Here's the source array:
Array
(
[0] => Array
(
[0] => Volvo
[1] => 22
)
[1] => Array
(
[0] => BMW
[1] => 15
)
[2] => Array
(
[0] => Saab
[1] => 5
)
[3] => Array
(
[0] => Volvo
[1] => 17
)
)
and I'd like to convert it to this one:
Array
(
[0] => Array
(
[0] => Volvo
[1] => 39
)
[1] => Array
(
[0] => BMW
[1] => 15
)
[2] => Array
(
[0] => Saab
[1] => 5
)
)
I think this would make more sense to return an associated array, that way you can do $arr["volvo"], if you're fine with an associated array, just remove the second foreach loop.
If not, this will get the correct output:
<?php
$arr = Array (
Array (
"Volvo",
22
),
Array (
"BMW",
15
),
Array (
"Saab",
5
),
Array (
"Volvo",
17
)
);
$tmpNewArr = Array();
foreach ($arr as $ele) {
if (!isset($arr[$ele[0]])) {
$tmpNewArr[$ele[0]] = 0;
}
$tmpNewArr[$ele[0]] += $ele[1];
}
$newArr = [];
foreach ($tmpNewArr as $key => $ele) {
array_push($newArr,[$key,$ele]);
}
var_dump($newArr);
?>
Here's an eval.in:
https://eval.in/766340
$keyValueCars = [];
foreach($cars as $car){
$brand = $car[0];
$total = $car[1];
if(!isset($keyValueCars[$brand])){
$keyValueCars[$brand] = total;
}
else{
$keyValueCars[$brand] += total;
}
}
You could use
array_unique(Your_array, SORT_REGULAR);
I have an array like this:
Array
(
[0] => Array
(
[0] => 1
[1] => Firstname one
[2] => Lastname one
)
[1] => Array
(
[0] => 2
[1] => Firstname two
[2] => Lastname two
)
[2] => Array
(
[0] => 3
[1] => Firstname three
[2] => Lastname three
)
)
Now, I would like to remove the entire index 1, i.e., all the firstname's from the array. I was planning on using array_splice by looping through the entire array and removing its index 1. But is there a better way. (I want the re-indexing after deletion of elements.)
$yourarray = array_map(function($el) {
unset($el[1]); //remove index 1
return array_values($el); //return and reindex
}, $yourarray);
You can also use array_slice function like
foreach($array as $k=>$v)
{
array_splice($v, 1,1);
$array[$k] = $v;
}
OUTPUT :
Array
(
[0] => Array
(
[0] => 1
[1] => Lastname one
)
[1] => Array
(
[0] => 1
[1] => Lastname two
)
[2] => Array
(
[0] => 1
[1] => Lastname three
)
)
You should use array_map
$input_array = [[1,'Firstname one','Lastname one'],
[2,'Firstname two','Lastname two'],
[3,'Firstname three','Lastname three']];
$resultArray = array_map(function($record) {
return [$record[0], $record[2]]; // add your desired records
}, $input_array);
echo '<pre>'; print_r($resultArray);
output:-
Array
(
[0] => Array
(
[0] => 1
[1] => Lastname one
)
[1] => Array
(
[0] => 2
[1] => Lastname two
)
[2] => Array
(
[0] => 3
[1] => Lastname three
)
)
foreach($array as $k=>$v)
{
unset($array[$k][1]); /// Chnage the key of array which you want to remove
}
I have a big problem with a array and I want to group by key (or I don't now :( )
I get the links from mySQL and I do the following:
$lien2 = $links2['text'];
$lien2 = stripslashes($lien2);
$lien2 = htmlspecialchars($lien2);
$lien2 = nl2br($lien2);
preg_match_all('#http://.*?\.?([a-zA-Z0-9\-]+)(\.[a-zA-Z0-9]+)/[a-zA-Z0-9\/\*\-\?\&\%\=\,\.\;\#\_]+#i', $lien2, $lien2_result, PREG_SET_ORDER);
This is the array $lien2_result:
Array
(
[0] => Array
(
[0] => links1
[1] => A
)
[1] => Array
(
[0] => links2
[1] => B
)
)
Array
(
[0] => Array
(
[0] => links3
[1] => C
)
)
Array
(
[0] => Array
(
[0] => links4
[1] => B
)
)
Array
(
[0] => Array
(
[0] => links5
[1] => D
)
)
Array
(
[0] => Array
(
[0] => links6
[1] => E
)
)
and I want to get the following result:
A
links1
B
links2
links4
C
links3
D
links5
E
links6
I would adjust the query personally but if you are stuck with this resultset you could rewrite it with
foreach($lien2_result as $lien2){
foreach($lien2 as $item){
$arr[$item[1]][] = $item[0];
}
}
where print_r($arr) would result something like:
$arr = Array('A' => Array('links1'), 'B' => Array('links2','links4')); //and so on..
And actual printing the way you asked:
foreach($arr as $name => $value){
echo($name.'<br />');
foreach($value as $item){
echo($item.'<br />');
}
}
EDIT
Here's an example
http://sandbox.onlinephpfunctions.com/code/c0da893797cb2049e8346168b280a9f5b1fa145b
i have a multidimensional array whose index/keys (not the values) are like this:
this is how the submitted array looks
[param] => Array
(
[3] => groupedlista
[0] => groupedlistb
[2] => groupedlistc
)
[f_name] => Array
(
[3] => grouplistaa
[0] => grouplistbb
[2] => grouplistcc
)
[f_label] => Array
(
[3] => grouplistL3
[0] => grouplistL0
[2] => grouplistL2
)
this is how the order looks
0,2,3
i want that Result
[param] => Array
(
[0] => groupedlistb
[1] => groupedlistc
[2] => groupedlista
)
[f_name] => Array
(
[0] => grouplistbb
[1] => grouplistcc
[2] => grouplistaa
)
[f_label] => Array
(
[0] => grouplistL0
[1] => grouplistL2
[2] => grouplistL3
)
that's it
PS: i use a jquery sort / add / delete feature in the form and i prefer to do the final sorting php-based. the index array [$i] is required to be declared at the form.
$order = '0,2,3';
$out = array(); // This will hold the sorted values
$order = explode(',',$order); // Turn the order into an array
foreach ($multiDimArray as $key => $subArray) { // Loop outer array
foreach ($order as $pos) { // Loop order array
if (isset($subArray[$pos])) { // Make sure the key exists
$out[$key][] = $subArray[$pos]; // Put the correct value in the correct place
}
}
}
print_r($out);