Input :
Array
(
[1] => Array
(
[id] => 11
[has_point] => 0
[xpr] => 0
[war] => 0
[sty] => 0
)
[2] => Array
(
[id] => 52
[has_point] => 0
[xpr] => 0
[war] => 0
[sty] => 0
)
[3] => Array
(
[id] => 34
[has_point] => 1
[xpr] => 300
[war] => 0
[sty] => 0
)
[4] => Array
(
[id] => 47
[has_point] => 0
[xpr] => 0
[war] => 0
[sty] => 0
)
[5] => Array
(
[id] => 57
[has_point] => 1
[xpr] => 500
[war] => 0
[sty] => 0
)
)
Output:
Array
(
[1] => 11
[2] => 52
[3] => 34
[4] => 47
[5] => 57
)
So is there any function in PHP to obtain this output so i don't have to write my own.
I tried
function customArraySearch($array, $search) {
$col = array();
foreach ($array as $v) {
$col[] = $v[$search];
}
return $col;
}
I am getting my output but I want to optimize my code so if there are 10000 records in array it take some time. Please suggest any inbuilt php function
The function you need is array_column.
$id_array = array_column($your_array, 'id');
Note: Supported version is PHP 5 >= 5.5.0
Try this...
$aa = array(
array(11, 0,0,0,0),
array(52, 0,0,0,0),
array(34, 0,0,0,0),
array(47, 0,0,0,0),
array(57, 0,0,0,0)
);
$newarray=array();
foreach( $aa as $k => list($a, $b,$c,$d,$e)) {
$newarray[]=$a;
}
print_r($newarray);
Supported version is (PHP 5 >= 5.5.0)
Related
My scenario is i have an multidimensional array and i want to get separated array from multidimensional array.
Ex:- First array return which is all values have 100 or more that key and value return as a separated and Second if array has one value less than 100 and other values have a 100 and more that array key and value return as a separated see below example,
Array(
[124] => Array
(
[0] => 140
[1] => 101
[2] => 107
[3] => 116
[4] => 100
)
)
Array(
[164] => Array
(
[0] => 108
[1] => 111
[2] => 124
[3] => 87
[4] => 278
)
)
Array(
[162] => Array
(
[0] => 6
[1] => 79
[2] => 3
)
)
And output should be like this,
Array(
[164] => Array
(
[0] => 108
[1] => 111
[2] => 124
[3] => 87
[4] => 278
)
)
Array(
[162] => Array
(
[0] => 6
[1] => 79
[2] => 3
)
)
Solution with array_filter() and a function filter100(). For explanations of the filter function, see the comments there.
$data = [
124 => [140,101,107,116,100],
164 => [108,111,124,87,278],
162 => [6,79,3],
170 => [6],
171 => [45,34],
172 => [45,134],
];
function filter100($subArr){
$limit = 100;
if(min($subArr) >= $limit) return false; //all values >= 100
$count = count($subArr);
if($count >= 3) return true; //2 values < 100 or 2 values >= 100
//from here on only arrays with 0.1 or 2 values
if($count < 2 OR max($subArr) >= $limit) return false;
return true;
}
$newData = array_filter($data,'filter100');
//test output
echo '<pre>', var_export($newData,true);
Output:
array (
164 =>
array (
0 => 108,
1 => 111,
2 => 124,
3 => 87,
4 => 278,
),
162 =>
array (
0 => 6,
1 => 79,
2 => 3,
),
171 =>
array (
0 => 45,
1 => 34,
),
)
foreach ($total_progress as $key => $value) {
foreach ($value as $key1 => $value1) {
if($value1 > 100 ){
unset($total_progress[$key]);
}
}
}
I have tried with this code but it is removed all the 100 and 100 more values from array and return only 100 less value array. but i want to get array which is 100 and more with 100 less values.
So final output should be like this
Array(
[164] => Array
(
[0] => 108
[1] => 111
[2] => 124
[3] => 87
[4] => 278
)
[170] => Array
(
[0] => 63
[1] => 100
[2] => 45
[3] => 133
[4] => 243
[5] => 170
)
)
I want to display the unique pairs values in array.and to print the pair only if addition of that pair is even number.Language to be used is PHP.
As you aren't explain well in your question but I tried , Maybe helpful.
<?php
$range1 = range(1,2000);//change as per your requirement
$i = 5; //change as per your requirement
$UniqueEvenPairs = array();
while($i > 0){
shuffle($range1);
$addition = (($range1[0] + $range1[10]));
if($addition % 2 == 0){
$UniqueEvenPairs[$i] = array("val_1"=>$range1[0] , "val_2"=>$range1[10] , "addition" =>$addition);
$i--;
}
}
echo "<pre>";print_r($UniqueEvenPairs);
?>
OutPut
Array
(
[5] => Array
(
[val_1] => 836
[val_2] => 500
[addition] => 1336
)
[4] => Array
(
[val_1] => 293
[val_2] => 319
[addition] => 612
)
[3] => Array
(
[val_1] => 1604
[val_2] => 742
[addition] => 2346
)
[2] => Array
(
[val_1] => 432
[val_2] => 1606
[addition] => 2038
)
[1] => Array
(
[val_1] => 896
[val_2] => 1766
[addition] => 2662
)
)
My Array is below
$sample_arr = Array
(
[0] => Array
(
[index] => 0
[in_id] => 309
[date] => 2016-08-01
[article_Id] => 4
[qty] => 50
[ref] => ref
)
[1] => Array
(
[index] => 1
[in_id] => 309
[date] => 2016-08-01
[article_Id] => 6
[qty] => 20
[ref] => ref 2
)
[2] => Array
(
[index] => 2
[in_id] => 309
[date] => 2016-08-01
[article_Id] => 6
[qty] => 21
[ref] => ref 2
)
)
As you can see in the above array there is article_Id twice in the array with value 6
I would like to find the details of the second row so that I can make the second row qty 41 i.e my result array will be like the one below
I have tried the in_array function but still there is something missing
I also tried with the foreach but the problem is that how to get the first appearance row qty? i.e in the $sample_arr when the user adds the third record with article_Id 6 the second row must be updated as shown below
$result_arr = Array
(
[0] => Array
(
[index] => 0
[in_id] => 309
[date] => 2016-08-01
[article_Id] => 4
[qty] => 50
[ref] => ref
)
[1] => Array
(
[index] => 1
[in_id] => 309
[date] => 2016-08-01
[article_Id] => 6
[qty] => 41
[ref] => ref goes here
)
)
Looks like article ID is unique. So, we can kep storing data in this $newArray variable with the key as article ID.
Every time, it's checked if a record containing the article id exists. If so, the quantity is added. If not, it's appended to the $newArray.
$newArray = array();
foreach($sample_arr as $arr) {
if (isset($newArray[$arr['article_Id']])) {
$newArray[$arr['article_Id']]['qty'] += $arr['qty'];
$newArray[$arr['article_Id']]['ref'] = 'Ref goes here'; // If this the string that replaces the ref
} else {
$newArray[$arr['article_Id']] = $arr;
}
}
$newArray = array_values($newArray);
Output:
Array
(
[0] => Array
(
[index] => 0
[in_id] => 309
[date] => 2016-08-01
[article_Id] => 4
[qty] => 50
[ref] => ref
)
[1] => Array
(
[index] => 1
[in_id] => 309
[date] => 2016-08-01
[article_Id] => 6
[qty] => 41
[ref] => ref 2
)
)
Try the following:
$new_merged_array = array();
if(!empty($sample_arr )){
foreach($sample_arr as $sample_ar){
if(!empty($sample_ar) && isset($sample_ar['article_Id'])){
if(isset($new_merged_array[$sample_ar['article_Id']])){
$new_merged_array[$sample_ar['article_Id']]['qty'] += $sample_ar['qty'];
}else{
$new_merged_array[$sample_ar['article_Id']] = $sample_ar;
}
}
}
}
print_r($new_merged_array);
Output will be:
Array(
[4] => Array
(
[index] => 0
[in_id] => 309
[date] => 2016-08-01
[article_Id] => 4
[qty] => 50
[ref] => ref
)
[6] => Array
(
[index] => 1
[in_id] => 309
[date] => 2016-08-01
[article_Id] => 6
[qty] => 41
[ref] => ref 2
)
)
Another way:
$new_merged_array = $sample_array = array();
if(!empty($sample_arr )){
foreach($sample_arr as $sample_ar){
if(!empty($sample_ar) && isset($sample_ar['article_Id'])){
if(isset($new_merged_array[$sample_ar['article_Id']])){
$new_merged_array[$sample_ar['article_Id']]['qty'] += $sample_ar['qty'];
}else{
$new_merged_array[$sample_ar['article_Id']] = $sample_ar;
}
}
}
}
if(!empty($new_merged_array )){
foreach($new_merged_array as $new_merged_arr){
$sample_array[] = $new_merged_arr;
}
}
print_r($sample_array);
Output will be:
Array(
[0] => Array
(
[index] => 0
[in_id] => 309
[date] => 2016-08-01
[article_Id] => 4
[qty] => 50
[ref] => ref
)
[1] => Array
(
[index] => 1
[in_id] => 309
[date] => 2016-08-01
[article_Id] => 6
[qty] => 41
[ref] => ref 2
)
)
$sample_arr = array
(
0 => array
(
'index'=> 0,
'in_id' => 309,
'date' => '2016-08-01',
'article_Id' => 4,
'qty' => 50,
'ref' => 'ref'
),
1 => array
(
'index' => 1,
'in_id' => 309,
'date' => '2016-08-01',
'article_Id' => 6,
'qty' => 20,
'ref' => 'ref 2'
),
2 => array
(
'index' => 2,
'in_id' => 309,
'date' => '2016-08-01',
'article_Id' => 6,
'qty' => 21,
'ref' => 'ref 2'
)
);
$articleArray = [];
foreach($sample_arr as $key=>$value){
$articleId = $value['article_Id'];
if(array_key_exists($articleId,$articleArray)){
$articleArray[$articleId]['qty'] = $articleArray[$articleId]['qty'] + $value['qty'];
}else{
$articleArray[$articleId] = $value;
}
}
$articleArray = array_values($articleArray);
print_r($sample_arr);
print_r($articleArray);
Demo
in_array() does not work on multidimensional arrays. You could write a recursive function to do that for you:
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
Usage:
$b = array(array("Mac", "NT"), array("Irix", "Linux"));
echo in_array_r("Irix", $b) ? 'found' : 'not found';
This question already has answers here:
PHP Array Group by one field and Sum up two fields [duplicate]
(2 answers)
Closed 4 months ago.
My situation is similar to this thread :
Associative array, sum values of the same key
However in my case all keys are number.
I would like to reduce / combine array where key 0 is similar and make a sum of all other keys.
Here is my original array :
Array
(
[0] => Array
(
[0] => 093042
[1] => 3
[2] => 0
[4] => 0
)
[1] => Array
(
[0] => 222032
[1] => 0
[2] => 13
[4] => 0
)
[2] => Array
(
[0] => 222032
[1] => 0
[2] => 0
[4] => 15
)
[3] => Array
(
[0] => 152963
[1] => 45
[2] => 0
[4] => 0
)
[4] => Array
(
[0] => 222032
[1] => 0
[2] => 7
[4] => 0
)
)
and here is the output i need :
Array
(
[0] => Array
(
[0] => 093042
[1] => 3
[2] => 0
[4] => 0
)
[1] => Array
(
[0] => 222032
[1] => 0
[2] => 20
[4] => 15
)
[2] => Array
(
[0] => 152963
[1] => 45
[2] => 0
[4] => 0
)
)
The solution of other thread is not working because they use the key name and i don't know how i can adapt this to my situation.
Please if you can give me an example of working solution.
REPLY :
For now i try something like that : Take from other thread
$sum = array_reduce($data, function ($a, $b) {
if (isset($a[$b[0]])) {
$a[$b[0]]['budget'] += $b['budget'];
}
else {
$a[$b[0]] = $b;
}
return $a;
});
But this example look is only for key named budget but in my case is number and i have 3 key [1] [2] [3] how can't sum key 1,2,4 where key 0 is similar
This should work for you:
Basically I just loop through your array and check if there is already an element in $result with the key of the first element of $v. If not I initialize it with an array_pad()'ed array of 0's + the current array of the iteration of the foreach loop.
And after this I loop through each element of $v expect the first one and add it to the result array.
At the end I just reindex the result array with array_values().
<?php
foreach($arr as $v){
if(!isset($result[$v[0]]))
$result[$v[0]] = array_pad([$v[0]], count($v), 0);
$count = count($v);
for($i = 1; $i < $count; $i++)
$result[$v[0]][$i] += $v[$i];
}
$result = array_values($result);
print_r($result);
?>
output:
Array
(
[0] => Array
(
[0] => 093042
[1] => 3
[2] => 0
[3] => 0
)
[1] => Array
(
[0] => 222032
[1] => 0
[2] => 20
[3] => 15
)
[2] => Array
(
[0] => 152963
[1] => 45
[2] => 0
[3] => 0
)
)
try this
$data = Array
(
0 => Array
(
0 => 93042,
1 => 3,
2 => 0,
4 => 0,
),
1 => Array
(
0 => 222032,
1 => 0,
2 => 13,
4 => 0,
),
2 => Array
(
0 => 222032,
1 => 0,
2 => 0,
4 => 15,
),
3 => Array
(
0 => 152963,
1 => 45,
2 => 0,
4 => 0,
),
4 => Array
(
0 => 222032,
1 => 0,
2 => 7,
4 => 0,
),
);
var_dump($data);
// grouping
$tab1 = array();
foreach ($data as $e) {
if (!isset($tab1[$e[0]])) {
$tab1[$e[0]] = array();
}
$tab1[$e[0]][] = $e;
}
//var_dump($tab1);
// summing
$tabSum = array();
foreach ($tab1 as $id => $t) {
foreach ($t as $e) {
unset($e[0]);
if (!isset($tabSum[$id])) {
$tabSum[$id] = $e;
} else {
foreach ($e as $key => $value) {
$tabSum[$id][$key] += $value;
}
}
}
}
var_dump($tabSum);
This question already has answers here:
Group array data on one column and sum data from another column
(5 answers)
Closed 5 months ago.
How to GROUP BY AND SUM this Array by evaluation_category_id
Array
(
[0] => Array
(
[id] => 60
[evaluation_category_id] => 3
[score] => 15
[itemMaxPoint] => 20
)
[1] => Array
(
[id] => 61
[evaluation_category_id] => 2
[score] => 10
[itemMaxPoint] => 20
)
[2] => Array
(
[id] => 62
[evaluation_category_id] => 1
[score] => 5
[itemMaxPoint] => 20
)
[3] => Array
(
[id] => 63
[evaluation_category_id] => 1
[score] => 50
[itemMaxPoint] => 200
)
[4] => Array
(
[id] => 64
[evaluation_category_id] => 2
[score] => 150
[itemMaxPoint] => 200
)
[5] => Array
(
[id] => 65
[evaluation_category_id] => 3
[score] => 30
[itemMaxPoint] => 50
)
.
.
.
)
So that i get array like this
Array
(
[0] => Array
(
[evaluation_category_id] => 3
[score] => 45
[itemMaxPoint] => 70
)
[1] => Array
(
[evaluation_category_id] => 2
[score] => 160
[itemMaxPoint] => 220
)
[2] => Array
(
[evaluation_category_id] => 1
[score] => 55
[itemMaxPoint] => 220
)
}
i have tried this but its not working .please correct me where i am doing wrong
public function test($data) {
$groups = array();
foreach ($data as $item) {
$key = $item['evaluation_category_id'];
if (!isset($groups[$key])) {
$groups[$key] = array(
'id' => $key,
'score' => $item['score'],
'itemMaxPoint' => $item['itemMaxPoint'],
);
} else {
$groups[$key]['score'] = $groups[$key]['score'] + $item['score'];
$groups[$key]['itemMaxPoint'] = $groups[$key]['itemMaxPoint'] +$item['itemMaxPoint'];
}
}
return $groups;
}
the Output is
Array
(
[2] => Array
(
[id] => 2
[score] => 121 //121 because the given array is different.but its actually SUM all values of score
[itemMaxPoint] => 300
)
)
I have solved it.
public function getDateWiseScore($data) {
$groups = array();
foreach ($data as $item) {
$key = $item['evaluation_category_id'];
if (!array_key_exists($key, $groups)) {
$groups[$key] = array(
'id' => $item['evaluation_category_id'],
'score' => $item['score'],
'itemMaxPoint' => $item['itemMaxPoint'],
);
} else {
$groups[$key]['score'] = $groups[$key]['score'] + $item['score'];
$groups[$key]['itemMaxPoint'] = $groups[$key]['itemMaxPoint'] + $item['itemMaxPoint'];
}
}
return $groups;
}
You could do a Foreach, generate arrays for equal keys, and then array_sum() for each of the arrays values you want to sum :)