How to find minimums of array ( local minimums graphic) - php

i need to find minimums of graphic.
Code :
$mindots = array();
for ($i = 0; $i<=sizeof($chart); $i++ ) {
if ( $chart[$i] >=0 ) {
$mindots[1] = $i;
if ($chart[$i+1] >= $chart[$i] && $chart[$i+2] >= $chart[$i] ) {
break;
}
}
}
for ($i = $mindots[1]+2; $i<=sizeof($chart); $i++ ) {
if ( $chart[$i] >=0 ) {
$mindots[2] = $i;
if ($chart[$i+1] >= $chart[$i] && $chart[$i+2] >= $chart[$i]) {
break;
}
}
}
for ($i = $mindots[2]+2; $i<=sizeof($chart); $i++ ) {
if ( $chart[$i] >=0 ) {
$mindots[3] = $i;
if ($chart[$i+1] >= $chart[$i] && $chart[$i+2] >= $chart[$i] ) {
break;
}
}
}
Here you can see graphics :
as result i have :
array(3) { [1]=> int(0) [2]=> int(6) [3]=> int(8) }
but i need numbers like ~ 6 22 36 .
My graph size ( x coord max ) in range 35-45 numbs.
My values :
0.0;2.0
1.0;4.0
2.0;16.0
3.0;18.0
4.0;7.0
5.0;4.0
6.0;2.0
7.0;2.0
8.0;5.0
9.0;7.0
10.0;10.0
11.0;10.0
12.0;12.0
13.0;7.0
14.0;5.0
15.0;9.0
16.0;10.0
17.0;11.0
18.0;12.0
19.0;6.0
20.0;2.0
21.0;0.0
22.0;2.0
23.0;6.0
24.0;11.0
25.0;11.0
26.0;12.0
27.0;11.0
28.0;6.0
29.0;7.0
30.0;11.0
31.0;10.0
32.0;11.0
33.0;10.0
34.0;2.0
35.0;0.0
36.0;0.0
37.0;2.0
38.0;4.0
39.0;14.0
40.0;15.0
41.0;7.0
42.0;4.0
Or in array form ( print )
Array ( [0] => 2 [1] => 4 [2] => 16 [3] => 18 [4] => 7 [5] => 4 [6] => 2 [7] => 2 [8] => 5 [9] => 7 [10] => 10 [11] => 10 [12] => 12 [13] => 7 [14] => 5 [15] => 9 [16] => 10 [17] => 11 [18] => 12 [19] => 6 [20] => 2 [21] => 0 [22] => 2 [23] => 6 [24] => 11 [25] => 11 [26] => 12 [27] => 11 [28] => 6 [29] => 7 [30] => 11 [31] => 10 [32] => 11 [33] => 10 [34] => 2 [35] => 0 [36] => 0 [37] => 2 [38] => 4 [39] => 14 [40] => 15 [41] => 7 [42] => 4 )
If someone dont understand graph , i made another (with another values )
How can i do it?

If I'm understand your question right, then next code might help you:
$localMinimums = array ();
for ($i = 1; $i<count($chart)-1; $i++) {
if ($chart[$i-1] > $chart[$i] && $chart[$i+1] >= $chart[$i]) {
$localMinimums[$i] = $chart[$i];
}
}
asort($localMinimums);
$result = array_slice($localMinimums, 0, 3); //I think that you need only 3 minimum elements
Or maybe you need slice by thresold?

Sort your array:
// Sorts by value..
asort($chart);
then just get min:
// Smallest
$smallest = $chart[0];

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.

How to create a looping random number generator

I need to create a looping random number generator so each loops pumps out a different set of random numbers.
e.g result would be:
9463216549
6541335466
6749746326
6546879994
Code I have so far is:
<?php
$limit1 = 10;
$counter1 = 1;
$limit2 = 4;
$counter2 = 1;
while ($counter2 <= $limit2) {
while ($counter1 <= $limit1) {
$rayndom = mt_rand(0,6);
$counter1++;
}
$counter2++;
}
?>
If you are using php version > 7 you can use inbuilt function random_int():
Generates cryptographically secure pseudo-random integers
Usage of random_int():
random_int(0, 1000); // 0 is min value and 1000 is max
random_int() is always safe alternative to rand() and mt_rand()
If you are using PHP version < 7.0 then you can take a look at userland implementation of random_int i.e. random_compat.
Here is the code hope this helps
<?php
$waves = array(
array(),
array(),
array(),
array()
);
foreach($waves as $wave) {
for($counter = 1; $counter <= 10; $counter++) {
$num = mt_rand(0,6);
array_push($wave, $num);
}
print_r($wave);
}
?>
Your array will look like this
Array
(
[0] => 0
[1] => 2
[2] => 1
[3] => 0
[4] => 2
[5] => 1
[6] => 2
[7] => 6
[8] => 5
[9] => 0
)
Array
(
[0] => 4
[1] => 2
[2] => 2
[3] => 5
[4] => 5
[5] => 5
[6] => 6
[7] => 0
[8] => 4
[9] => 2
)
Array
(
[0] => 5
[1] => 4
[2] => 2
[3] => 5
[4] => 4
[5] => 3
[6] => 0
[7] => 3
[8] => 5
[9] => 2
)
Array
(
[0] => 2
[1] => 1
[2] => 4
[3] => 5
[4] => 0
[5] => 2
[6] => 4
[7] => 4
[8] => 4
[9] => 6
)
each array will be one wave

Years in Array to key, then populate with many dates in that year

So I have an array like this pulled from the database:
Array
(
[0] => stdClass Object
(
[created] => 2012-08-22 00:00:00
)
[1] => stdClass Object
(
[created] => 2012-08-23 00:00:00
)
[2] => stdClass Object
(
[created] => 2012-08-24 00:00:00
)
[3] => stdClass Object
(
[created] => 2012-08-24 00:00:00
)
[4] => stdClass Object
(
[created] => 2012-08-24 00:00:00
)
...
There's currently several thousand records...
From that, I'm trying to build an array like what's below
Array (
[2016] array (
[01] array(), //Jan
[02] array( //Feb
[01] = 8 // number of occurances
[02] = 6 // number of occurances
.... etc
),
... etc
)
[2015] array(
[01] array(), //Jan
[02] array(), //Feb
... etc
)
... etc...
)
I'm not entirely sure how to do this completely dynamically (IE: no hard coding the year keys).
I'm open to other ways to pull the information from the database as well using MySQL instead of PHP/foreach loops on thousands of records.
Not that it matters significantly, I'm using Codeigniter 3 on PHP 5.6 & MySQL 5.6.28 so I tagged those.
Edit: If you haven't guessed, I'm creating graphs...
EDIT
As requested by #WEBjuju below is my working method. During the process, I discovered I also needed totals for the year and each month. I have not yet tested this for speed.
// much of this function provided by Stackoverflow user #Webjuju
// http://stackoverflow.com/posts/41067942
$all_dates = $this->db->select('created')->order_by('created', 'ASC')->get('jobs')->result(); // produces original array above
foreach ($all_dates as $obj)
{
$stt = strtotime($obj->created);
list($year, $month, $day) = explode('-', date('Y-m-d', $stt));
#$target[$year]['total']++;
#$target[$year][$month]['total']++;
// increment the string for each occurrence of that day/month/year
#$target[$year][$month]['counts'][$day]++;
}
$minyear = 2012; // TODO: get this dynamically
$maxyear = date('Y');
for ($y = $minyear; $y <= $maxyear; $y++)
{
for ($m = 1; $m < 13; $m++)
{
$m01 = (strlen($m)<2) ? "0$m" : $m;
$days_in_m = date('t', strtotime("$y-$m-01"));
for ($d = 1; $d <= $days_in_m; $d++)
{
// single digit? add preceeding 0
$d01 = (strlen($d)<2) ? "0$d" : $d;
// if day isn't in array, add it with 0 as a value
if (empty($target[$y][$m01]['counts'][$d01])) $target[$y][$m01]['counts'][$d01] = 0;
// put in correct place but works for now
ksort($target);
ksort($target[$y]);
ksort($target[$y][$m01]);
ksort($target[$y][$m01]['counts']);
}
}
}
$data->trends = $target;
Which now produces an array like this:
Array
(
[2013] => Array
(
[total] => 1510 // total for year
[01] => Array // Jan
(
[counts] => Array
(
[01] => 0 //Jan 1
[02] => 1 // Jan 2
[03] => 1 //...
[04] => 3
[05] => 0
[06] => 0
[07] => 2
[08] => 1
[09] => 2
[10] => 4
[11] => 5
[12] => 0
[13] => 0
[14] => 0
[15] => 5
[16] => 21
[17] => 0
[18] => 1
[19] => 0
[20] => 0
[21] => 0
[22] => 0
[23] => 3
[24] => 2
[25] => 13
[26] => 0
[27] => 0
[28] => 24
[29] => 4
[30] => 3
[31] => 6
)
[total] => 101 // total for Jan
)
...
// repeats for all years found in db
// and all months and days for those years.
I now have working graphs...
A single loop over the $arr pulled from the database will do:
foreach ($arr as $obj) {
$stt = strtotime($obj->created);
list($year, $month, $day) = explode('-', date('Y-m-d', $stt));
// #$target[$year][$month] = array(); // original OP request
// increment the string for each occurrence of that day/month/year
#$target[$year][$month][$day]++;
}
die('<pre>'.print_r($target,true));
Producing
Array
(
[2012] => Array
(
[08] => Array
(
[22] => 1
[23] => 1
[24] => 1
)
[09] => Array
(
[22] => 1
[23] => 1
)
)
[2013] => Array
(
[08] => Array
(
[24] => 2
)
)
)
(from the following test data):
$_01 = new stdClass();
$_01->created = '2012-08-22 00:00:00';
$_02 = new stdClass();
$_02->created = '2012-08-23 00:00:00';
$_03 = new stdClass();
$_03->created = '2012-08-24 00:00:00';
$_04 = new stdClass();
$_04->created = '2012-09-22 00:00:00';
$_05 = new stdClass();
$_05->created = '2012-09-23 00:00:00';
$_06 = new stdClass();
$_06->created = '2013-08-24 00:00:00';
$_07 = new stdClass();
$_07->created = '2013-08-24 00:00:00';
$arr = [$_01, $_02, $_03, $_04, $_05, $_06, $_07];
EDIT - FILL IN THE ZEROS!
for ($y = $minyear; $y <= $maxyear; $y++) {
for ($m = 1; $m < 13; $m++) {
$m01 = (strlen($m)<2) ? "0$m" : $m;
$days_in_m = date('t', strtotime("$y-$m-01"));
for ($d = 1; $d <= $days_in_m; $d++) {
$d01 = (strlen($d)<2) ? "0$d" : $d;
if (empty($target[$y][$m01][$d01])) $target[$y][$m01][$d01] = 0;
}
}
}
THEN THE KEY SORTING
foreach ($target as &$month) {
uksort($month, zeroonesort);
foreach ($month as $day) {
uksort($day, zeroonesort);
}
}
function zeroonesort($a, $b) {
$ai = (integer) $a;
$bi = (integer) $b;
// in php 7 use the spaceship operator <=>
// otherwise do it the old way
switch (true) {
case ($ai == $bi): return 0;
case ($ai > $bi): return 1;
case ($ai < $bi): return -1;
}
}
PRODUCES
Array
(
[2012] => Array
(
[01] => Array
(
[01] => 0
[02] => 0
[03] => 0
[04] => 0
[05] => 0
[06] => 0
[07] => 0
[08] => 0
[09] => 0
[10] => 0
[11] => 0
[12] => 0
[13] => 0
[14] => 0
[15] => 0
[16] => 0
[17] => 0
[18] => 0
[19] => 0
[20] => 0
[21] => 0
[22] => 0
[23] => 0
[24] => 0
[25] => 0
[26] => 0
[27] => 0
[28] => 0
[29] => 0
[30] => 0
[31] => 0
)
[02] => Array
(
[01] => 0
[02] => 0
[03] => 0
[04] => 0
[05] => 0
[06] => 0
[07] => 0
[08] => 0
[09] => 0
[10] => 0
[11] => 0
[12] => 0
[13] => 0
[14] => 0
[15] => 0
[16] => 0
[17] => 0
[18] => 0
[19] => 0
[20] => 0
[21] => 0
[22] => 0
[23] => 0
[24] => 0
[25] => 0
[26] => 0
[27] => 0
[28] => 0
[29] => 0
)
[03] => Array
(
etc...

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

Php, printing a 3d array into a table

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)

Categories