How to group of 2 arrays - php

I have 2 arrays like this
$head = array(7, 1, 1, 1, 1, 14, 14, 14, 9, 9, 9, 13, 13, 13, 3, 3, 5, 8, 8, 8, 2, 2); //count =22
$customer = array(1, 7, 9, 13, 14, 1, 9, 13, 1, 13, 14, 1, 9, 14, 2, 8, 8, 2, 3, 5, 3, 8); //count=22
And I want to group this 2 arrays by consider at $customer, if $customer[0]=1 in $customer[1-21] and $head[1-21] will not have a value 1, such as in the $head[1] have a value 1, So delete at $head[1] and $customer[1]. And then consider at $customer[6]. The value is 9. It means in $head[7-21] and $customer[7-21] will not have a value 9.
I am trying to write a code for this concept like this. Here is my code
for ($i = 0; $i < count($head); $i++) {
for ($j = $i + 1; $j < count($customer); $j++) {
if ($customer[$i] == $head[$j]) {
unset($head[$j]);
unset($customer[$j]);
}
if ($customer[$i] == $customer[$j]) {
unset($head[$j]);
unset($customer[$j]);
}
}
}
print_r($head);
print_r($customer);
the result is of $head and $customer is:
Array ( [0] => 7 [6] => 14 [7] => 14 [13] => 13 [14] => 3 [15] => 3 [16] => 5 [17] => 8 [18] => 8 [19] => 8 [20] => 2 [21] => 2 )
Array ( [0] => 1 [6] => 9 [7] => 13 [13] => 14 [14] => 2 [15] => 8 [16] => 8 [17] => 2 [18] => 3 [19] => 5 [20] => 3 [21] => 8 )
I found that it's wrong. Because the real result should be:
Array ( [0] => 7 [6] => 14 [7] => 14 [14] => 3 [15] => 3 )
Array ( [0] => 1 [6] => 9 [7] => 13 [14] => 2 [15] => 8 )
Please help me to fix this problem.

Your logic is all ok but when you unset a particular index then all other index after when you iterate it again then i index is missing. Just open the error and warning then you will see
Notice: Undefined offset
I have just replaced your uset to assign it to ''. So you can understand it
<?php
$head = array(7, 1, 1, 1, 1, 14, 14, 14, 9, 9, 9, 13, 13, 13, 3, 3, 5, 8, 8, 8, 2, 2); //count =22
$customer = array(1, 7, 9, 13, 14, 1, 9, 13, 1, 13, 14, 1, 9, 14, 2, 8, 8, 2, 3, 5, 3, 8); //count=22
for ($i = 0; $i < count($head); $i++) {
for ($j = $i + 1; $j < count($customer); $j++) {
if ($customer[$i] == $head[$j]) {
$head[$j] = '';
$customer[$j] = '';
}
if ($customer[$i] == $customer[$j]) {
$head[$j] = '';
$customer[$j] = '';
}
}
}
print_r(array_diff($head, [''])); // remove all the '' entries
print_r(array_diff($customer, [''])); // remove all the '' entries

The problem is that count() only returns a count of the set elements in the array. So if you unset them it will be reduced and you won't arrive at the end of the array. To fix, calculate the count at the start and store in a variable:
$headcount = count($head);
$customercount = count($customer);
for ($i = 0; $i < $headcount; $i++) {
for ($j = $i + 1; $j < $customercount; $j++) {
if ($customer[$i] == $head[$j]) {
unset($head[$j]);
unset($customer[$j]);
}
if ($customer[$i] == $customer[$j]) {
unset($head[$j]);
unset($customer[$j]);
}
}
}

Related

PHP: Most frequent value in array

So I have this JSON Array:
[0] => 238
[1] => 7
[2] => 86
[3] => 79
[4] => 55
[5] => 92
[6] => 55
[7] => 7
[8] => 254
[9] => 9
[10] => 75
[11] => 238
[12] => 89
[13] => 238
I will be having more values in the actual JSON file. But by looking at this I can see that 238 and 55 is being repeated more than any other number. What I want to do is get the top 5 most repeated values in the array and store them in a new PHP array.
$values = array_count_values($array);
arsort($values);
$popular = array_slice(array_keys($values), 0, 5, true);
array_count_values() gets the count of the number of times each item appears in an array
arsort() sorts the array by number of occurrences in reverse order
array_keys() gets the actual value which is the array key in the results from array_count_values()
array_slice() gives us the first five elements of the results
Demo
$array = [1,2,3,4,238, 7, 86, 79, 55, 92, 55, 7, 254, 9, 75, 238, 89, 238];
$values = array_count_values($array);
arsort($values);
$popular = array_slice(array_keys($values), 0, 5, true);
array (
0 => 238,
1 => 55,
2 => 7,
3 => 4,
4 => 3,
)
The key is to use something like array_count_values() to tally up the number of occurrences of each value.
<?php
$array = [238, 7, 86, 79, 55, 92, 55, 7, 254, 9, 75, 238, 89, 238];
// Get array of (value => count) pairs, sorted by descending count
$counts = array_count_values($array);
arsort($counts);
// array(238 => 3, 55 => 2, 7 => 2, 75 => 1, 89 => 1, 9 => 1, ...)
// An array with the first (top) 5 counts
$top_with_count = array_slice($counts, 0, 5, true);
// array(238 => 3, 55 => 2, 7 => 2, 75 => 1, 89 => 1)
// An array with just the values
$top = array_keys($top_with_count);
// array(238, 55, 7, 75, 89)
?>

PHP - Set first array's values to second array's iterations

I'm trying to make one array set to the iterations of another array. I'm working on a hash algorithm that takes in a user value of the order they want the array. It takes their code and breaks it down into 40 blocks of binary to be converted into hexadecimal. So far I'm able to change the iteration order, but it only takes the last value of the first array and sets as the value for each iteration of the second array.
The first array looks like this (Showing only 10 of the 40 to save space):
Array
(
[0] => 0111
[1] => 1000
[2] => 0110
[3] => 0010
[4] => 0011
[5] => 0001
[6] => 0011
[7] => 0010
[8] => 0011
[9] => 0101
)
The second one is like this:
Array
(
[3] => 0101
[2] => 0101
[1] => 0101
[6] => 0101
[5] => 0101
[4] => 0101
[9] => 0101
[8] => 0101
[7] => 0101
[0] => 0101
)
And here is the PHP code:
$arrayDump = $test->binarySplit($name);
$ordered = array();
$orderKey = array(3, 2, 1, 6, 5, 4, 9, 8, 7, 12, 11, 10, 15, 14, 13, 18, 17, 16, 21, 20, 19, 24, 23, 22, 27, 26, 25, 30, 29, 28, 33, 32, 31, 36, 35, 34, 39, 38, 37, 0);
foreach ($orderKey as $key) {
for ($i = $key; $i < count($arrayDump); $i++) {
$ordered[$key] = $arrayDump[$i];
}
}
The class call above isn't too important for this problem that I can tell. The $arrayDump is the first array; $ordered is the second. As you can tell, the second array changes the iteration to be what I want, but it only contains the last value from the first array. I threw it through a loop to try and get each value, but I'm at a loss. Any help would be appreciated.
You don't need the second loop, try this:
foreach ($orderKey as $key => $value) {
$ordered[$key] = $arrayDump[$value];
}

Create an array from range elements with a base array

I have an array like this
$baseArray = array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 );
So i want to make two arrays of range
$arrayOne = from index 5 - 10
$arrayTwo = from index 15 - 20
How can I split the $baseArray to make arrays like that in php ?
There is function array_slice
$baseArray = array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 );
$arrayOne = array_slice($baseArray, 5, 10);
$arrayTwo = array_slice($baseArray, 15, 20);
This should work for you:
(Only works if you have an index with numbers start's with 0)
$baseArray = array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 );
$arrayOne = array();
$arrayTwo = array();
foreach($baseArray as $k => $v) {
if($k >= 5 && $k <= 10)
$arrayOne[] = $v;
elseif($k >= 15 && $k <= 20)
$arrayTwo[] = $v;
}
print_r($arrayOne);
print_r($arrayTwo);
Output:
arrayOne:
Array
(
[0] => 6
[1] => 7
[2] => 8
[3] => 9
[4] => 10
[5] => 11
)
arrayTwo:
Array
(
[0] => 16
[1] => 17
[2] => 18
[3] => 19
[4] => 20
)
Can try using for() & array_slice(). Example:
$baseArray = array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 );
$newAr = array();
for($i = 5; $i < count($baseArray); $i += 10){
$newAr[] = array_slice($baseArray, $i, 5);
}
print '<pre>';
print_r($newAr);
print '</pre>';

Calculate overall and division ranking via arrays

This is a follow-up to Detect future duplicate values while iterating through MySQL results in PHP.
I have an SQL query which produces the results:
team_id division_id wins
-------------------------------
10 2 44
9 2 42
5 1 42
2 1 42
3 1 41
11 2 40
1 1 36
8 2 31
7 2 29
12 2 24
4 1 20
6 1 18
I'm trying to calculate a given team's overall and divisional rankings.
For example, team_id = 1:
Overall: 7
Divisional: 4
For team_id = 3:
Overall: 5
Divisional: 3
For team_id = 9:
Overall: T-2 //must indicate tie
Divisional: 2
As you can see from the linked previous question/answer, I can calculate the Overall rank just fine. The issue comes with calculating the divisional rank as well (including properly handling ties).
I've tried storing the results in a multi-dimensional array like $arr['wins']['division_id']['team_id'], such as...
44 => 2 => 10
42 => 1 => 5
2
2 => 9
41 => 1 => 3
40 => 11 => 2
...
But am stuck as to how to iterate through and get my two respective ranks, as well as detecting ties appropriately for each.
Try something like this:
// Dataset as defined on question
$arr = array(
array('team_id' => 10, 'division_id' => 2, 'wins' => 44),
array('team_id' => 9, 'division_id' => 2, 'wins' => 42),
array('team_id' => 5, 'division_id' => 1, 'wins' => 42),
array('team_id' => 2, 'division_id' => 1, 'wins' => 42),
array('team_id' => 3, 'division_id' => 1, 'wins' => 41),
array('team_id' => 11, 'division_id' => 2, 'wins' => 40),
array('team_id' => 1, 'division_id' => 1, 'wins' => 36),
array('team_id' => 8, 'division_id' => 2, 'wins' => 31),
array('team_id' => 7, 'division_id' => 2, 'wins' => 29),
array('team_id' => 12, 'division_id' => 2, 'wins' => 24),
array('team_id' => 4, 'division_id' => 1, 'wins' => 20),
array('team_id' => 6, 'division_id' => 1, 'wins' => 18));
$divisionTeam = array();
$divisionWins = array();
foreach($arr as $team) {
$divisionTeam[$team['division_id']][] = $team['team_id'];
$divisionWins[$team['division_id']][] = $team['wins'];
}
echo "<pre>";
foreach (array_keys($divisionTeam) as $division_id) {
$rank = 0;
$prevWins = -1;
echo "DIVISION $division_id \n";
foreach ($divisionTeam[$division_id] as $index => $team_id) {
if ($prevWins == $divisionWins[$division_id][$index]) {
echo $team_id . " T - " . $rank . "\n";
}
else {
$rank++;
echo $team_id . " " . $rank . "\n";
}
$prevWins = $divisionWins[$division_id][$index];
}
}
echo "</pre>";

php importance of a number in a set of numbers

I have a set of numbers e.g.
$input = array(1, 4, 7, 4, 9, 4, 8, 6, 2, 8, 7, 7, 4, 5, 3);
I am trying to work out the importance of each number based on the following rule:
As the sequence gets longer the numbers get less significant, and each time a number is mentioned then it will improve the relevance (how much depends on its position in the
sequence).
I am expecting something like:
Array(
'4' => 90%
'1' => 75%
'7' => 60%
....
)
So 4 is the most inportant, followed by 1 and then 7 etc. Note that the output is completely fabricated but gives in indication that 4 should be the most important. I believe I want some kind of linear solution.
Is this more of what you were thinking? Answer based on stillstanding
$numbers = array(1, 4, 7, 4, 9, 4, 8, 6, 2, 8, 7, 7, 4, 5, 3);
$weight = array();
$count = count($numbers);
for ($i=0; $i<$count; $i++) {
if (!isset($weight[$numbers[$i]])) $weight[$numbers[$i]] = 1;
$weight[$numbers[$i]] += $count + pow($count - $i, 2);
}
$max = array_sum($weight);
foreach ($weight as &$w) {
$w = ($w / $max) * 100;
}
arsort($weight);
result:
Array
(
[4] => 34.5997286296
[7] => 17.3677069199
[1] => 16.3500678426
[8] => 10.0407055631
[9] => 9.29443690638
[6] => 5.42740841248
[2] => 4.40976933514
[5] => 1.35685210312
[3] => 1.15332428765
)
$numbers=array(1, 4, 7, 4, 9, 4, 8, 6, 2, 8, 7, 7, 4, 5, 3);
$weight=array();
$count=count($numbers);
for ($i=0; $i<$count; $i++) {
if (!isset($weight[$numbers[$i]]))
$weight[$numbers[$i]]=1;
$weight[$numbers[$i]]*=$count-$i;
}
var_dump($weight);
Result:
Array
(
[1] => 15
[4] => 5040
[7] => 260
[9] => 11
[8] => 54
[6] => 8
[2] => 7
[5] => 2
[3] => 1
)
This algorithm is fairly simplistic, but I think it accomplishes what you're looking for.
Given that you have the sequence you described above and it is stored in an array called $sequence
$a = array();
for($i=0;$i<count($sequence);$i++)
{
//calculate the relevance = 1/position in array
$relevance = 1/($i+1);
//add $relevance to the value of $a[$sequence[$i]]
if(array_key_exists((string)$sequence[$i],$a))
$a[(string)$sequence[$i]] += $relevance;
else
$a[(string)$sequence[$i]] = $relevance;
}
return $a;

Categories