Subtracting many arrays - php

I have one big array circa 50 values like this (for example I used smaller array):
Array
(
[Adam Małysz] => 1
[Justyna Kowalczyk] => 2
[Janne Ahonen] => 3
[Stefan Hula] => 4
[Ole Einar Bjoerdalen] => 5
[Jakub Janda] => 6
[Mariusz Pudzianowski] => 7
[Harry Potter] => 8
[Vladimir Zografski] => 9
[Pavel Karelin] => 10
[Eddie Edwards] => 11
[Apoloniusz Tajner] => 12
)
and I have n smaller constant arrays with different values:
Array
(
[Adam Małysz] => 1
[Janne Ahonen] => 2
[Stefan Hula] => 3
[Ole Einar Bjoerdalen] => 4
[Vladimir Zografski] => 5
[Pavel Karelin] => 6
[Apoloniusz Tajner] => 7
[Mariusz Pudzianowski] => 8
[Jakub Janda] => 9
[Harry Potter] => 10
)
Array
(
[Justyna Kowalczyk] => 1
[Apoloniusz Tajner] => 2
[Harry Potter] => 3
[Janne Ahonen] => 4
[Mariusz Pudzianowski] => 5
[Adam Małysz] => 6
[Jakub Janda] => 7
[Ole Einar Bjoerdalen] => 8
[Vladimir Zografski] => 9
[Pavel Karelin] => 10
)
Array
(
[Adam Małysz] => 1
[Janne Ahonen] => 2
[Jakub Janda] => 3
[Stefan Hula] => 4
[Ole Einar Bjoerdalen] => 5
[Justyna Kowalczyk] => 6
[Harry Potter] => 7
[Mariusz Pudzianowski] => 8
[Vladimir Zografski] => 9
[Apoloniusz Tajner] => 10
)
So I want to subtract array values where is the same key like:
Big Array: [Harry Potter] => 8
[Mariusz Pudzianowski] => 7
Small Array #1: [Harry Potter] => 10
[Mariusz Pudzianowski] => 8
Small Array #2: [Harry Potter] => 3
[Mariusz Pudzianowski] => 5
Output: Difference between Small Array #1 and Big Array for key [Harry
Potter] = 2
Difference between Small Array #1 and Big Array for key [Mariusz Pudzianowski] = 1
Output: Difference between Small Array #2
and Big Array for key [Harry Potter] = 5
Difference between Small Array #2 and Big Array for key [Mariusz Pudzianowski] = 2
Basically I'm stuck.
Please help.

lets name your big array as $bigArray and small array as $smallArray both having key-value pair. Now the code should be:
foreach($bigArray as $bigKey=>$bigValue){
foreach($smallArray as $smallKey=>$smallValue){
if($smallKey==$bigKey){
$output=$bigValue-$smallValue;
echo 'Difference between Small Array #1 and Big Array for $bigKey is '.$output;
}
}
}
This way for every element of your big Array , you will loop small array and check if the keys are matching and if they do then you subtract their values and print them. You can do this for other small arrays as well.

You could use array_intersect_key to find all the key that are common to all arrays.
$commonKeys = array_intersect_key($bigArray, $smallArray1, $smallArray2, ...)
From there you just have to iterate over $commonKeys and make the appropriate substractions. Something like :
foreach ($commonKeys as $key) {
$result = abs($bigArray[$key] - $smallArray1[$key]);
echo " Difference between Small Array #1 and Big Array for key [$key] = $result";
//you do that for each Small Array
}

Related

Sort an array of numbers into two arrays of equal length in PHP

I have an array of 20 different numbers which contains ranking of students.
I want to split this array into two sub arrays of equal length i.e 10
I also want the sum of all numbers within each array to be close.
For example, in sub-array A there could be a total sum of 56 and in sub-array B there could be a total sum of 57.
I am using PHP.
I sort the main array here and would like to assign index[0] to sub-array A and index[1] to sub-array B, and keep repeating this until both arrays are filled.
My approach works but i think its not great and not dynamic.
I interate through the main original array for [i] and then add that to the first sub-array, then I set i = i+2 so that I get every second value and store them in the first array.
I then remove the value at index[i] from the main array.
What is left over is now sub-array B.
$kids = array (8,5,6,9,3,8,2,4,6,10,8,5,6,1,7,10,5,3,7,6);
sort($kids);
$arrlength = count($kids);
for($x = 0; $x < $arrlength; $x++) {
echo $kids[$x];
echo "<br>";
}
$teamA = array();
$teamB = array();
$i = 0;
while ($i < $arrlength)
{
#echo $kids[$i] ."<br />";
array_push($teamA, $kids[$i]);
unset($kids[$i]);
$i += 2;
}
$teamB = $kids;
print_r($teamA);
print_r($teamB);
My Output is :
Array ( [0] => 1 [1] => 3 [2] => 4 [3] => 5 [4] => 6 [5] => 6 [6] => 7 [7] =>
8 [8] => 8 [9] => 10 )
The sum here of all values is = 58
Array ( [1] => 2 [3] => 3 [5] => 5 [7] => 5 [9] => 6
[11] => 6 [13] => 7 [15] => 8 [17] => 9 [19] => 10 )
The sum here of all values is = 61
Any help is greatly appreciated. I have no real experience with PHP or its built in functions so sorry if this is a basic question. Thanks!
There is a built-in helper function of php, array_slice. You can read about it in the link I provided.
Here's how you can use in to achieve what you want:
$kids = array (5,7,6,8,3,8,2,4,6,10,8,5,6,10,7,6,5,3,7,6);
sort($kids);
$arrlength = count($kids);
$arrayA= array_slice($kids, 0, $arrlength / 2);
$arrayB= array_slice($kids, $arrlength / 2);
Output
// $arrayA
array:10 [▼
0 => 2
1 => 3
2 => 3
3 => 4
4 => 5
5 => 5
6 => 5
7 => 6
8 => 6
9 => 6
]
// $arrayB
array:10 [▼
0 => 6
1 => 6
2 => 7
3 => 7
4 => 7
5 => 8
6 => 8
7 => 8
8 => 10
9 => 10
]
Another approach for achieving what you asked
$kids = array(8,5,6,9,3,8,2,4,6,10,8,5,6,1,7,10,5,3,7,6);
sort($kids);
$teamA = array();
$teamB = array();
foreach($kids as $i => $kid){
if($i % 2){
array_push($teamA, $kid);
} else{
array_push($teamB, $kid);
}
}
It will generate the same output and sum as you want.

laravel sorting arrays last 5 unique and reverse

I have an array of id's and I want to filter those id's to last 5 and unique ids.
$recently_viewed_ids
array:16 [▼
0 => 1
1 => 2
2 => 1
3 => 2
4 => 8
5 => 7
6 => 6
7 => 6
8 => 6
9 => 5
10 => 8
11 => 4
12 => 1
13 => 1
14 => 1
15 => 1
]
Here is my code and it's messing up because I'm getting 85672
$items = array_slice(array_unique(array_reverse($recently_viewed_ids)), -5);
Output I am expecting
14856
You need to use next combination of array functions:
array_slice( // get first 5 values
array_unique( // get only unique values
array_reverse($arr) //reverse array for get last values
)
,0,5);
Code example here: PHPize.online
In Laravel, I believe you can rewrite the correct answer to:
return collect($arr)
->reverse()
->unique()
->slice(0, 5)
->all();
try this :
$items = array_slice(array_unique(array_reverse($recently_viewed_ids)), 5);

Merging 2 arrays by keys

There are 2 arrays, the reason for 2 arrays is because of 2 drop down from form post.
array1
1 => 878
2 => 983
3 => 717
array2
1 => 10
2 => 15
3 => 12
I have to loop 2 arrays and get where the keys matched and combine them into an array.
878 => 10
983 => 15
717 => 12
Thanks!
using the array_combine. try it here
$combined = array_combine(array_values($array1),array_values($array2));

PHP: display values of array having common column constraint

I have a multidimensional array like this:
Array (
[0] => Array ( [ans_id] => 1 [ans_name] => PHP Hypertext Preprocessor [ques_id] => 1 [right_ans] => Yes [ques_name] => What is the acronym of PHP? [section_id] => 1 [section_name] => PHP )
[1] => Array ( [ans_id] => 2 [ans_name] => Preety Home Page [ques_id] => 1 [right_ans] => No [ques_name] => What is the acronym of PHP? [section_id] => 1 [section_name] => PHP )
[2] => Array ( [ans_id] => 3 [ans_name] => Programmed Hypertext Page [ques_id] => 1 [right_ans] => No [ques_name] => What is the acronym of PHP? [section_id] => 1 [section_name] => PHP )
[3] => Array ( [ans_id] => 4 [ans_name] => Programmed Hypertext Preprocessor [ques_id] => 1 [right_ans] => No [ques_name] => What is the acronym of PHP? [section_id] => 1 [section_name] => PHP ) )
My table is like this:
ans_id ans_name ques_id right_ans
1 PHP Hypertext Preprocessor 1 Yes
2 Preety Home Page 1 No
3 Programmed Hypertext Page 1 No
4 Programmed Hypertext Preprocessor 1 No
5 Andy Suraski 12 No
6 Zeev Gutman 12 No
7 Rasmus Lerdorf 12 Yes
8 Perl 12 No
I want to retrieve the answers in sets of common question id's
i.e. one set of all answers for Ques_id='1', 2nd set of all answers for ques_id='12', etc.
I am supposed to use this code:
foreach($all_ans_cat as $r)
{
echo $r['ans_id']." ".$r['ans_name']."<br>";
}
This is retrieving all the values present in the table as one set and again in the 2nd set the same values are being displayed.
Select all answers sorted by ques_id and generate array with structure needed
$questions = array();
$prevQuestionId = null;
foreach ($all_ans_cat as $r) {
if ($r['ques_id'] !== $prevQuestionId) {
$questions[] = array();
}
$questions[sizeof($questions)-1][] = $r;
$prevQuestionId = $r['ques_id'];
}
Result array would be:
0:
- [ans1, ques1]
- [ans2, ques1]
...
1:
- [ans5, ques13]
- [ans6, ques13]
...
...
you can use a query with group by and aggregate function
have a look at
http://www.w3schools.com/sql/sql_groupby.asp

Sort multidimensional array (PHP) - date complications and counting

I have the following output of an array using PHP. I need to do two things... First, I need to sort the array so it prints by the most recent date. I can't use a simple sort, because the date is outputted in the format mm/dd/yyyy (and not a regular time stamp) ...
Then I need to count how many rows exist for each year.
So, in the example below, I would need to know that there are ...
2 entries from 2010
2 entries from 2011
1 entry from 2012
Stop counting when there are no more rows
Since the year is not separate from the rest of the date digits, this also complicates things...
Array
(
[0] => Array
(
[racer_date] => 11/15/2010
[racer_race] => Test Row 4
[racer_event] => 321
[racer_time] => 16
[racer_place] => 12
[racer_medal] => 1
)
[1] => Array
(
[racer_date] => 7/15/2010
[racer_race] => Test Row 3
[racer_event] => 123
[racer_time] => 14
[racer_place] => 6
[racer_medal] => 0
)
[2] => Array
(
[racer_date] => 7/28/2011
[racer_race] => Test Row
[racer_event] => 123
[racer_time] => 10
[racer_place] => 2
[racer_medal] => 2
)
[3] => Array
(
[racer_date] => 10/9/2011
[racer_race] => Test Row 2
[racer_event] => 321
[racer_time] => 12
[racer_place] => 3
[racer_medal] => 3
)
[4] => Array
(
[racer_date] => 10/3/2012
[racer_race] => World Indoor Championships (final)
[racer_event] => 400m
[racer_time] => 50.79
[racer_place] => 1
[racer_medal] => 1
)
)
function cmp($a, $b)
{
if (strtotime($a["racer_date"]) == strtotime($b["racer_date"])) {
return 0;
}
return (strtotime($a["racer_date"]) < strtotime($b["racer_date"])) ? -1 : 1;
}
usort($array, "cmp");
call your array $array, and above code will sort it..
And to count entities you'll need to run foreach and check date('Y',strtotime($a["racer_date"])) in that foreach which will give you year in 4 digit..

Categories