How to make the unique pair of values in array with php - php

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
)
)

Related

Get All Array Values have more than 100 or more, and get array all values 100 with less

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
)
)

Sync array values across all related array keys

I have a PHP array with the following data
Array
(
[3] => Array
(
[0] => 4095
[2] => 651
)
[4095] => Array
(
[0] => 3
)
[651] => Array
(
[0] => 4432
)
[4432] => Array
(
[0] => 651
)
[92] => Array
(
[0] => 45
)
)
The above array has keys as student_id and the values are also student_id creating a circular relation. What I am trying to achieve is all the student_id has the same set of student_id values. Basically if student_id 3 is related to 4095, 4432 & 651, then, in turn, each of these values has to have 3 among them including other student_id from 3. The below output demonstrates what I am trying to achieve.
Array
(
[3] => Array
(
[0] => 4095
[1] => 4432
[2] => 651
)
[4095] => Array
(
[0] => 3
[1] => 4432
[2] => 651
)
[651] => Array
(
[0] => 3
[1] => 4432
[2] => 4095
)
[4432] => Array
(
[0] => 3
[1] => 4095
[2] => 651
)
[92] => Array
(
[0] => 45
)
[45] => Array
(
[0] => 92
)
)
Explanation of output
The array keys 3, 4095, 651 & 4432 are related to each other either directly or via a common relation (indirect), so will have common set of values (siblings). The key 92 in input array has a value (sibling) of 45, so in a resultant array, a new key 45 will be added to array with the inverse relation as well.
What I have tried so far
I have tried to do it with this code
$syncedSiblings = [];
foreach ($studentsWithSiblings as $sid => $siblings) {
$all = map_assoc(array_merge([$sid], array_keys($siblings)));
foreach ($all as $studentId) {
if (isset($syncedSiblings[$studentId])) {
$old = $syncedSiblings[$studentId];
$syncedSiblings[$studentId] = array_unique(array_merge($old, array_except($all, $studentId)));
} else {
$syncedSiblings[$studentId] = array_unique(array_except($all, $studentId));
}
}
}
Where $studentsWithSiblings has the above array & array_except returns array without the passed values as second argument.
This is the output I am getting right now
Array
(
[3] => Array
(
[0] => 4095
[1] => 651
)
[4095] => Array
(
[0] => 3
[1] => 651
)
[651] => Array
(
[0] => 3
[1] => 4095
[2] => 4432
)
[4432] => Array
(
[0] => 651
)
[92] => Array
(
[0] => 45
)
)
Any help with this will be highly appreciated.
If I've understood you correctly, then this possible to achieve with recursion:
function getChildren($ind_ar, $prev_ar, $data, $rem){
$tmp = [];
$mark = 0;
foreach($ind_ar as $ind){
foreach($data[$ind] as $new_val){
if(!in_array($new_val,$prev_ar) && $new_val != $ind && $new_val != $rem){
$mark = 1;
$tmp[] = $new_val;
}
foreach($data[$new_val] as $new){
if(!in_array($new,$prev_ar) && $new != $ind && $new != $rem){
$mark = 1;
$tmp[] = $new;
}
}
}
}
$res_ar = $prev_ar;
if(!empty($tmp)) $res_ar = array_unique(array_merge($tmp,$prev_ar));
if($mark) $res_ar = getChildren($tmp,$res_ar,$data, $rem);
return $res_ar;
}
You can use this function in this way:
$data = array( 3 => [4095, 651], 4095 => [3], 651 => [4432], 4432 => [3, 651], 92 => [45], 45 => [92], );
foreach($data as $in => &$data_val) {
$data_val = getChildren([$in],$data_val,$data, $in);
sort($data_val);
}
Demo
Output:
Array
(
[3] => Array
(
[0] => 651
[1] => 4095
[2] => 4432
)
[4095] => Array
(
[0] => 3
[1] => 651
[2] => 4432
)
[651] => Array
(
[0] => 3
[1] => 4095
[2] => 4432
)
[4432] => Array
(
[0] => 3
[1] => 651
[2] => 4095
)
[92] => Array
(
[0] => 45
)
[45] => Array
(
[0] => 92
)
)
Two nested loops over the data. If the keys are different, then check if the key of the inner loop is already contained in the data array of the outer key element - if not, add it.
$data = json_decode('{"3":{"0":4095,"2":651},"4095":[3],"651":[4432],"4432":{"1":651}}', true);
foreach($data as $key_outer => $val_outer) {
foreach($data as $key_inner => $val_inner) {
if($key_outer != $key_inner && !in_array($key_inner, $data[$key_outer])) {
$data[$key_outer][] = $key_inner;
}
}
}
var_dump($data);
This gets you
array (size=4)
3 =>
array (size=3)
0 => int 4095
2 => int 651
3 => int 4432
4095 =>
array (size=3)
0 => int 3
1 => int 651
2 => int 4432
651 =>
array (size=3)
0 => int 4432
1 => int 3
2 => int 4095
4432 =>
array (size=3)
1 => int 651
2 => int 3
3 => int 4095
I am assuming a specific order of the elements in those sub-items is not actually required. If it is, then please sort them yourself as desired afterwards or in between (depending on what exactly you need, f.e. sort($data[$key_outer]); after the inner loop would get you the IDs in all sub-arrays sorted ascending.)

How to Sum array value by key for chart?

Sorry, I see multiple similar solutions but i'm unable to implement them in my situations. I tried isset to check if value exist before sum but its giving me true value and not able to add its actual value. with bellow code it's repeating the same value as long as for loop running and giving me wrong sum value. Please correct me.
I want to add array value to single array by matching key..
I have this array...
Array(
[0] => Array
(
[Customer] => Array
(
[created_by] => 3
[amount] => 475
)
)
[1] => Array
(
[Customer] => Array
(
[created_by] => 5
[amount] => 199
)
)
[2] => Array
(
[Customer] => Array
(
[created_by] => 1
[amount] => 199
[refund_amount] => 200
[upgrade_amount] => 199
)
)
[3] => Array
(
[Customer] => Array
(
[created_by] => 1
[amount] => 199
)
)
[4] => Array
(
[Customer] => Array
(
[created_by] => 4
[upgrade_amount] => 199
)
)
)
I'm looking for result like this.....
Array(
[3] => Array
(
[sale] => 475
[refund] => 0
[upgrade] => 0
)
[5] => Array
(
[sale] => 199
[refund] => 0
[upgrade] => 0
)
[1] => Array
(
[sale] => 398
[refund] => 200
[upgrade] => 199
)
[4] => Array
(
[sale] => 0
[refund] => 0
[upgrade] => 199
)
)
I'm using this code to get my result....
foreach($records as $y_key => $y_value){
//print_r($y_value);
if(!isset($top_performer[$y_value['Customer']['created_by']])){
$top_performer[$y_value['Customer']['created_by']]['sale'] = 0;
$top_performer[$y_value['Customer']['created_by']]['refund'] = 0;
$top_performer[$y_value['Customer']['created_by']]['upgrade'] = 0;
}
$top_performer[$y_value['Customer']['created_by']]['sale']+= $y_value['Customer']['amount'];
$top_performer[$y_value['Customer']['created_by']]['refund']+= $y_value['Customer']['refund_amount'];
$top_performer[$y_value['Customer']['created_by']]['upgrade']+=$y_value['Customer']['upgrade_amount'];
}
Your code is looking good, you only need to check if array element exists
foreach($records as $y_key => $y_value){
if(!isset($top_performer[$y_value['Customer']['created_by']])){
$top_performer[$y_value['Customer']['created_by']]['sale'] = 0;
$top_performer[$y_value['Customer']['created_by']]['refund'] = 0;
$top_performer[$y_value['Customer']['created_by']]['upgrade'] = 0;
}
$top_performer[$y_value['Customer']['created_by']]['sale'] += isset($y_value['Customer']['amount']) ? $y_value['Customer']['amount'] : 0;
$top_performer[$y_value['Customer']['created_by']]['refund'] += isset($y_value['Customer']['refund_amount']) ? $y_value['Customer']['refund_amount'] : 0;
$top_performer[$y_value['Customer']['created_by']]['upgrade'] += isset($y_value['Customer']['upgrade_amount']) ? $y_value['Customer']['upgrade_amount'] : 0;
}

Sum 2 arrays with differents keys

I have a problem with my sum of arrays. So I have this array :
Array
(
[0] => Array
(
[totalGames] => 21
[nature] => 542
)
[1] => Array
(
[totalGames] => 2
[nature] => 418
)
[2] => Array
(
[totalGames] => 26
[nature] => 728
)
[3] => Array
(
[totalGames] => 3
[nature] => 542
)
[4] => Array
(
[totalGames] => 2
[nature] => 418
)
)
I want to make a sum of this array to get this result :
Array
(
[0] => Array
(
[totalGames] => 24
[nature] => 542
)
[1] => Array
(
[totalGames] => 4
[nature] => 418
)
[2] => Array
(
[totalGames] => 26
[nature] => 728
)
)
I tried like this :
while ($aRecord = $rResult->fetch_array(MYSQLI_ASSOC))
{
if($aData['nature'] == $aRecord['nature']){
$aData[] = $aRecord;
}else{
$aData['nature'] = $aRecord['nature'];
$aData['totalGames'] += $aRecord['nature']['totalGames'];
}
}
But not work. What I'm doing wrong ? Can you help me please ? Thx in advance and sorry for my english.
I also tried but it did not work :
SELECT COUNT(*) as totalGames, nature FROM master WHERE date(date)="2015-11-20" GROUP BY nature ORDER by totalGames
Your code should be:
$myArr = array(array('totalGames' => 21, 'nature' => 542),array('totalGames' => 2,'nature' => 418),array('totalGames' => 26,'nature' => 728), array('totalGames' => 3,'nature' => 542),array('totalGames' => 2,'nature' => 418));
$sum = array_reduce($myArr, function ($a, $b) {
isset($a[$b['nature']]) ? $a[$b['nature']]['totalGames'] += $b['totalGames'] : $a[$b['nature']] = $b;
return $a;
});
print_r($sum);
Try as
while ($aRecord = $rResult->fetch_array(MYSQLI_ASSOC)){
$hash = $aRecord['nature'];
if(isset($aData[$hash])){
$aData[$hash]['totalGames'] += $aRecord['totalGames'];
}else{
$aData[$hash]['nature'] = $aRecord['nature'];
$aData[$hash]['totalGames'] = $aRecord['totalGames'];
}
}

From multidimensional array i want to extract single column

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)

Categories