Php, printing a 3d array into a table - php

Basically i need to sort some data into the top 5 best sales.
I've managed to group the 3 arrays i've used using.
$c = array_combine($PriceArray,$namesArray,$ProductArray);
krsort($c);
Price array being the total. (Bit of a silly name, i just realised)
namesarray is the array of names
and product array is the list of product codes
I need to print it in a table like
"|£3.45|Jelly Beans | 120|"
so they have their own column, but at the moment it's printing it like
| 3.45| array | array|
and i use
echo '<td>'.$ProductArray[$i].'</td>'.'<td>'.$year.'</td>'.'<td>'.array_keys($c,$c[$i]).'<td>'. $PriceArray[$i].'</td>';
to print it.
Thanks in advance
Array
(
[0] => 77358.47
[1] => 111004.98
[2] => 227194.9
[3] => 84645.75
[4] => 29693.58
[5] => 198867.2
[6] => 110779.5
[7] => 210530.62
[8] => 102916.79
[9] => 186630.75
[10] => 140143.24
[11] => 48984.48
[12] => 74759.34
[13] => 204793.14
[14] => 82192.5
[15] => 167402.7
[16] => 58232.72
[17] => 216302.32
[18] => 26353.92
[19] => 149993.1
)
Array
(
[0] => Jelly beans
[1] => Banana milkshake powder
[2] => Edam Cheese
[3] => Hairnet
[4] => Aubergine jam
[5] => White bread
[6] => Brown bread
[7] => Purple bread
[8] => Plain flour
[9] => Striped flour
[10] => Soft tissues
[11] => Anti personnel mines
[12] => Chicken fillets
[13] => Beef cubes
[14] => Marshmallows
[15] => Fizzy carrot juice
[16] => Low fat lard
[17] => Suet dumpling mix
[18] => Gravy powder
[19] => Cherry pie filling
)
Array
(
[0] => 121
[1] => 122
[2] => 123
[3] => 124
[4] => 125
[5] => 126
[6] => 127
[7] => 128
[8] => 129
[9] => 130
[10] => 131
[11] => 132
[12] => 133
[13] => 134
[14] => 135
[15] => 136
[16] => 137
[17] => 138
[18] => 139
[19] => 140
)
Product Code Year Name Sales Total

Zip function blatantly stolen from: zip function from SO
function zip() {
$args = func_get_args();
$zipped = array();
$n = count($args);
for ($i=0; $i<$n; ++$i) {
reset($args[$i]);
}
while ($n) {
$tmp = array();
for ($i=0; $i<$n; ++$i) {
if (key($args[$i]) === null) {
break 2;
}
$tmp[] = current($args[$i]);
next($args[$i]);
}
$zipped[] = $tmp;
}
return $zipped;
}
function cmp($a, $b)
{
if($a[0] == $b[0]){
return 0;
}
return ($a[0] < $b[0]) ? -1 : 1;
}
$PriceArray = array( 4.56, 1.23, 7.89 );
$namesArray = array( 'ab', 'cd', 'ef' );
$ProductArray = array( '11', '22', '33' );
$c = zip($PriceArray, $namesArray, $ProductArray);
usort($c, 'cmp');
foreach($c as $prices)
{
//$prices[0] == price
//$prices[1] == name
//$prices[2] == code
echo "{$prices[0]}|{$prices[1]}|{$prices[2]}\n";
prints:
1.23|cd|22
4.56|ab|11
7.89|ef|33
I am using the zip-function that is available natively in Python to combine N-arrays and "zip" them together.
So take index 0 of all given arrays, and make that a new array entry. Do that for all indices available.
The cmp function takes two variables, in this case two arrays, where index-0 = price, 1 = name and 2 = code. You obviously want to sort by ascending by price thus we are comparing the price index. This results in a new/sorted array based on price.
You can also substitude the usort($c, 'cmp'); call with the following:
usort($c, function($a, $b){
if($a[0] == $b[0]){
return 0;
}
return ($a[0] < $b[0]) ? -1 : 1;
});
However, this is only available in PHP version >= 5.3

Just put the keys and values into new arrays.
$NewNamesArray = array_values($c); $NewPriceArray = array_keys($c)

Related

Percent limit with remainder distribution on PHP

There is a list of percentages. The sum of the numbers is 100.
$percent = [20.88, 14.93, 14.14, 13.29, 5.06, 4.43, 4.24, 4.22, 2.57, 2.51, 2.38, 2.18, 1.94, 1.80, 1.34, 1.21, 0.81, 0.63, 0.50, 0.48, 0.30, 0.16];
I need to limit the maximum number to a specific value, and distribute the remainder over the rest of the numbers. If possible, keeping the proportions of the remaining percentages.
Tried the following solution:
$limit = 8;
// Calculating the remainder of numbers exceeding $limit.
$rest = array_reduce($percent, function ($a, $b) use ($limit) {
return $limit < $b ? $a += ($b - $limit) : $a;
});
// Number of numbers not exceeding $limit.
$small = array_reduce($percent, function ($a, $b) use ($limit) {
return $limit > $b ? ++$a : $a;
});
// Percent calc with limit up to $limit and addition of the remainder ($rest / $small).
array_walk($percent, function(&$value) use ($limit, $rest, $small) {
$value = $limit < $value ? min($value, $limit) : $value + ($rest / $small);
});
print_r($percent);
print(array_sum($percent)) . "\n";
This code works correctly:
Array
(
[0] => 8
[1] => 8
[2] => 8
[3] => 8
[4] => 6.7955555555556
[5] => 6.1655555555556
[6] => 5.9755555555556
[7] => 5.9555555555556
[8] => 4.3055555555556
[9] => 4.2455555555556
[10] => 4.1155555555556
[11] => 3.9155555555556
[12] => 3.6755555555556
[13] => 3.5355555555556
[14] => 3.0755555555556
[15] => 2.9455555555556
[16] => 2.5455555555556
[17] => 2.3655555555556
[18] => 2.2355555555556
[19] => 2.2155555555556
[20] => 2.0355555555556
[21] => 1.8955555555556
)
100
But if I change the variable $limit to the value 6, the limit is exceeded due to the addition of the remainder:
Array
(
[0] => 6
[1] => 6
[2] => 6
[3] => 6
[4] => 7.24
[5] => 6.61
[6] => 6.42
[7] => 6.4
[8] => 4.75
[9] => 4.69
[10] => 4.56
[11] => 4.36
[12] => 4.12
[13] => 3.98
[14] => 3.52
[15] => 3.39
[16] => 2.99
[17] => 2.81
[18] => 2.68
[19] => 2.66
[20] => 2.48
[21] => 2.34
)
100
I can't find an algorithm.
You can try an iterative approch.
limit the maximum value
while the sum is not 100:
compute the missing value for each item under the limit
add this value to item under the limit, without exceeding this limit
Code: (demo)
$percent = [20.88, 14.93, 14.14, 13.29, 5.06, 4.43, 4.24, 4.22, 2.57, 2.51, 2.38, 2.18, 1.94, 1.80, 1.34, 1.21, 0.81, 0.63, 0.50, 0.48, 0.30, 0.16];
$limit = 6;
// check if a solution is possible
if ($limit * count($percent) < 100) die("Not possible");
// 1. limit the maximum value
$limited = array_map(fn($value) => min($value, $limit), $percent);
// 2. while the sum is not 100
while (($remain = 100 - array_sum($limited)) > 0)
{
// get the "non-full" values
$filtered = array_filter($limited, fn($v) => $v < $limit);
// get the value to add for each items
$evenly = $remain / count($filtered);
// add the value (maximum as possible)
$limited = array_map(fn($value) => min($value + $evenly, $limit), $limited);
}
print_r($limited);
print_r(array_sum($limited));
Output:
Array
(
[0] => 6
[1] => 6
[2] => 6
[3] => 6
[4] => 6
[5] => 6
[6] => 6
[7] => 6
[8] => 4.9407142857143
[9] => 4.8807142857143
[10] => 4.7507142857143
[11] => 4.5507142857143
[12] => 4.3107142857143
[13] => 4.1707142857143
[14] => 3.7107142857143
[15] => 3.5807142857143
[16] => 3.1807142857143
[17] => 3.0007142857143
[18] => 2.8707142857143
[19] => 2.8507142857143
[20] => 2.6707142857143
[21] => 2.5307142857143
)
100
See this demo for PHP version prior to 7.4.

Swiss Tournament. Team confrontation multiple rounds without repeat and ordered

I try to make a tournament based on Swiss method.
I have for example 12 teams, and in each round, every team has points (0 to 100) and wins, loses or draws.
I want to find which teams play against each other, with these conditions:
Ordered by wins, draws, and points.
Not to be played previously.
In each round, I got foreach team the possible teams to play against in an array like this: (The key indicates the team id, and the values indicate ll possible teams to play separated by ",")
[2] => 4,11,6,10,3,8,7,12,
[5] => 4,11,9,10,3,8,1,12,
[4] => 2,5,6,10,8,7,12,
[11] => 5,9,10,3,8,7,
[9] => 5,11,6,3,8,7,12,
[6] => 2,4,9,10,3,7,12,
[10] => 2,5,4,11,6,8,7,12,
[3] => 5,11,9,6,8,7,
[8] => 2,5,4,11,9,10,3,1,12,
[7] => 2,4,11,9,6,10,3,1,12,
[1] => 5,4,11,9,6,3,8,7,
[12] => 2,5,4,9,6,10,8,7,
First I have all teams played before in a array: (key indicates team id)
Array (
[1] => 2,10,12,
[2] => 1,9,5,
[3] => 4,12,10,
[4] => 3,11,9,
[5] => 6,7,2,
[6] => 5,8,11,
[7] => 8,5,8,
[8] => 7,6,7,
[9] => 10,2,4,
[10] => 9,1,3,
[11] => 12,4,6,
[12] => 11,3,1, )
Then, I get all teams ordered by wins, loses and points in a array: (Keys indicates also team id)
Array
(
[1] => 2
[2] => 5
[3] => 4
[4] => 11
[5] => 9
[6] => 6
[7] => 10
[8] => 3
[9] => 8
[10] => 7
[11] => 1
[12] => 12
)
Finaly I try to find the possible match against two teams.
$checks = array();
$pairs = array();
for ($i = 1; $i <= count($list); $i++) {
for ($j = 1; $j <= count($list); $j++) {
if(strpos($plays[$list[$i]], $list[$j]) !== false || $list[$i] == $list[$j] ) {
}else{
if(!in_array($list[$i],$checks) && !in_array($list[$j],$checks)){
$pairs[] = $list[$i].",".$list[$j];
$checks[] = $list[$i];
$checks[] = $list[$j];
}
}
}
}
And finaly I print the array "$pairs". It shows:
Array
(
[0] => 2,4
[1] => 5,11
[2] => 9,6
[3] => 10,8
[4] => 3,7
)
Thats not correct because the team_id 1 and team_id 12 can not play in this round because the played before:
I don't know how to solve this.
Thanks again!

php multidimensional array path segment combination loop

I am trying to figure a way to get this to work. But I have a hard time thinking out the logics.
I have this array:
Array
(
[0] => Array
(
[0] => news
[1] => {section}
[2] => {slug}
[3] => {*}
)
[1] => Array
(
[0] => {id}
[1] => {*}
)
[2] => Array
(
[0] => {date}
[1] => 25-07-1982
[2] => {section}
[3] => {slug}
[4] => {*}
)
)
That I need to convert to this result:
0 news/{id}/{date}
1 news/{id}/25-07-1982
2 news/{id}/{section}
3 news/{id}/{slug}
4 news/{id}/{*}
5 news/{*}/{date}
6 news/{*}/25-07-1982
7 news/{*}/{section}
8 news/{*}/{slug}
9 news/{*}/{*}
10 {section}/{id}/{date}
11 {section}/{id}/25-07-1982
12 {section}/{id}/{section}
13 {section}/{id}/{slug}
14 {section}/{id}/{*}
15 {section}/{*}/{date}
16 {section}/{*}/25-07-1982
17 {section}/{*}/{section}
18 {section}/{*}/{slug}
19 {section}/{*}/{*}
20 {slug}/{id}/{date}
21 {slug}/{id}/25-07-1982
22 {slug}/{id}/{section}
23 {slug}/{id}/{slug}
24 {slug}/{id}/{*}
25 {slug}/{*}/{date}
26 {slug}/{*}/25-07-1982
27 {slug}/{*}/{section}
28 {slug}/{*}/{slug}
29 {slug}/{*}/{*}
30 {*}/{id}/{date}
31 {*}/{id}/25-07-1982
32 {*}/{id}/{section}
33 {*}/{id}/{slug}
34 {*}/{id}/{*}
35 {*}/{*}/{date}
36 {*}/{*}/25-07-1982
37 {*}/{*}/{section}
38 {*}/{*}/{slug}
39 {*}/{*}/{*}
The input array could contain more than three keys, so the solution I'm looking for should be dynamic. And the result should have the same order as the result shown above.
Does someone know how to do this in a efficient way? Can someone give me a push in the right direction? Thanks a lot! :)
Sth like this
foreach ($array[0] as $val0 )
foreach ($array[1] as $val1 )
foreach ($array[2] as $val2 )
$newArray[] = "$val0/$val1/$val2";
EDIT: for variable array length
function recursive($array , $length = 0){
$retval =array();
if($length < count($array) -1){
foreach ($array[$length] as $val0 )
foreach (recursive($array, $length+1) as $val1)
$retval[] = "$val0/$val1";
}
else
{
foreach ($array[$length] as $val0 )
$retval[] = "$val0";
}
return $retval;
}
print_r(recursive($array));
Just because I like writing functions that mis/manage PHP arrays, I put this together, mainly because I was pretty sure you could avoid recursion — because the structure itself isn't recursive. (My head seems to think that is a rule, I'm sure someone somewhere can prove it wrong).
foreach ( array_reverse($array) as $sub ) {
if ( isset($rem) ) {
$ret = array();
foreach ( $sub as $itm ) {
foreach ( $rem as $val ) { $ret[] = "$itm/$val"; }
}
$rem = $ret;
}
else {
$rem = $sub;
}
}
The output found in $rem is as follows:
Array (
[0] => news/{id}/{date}
[1] => news/{id}/25-07-1982
[2] => news/{id}/{section}
[3] => news/{id}/{slug}
[4] => news/{id}/{*}
[5] => news/{*}/{date}
[6] => news/{*}/25-07-1982
[7] => news/{*}/{section}
[8] => news/{*}/{slug}
[9] => news/{*}/{*}
[10] => {section}/{id}/{date}
[11] => {section}/{id}/25-07-1982
[12] => {section}/{id}/{section}
[13] => {section}/{id}/{slug}
[14] => {section}/{id}/{*}
[15] => {section}/{*}/{date}
[16] => {section}/{*}/25-07-1982
[17] => {section}/{*}/{section}
[18] => {section}/{*}/{slug}
[19] => {section}/{*}/{*}
[20] => {slug}/{id}/{date}
[21] => {slug}/{id}/25-07-1982
[22] => {slug}/{id}/{section}
[23] => {slug}/{id}/{slug}
[24] => {slug}/{id}/{*}
[25] => {slug}/{*}/{date}
[26] => {slug}/{*}/25-07-1982
[27] => {slug}/{*}/{section}
[28] => {slug}/{*}/{slug}
[29] => {slug}/{*}/{*}
[30] => {*}/{id}/{date}
[31] => {*}/{id}/25-07-1982
[32] => {*}/{id}/{section}
[33] => {*}/{id}/{slug}
[34] => {*}/{id}/{*}
[35] => {*}/{*}/{date}
[36] => {*}/{*}/25-07-1982
[37] => {*}/{*}/{section}
[38] => {*}/{*}/{slug}
[39] => {*}/{*}/{*}
)
Also, for those that like their arrays multidimensional, this might come in handy (although I'd hate to think what the overheads are for such a code golfed version). Just to be clear, this second example doesn't create the string list as requested by the OP, but a hierarchical array structure instead.
foreach ( array_reverse($array) as $sub ) {
$rem = isset($rem)
? array_combine($sub, array_fill(0, count($sub), $rem))
: $sub
;
}
This generates (again in $rem):
Array (
[news] => Array (
[{id}] => Array (
[0] => {date}
[1] => 25-07-1982
[2] => {section}
[3] => {slug}
[4] => {*}
)
[{*}] => Array (
[0] => {date}
[1] => 25-07-1982
[2] => {section}
[3] => {slug}
[4] => {*}
)
)
[{section}] => Array (
[{id}] => Array (
[0] => {date}
[1] => 25-07-1982
[2] => {section}
[3] => {slug}
[4] => {*}
)
... and so on
Now if only PHP had a join_recursive that included keys.
(it would be almost pointless, save for helping with the above).

Re-order PHP array by middle key as start (Circular Sorting)

very basic question however I have had some trouble finding the answers on PHP.NET.
I have the following array:
Array (
[1] => Array
(
[1] => 4
[2] => 1
[3] => 5
[4] => 3
)
[2] => Array
(
[5] => 2
[6] => 8
[7] => 7
[8] => 6
)
[3] => Array
(
[9] => 10
[10] => 9
[11] => 12
[12] => 11
)
[4] => Array
(
[13] => 15
[14] => 16
[15] => 14
[16] => 13
)
)
I want the array to be re-ordered so that the key number 3 in the first series of the array becomes the first, then the rest to be re-ordered from there to eventually get the result of:
Array (
[3] => Array
(
[9] => 10
[10] => 9
[11] => 12
[12] => 11
)
[4] => Array
(
[13] => 15
[14] => 16
[15] => 14
[16] => 13
)
[1] => Array
(
[1] => 4
[2] => 1
[3] => 5
[4] => 3
)
[2] => Array
(
[5] => 2
[6] => 8
[7] => 7
[8] => 6
)
)
I am looking for a way to do this so I can define the array, then the first level key I need to sort by, and then it will return the array in this way.
The standard PHP keys didn't seem to offer something like this, so it would be good to be able to have a separate function such as $newArray = reorder_array($array, $key);
I don't require any sorting of the second level, only the initial 4 main / first level array sections.
You help is greatly appreciated as I have been sitting on this one for awhile without a clear and simple solution.
You re-ordering can be simply implemented with one foreach loop, like:
function reorderArray($array, $key)
{
$found = false;
foreach($array as $k=>$v)
{
$found = $found || $k===$key;
if(!$found)
{
unset($array[$k]);
$array[$k] = $v;
}
//else break can be added for performance issues
}
return $array;
}
with usage
$array=[1=>'foo', 4=>'bar', 9=>'baz', 'test'=>51];
var_dump(reorderArray($array, 9));
var_dump(reorderArray($array, 'test'));
var_dump(reorderArray($array, 'no_such_key'));//original array in result
-check this demo. If keys are consecutive numerics, however, this can be easily implemented with array_slice() calls.

Replace value in multi dimension array

I have array format like:
Array
(
[Australia] => Array
(
[0] => [1990,0.01],
[1] => [1991,0.02],
[2] => [1992,0.02],
[3] => [1993,0.02],
[4] => [1994,0.02],
[5] => [1995,0.02],
[6] => [1996,0.02],
[7] => [1997,0.02],
[8] => [1998,0.02],
[9] => [1999,0.02],
[10] => [2000,0.02],
[11] => [2001,0.02],
[12] => [2002,0.02],
[13] => [2003,0.02],
[14] => [2004,0.02],
[15] => [2005,0.02],
[16] => [2006,0.02],
[17] => [2007,0.02],
[18] => [2008,0.02],
[19] => [2009,empty],
[20] => [2010,empty],
[21] => [2011,empty],
[22] => [2012,empty],
[23] => [2013,empty],
[24] => [2014,empty],
[25] => [2015,empty]
)
[Pakistan] => Array
(
[0] => [1990,0.00],
[1] => [1991,0.00],
[2] => [1992,0.00],
[3] => [1993,0.00],
[4] => [1994,0.00],
[5] => [1995,0.00],
[6] => [1996,0.00],
[7] => [1997,0.00],
[8] => [1998,0.00],
[9] => [1999,0.00],
[10] => [2000,0.00],
[11] => [2001,0.00],
[12] => [2002,0.00],
[13] => [2003,0.00],
[14] => [2004,0.01],
[15] => [2005,0.01],
[16] => [2006,0.00],
[17] => [2007,0.00],
[18] => [2008,0.00],
[19] => [2009,empty],
[20] => [2010,empty],
[21] => [2011,empty],
[22] => [2012,empty],
[23] => [2013,empty],
[24] => [2014,empty],
[25] => [2015,empty]
)
)
and i want to replace 'empty' with 0 without change the array structure and elements position. I stuck how to do..
You can use array_walk_recursive function:
function replace_empty(&$item, $key) {
$item = str_replace('empty', '0', $item);
}
array_walk_recursive($your_array, 'replace_empty');
You could use the array_walk_recursive function, with a callback function that would replace empty by 0.
For example, considering your array is declared this way :
$myArray[0] = array(23, empty, 43, 12);
$myArray[1] = array(empty, empty, 53, 19);
Note : I supposed you made a typo, and your arrays are not containing only a string, but several sub-elements.
You could use this kind of code :
array_walk_recursive($myArray, 'replacer');
var_dump($myArray);
With the following callback functon :
function replacer(& $item, $key) {
if ($item === empty) {
$item = 0;
}
}
Note that :
the first parameter is passed by reference !
which means modifying it will modify the corresponding value in your array
I'm using the === operator for the comparison
And you'd get the following output :
array(
0 =>
array
0 => int 23
1 => int 0
2 => int 43
3 => int 12
1 =>
array
0 => int 0
1 => int 0
2 => int 53
3 => int 19)
I would foreach in both indices (not tested):
foreach($array as $country){
foreach($country as &$field){
if($field[1] == 'empty'){
$field[1] = 0;
}
}
}
(I assume empty is a string)
EDIT:
If this [1990,0.00] is not an array but a string, you could use str_replace instead
foreach($array as $country){
foreach($country as &$field){
$field = str_replace('empty', '0.00', $field);
}
}
}

Categories