how to sort the following php array? - php

Question
Array ( [0] => 12w12 [1] => 13w12 [2] => 14w12 [3] => 15w12 [4] => 2w13 [5] => 3w13 [6] => 4w13 [7] => 3w12 [8] => 7w12 [9] => 9w12 )
Answer should be
Array ( [0] => 3w12 [1] => 7w12 [2] => 9w12 [3] => 12w12 [4] => 13w12 [5] => 14w12 [6] => 15w12 [7] => 2w13 [8] => 3w13 [9] =>4w13 )

You can use PHP usort and write your own comparison function.

function cmp($a, $b){
if ($a == $b) { return 0; }
list($first1, $last1) = explode("w", $a);
list($first2, $last2) = explode("w", $b);
return (($last1.$first1) < ($last2.$first2)) ? -1 : 1;
}
usort($array, "cmp");

do a bit search on php.net for usort and make your own sorting method as what you are trying to achieve don't seem standard ordering at all

Related

Sorting an array of arrays by specific index descending [duplicate]

This question already has answers here:
Sort an array of associative arrays by column value
(23 answers)
Closed 5 years ago.
I have the array below
Array
(
[Prod1] => Array
(
[0] => $1,167,788.03
[1] => 26,872
[2] => 73.42
[3] => 19.0%
[4] => $1,134,106.83
[5] => $1,681,843.02
[6] => $3,098.65
[7] => $42.20
[8] => $-19.55
[9] => $-9.60
[10] => $43.46
[11] => 0.97
)
[Prod2] => Array
(
[0] => $6,730.84
[1] => 161
[2] => 0.44
[3] => 13.7%
[4] => $4,783.41
[5] => $6,755.61
[6] => $13.07
[7] => $29.71
[8] => $-27.30
[9] => $-21.50
[10] => $41.81
[11] => 0.71
)
[Prod3] => Array
(
[0] => $2,498,984.47
[1] => 30,409
[2] => 83.08
[3] => 21.5%
[4] => $3,026,866.16
[5] => $3,850,645.25
[6] => $8,270.13
[7] => $99.54
[8] => $-21.33
[9] => $-8.19
[10] => $82.18
[11] => 1.21
)
}
I am trying to sort it on descending order based of the index[0] and tried using different PHP built in functions but I was not successful on that.
Basically the desired result would be in the following order Prod3, Prod1, Prod2.
What would be the best way for solution for this?
Thanks
supposing $arr is the array you've displayed above, the below code should work in your case
$tmp_arr = array();
$sorted_arr = array();
foreach ($arr as $key => $val_array) {
//removing $ and ,
$first_index_without_dollar = str_replace(array("$", ","), array("", ""), $val_array[0]);
//getting string as number in order to sort
$first_index_without_dollar = number_format((float) $first_index_without_dollar, 2, '.', '');
$tmp_arr[$first_index_without_dollar] = $key;
}
//sorting by key descending
krsort($tmp_arr);
foreach ($tmp_arr as $val) {
$sorted_arr[$val] = $arr[$val];
}
I don't know if usort is the best way but the user function is fairly simple:
usort($array, function ($a, $b) {
$x = (int)str_replace(['$',',','.'],'',$a[0];
$y = (int)str_replace(['$',',','.'],'',$b[0];
if ($x === $y) return 0;
return ($x < $y) ? 1 : -1;
});
Edit: I missed the format on the values the first time. And I would not have found it without Mohamad Attat's answer. The str_replace calls should fix it, iff you always have two decimals in your dollar amounts.

How can I sort this array in PHP?

I've been trying to use the sort() function to rearrange the array from smallest to largest.
This is my print_r of the array which came from serialized data that was imploded:
Array
(
[0] => 127173
[1] => 127172
[2] => 127174
[3] => 127175
[4] => 127178
[5] => 127176
[6] => 127177
[7] => 127179
[8] => 127180
[9] => 127183
[10] => 127181
)
With sort() and asort() I just get a 1 returning.
Try this code... in fact the sort function is working fine.
$array = Array
(
'0' => 127173,
'1' => 127172,
'2' => 127174,
'3' => 127175,
'4' => 127178,
'5' => 127176,
'6' => 127177,
'7' => 127179,
'8' => 127180,
'9' => 127183,
'10' => 127181
);
sort($array); // <= Sort the array desc
foreach( $array as $key => $value ){
echo $key."\t=>\t".$value."\n";
}
Consider that sort function actually alters your array and returns bool. See doc.
Check this example online
Use asort(), like this:
$A = Array (127173,127172,127174,127175,127178,127176,127177,127179,127180,127183,127181);
asort($A);
print_r($A);
Result:
Array ( [1] => 127172 [0] => 127173 [2] => 127174 [3] => 127175 [5] => 127176 [6] => 127177 [4] => 127178 [7] => 127179 [8] => 127180 [10] => 127181 [9] => 127183 )
Compare sorting-functions here: http://php.net/manual/en/array.sorting.php

Shuffle inside array php

how to shuffle arrays in array ? I tried lots of way but I can't achieve that. I think it's very simple but I'm stuck on that.
Array
(
[2] => Array
(
[0] => 12011190
[1] => 12011158
[2] => 12011583
[3] => 12012107
[4] => 12011222
[5] => 12010638
[6] => 12013836
[7] => 12012232
[8] => 12011256
[9] => 12010007
[10] => 12012531
[11] => 12012182
[12] => 12013253
)
[6] => Array
(
[0] => 12011565
[1] => 12010020
[2] => 12011352
[3] => 12014366
[4] => 12011879
[5] => 12011449
)
)
I want to shuffle within arrays. I hope explain...
As far as I know, you can do it like this (assuming you want to shuffle every sub-array independently):
foreach($array AS &$element) {
shuffle($element);
}
Or maybe like this:
array_walk($array, function(&$value, $key) {
shuffle($value);
});
Here is a recursive multi-level function you can use.
function shuffle_array($arr) {
if (!is_array($arr)) return $arr;
shuffle($arr);
foreach ($arr as $key => $a) {
if (is_array($a)) {
$arr[$key] = shuffle_array($a);
}
}
return $arr;
}
print_r(shuffle_array($array));

Convert associative array into indexed

Ive seen a few examples by using array_values, but cant quite make out how to get it to work...
I have an associative array thats passed via POST, I need to convert it into a indexed array...
My print_r($_POST) gives me this... I need all of this put into an indexed array :)
Array (
[fieldnames] => 36771X21X198|36771X21X199|36771X21X200|36771X21X201|36771X21X202
[36771X21X198] => 3434343
[display36771X21X198] => on
[36771X21X199] => 5656565
[display36771X21X199] => on
[36771X21X200] => 89898989
[display36771X21X200] => on
[36771X21X201] => 90909090
[display36771X21X201] => on
[36771X21X202] => 12121212
[display36771X21X202] => on
[move] => movesubmit
[move2] => ONLINE Submit
[thisstep] => 1
[sid] => 36771
[token] => 1234567890
)
Observe this amazing way to convert your $_POST into a numerically indexed array:
$numerical = array_values($_POST);
but what if you want to preserve your keys? Perhaps you want something like this?
$numerical = array();
$sep = ':';
foreach($_POST as $k=>$v)
{
$numerical[] = $k.$sep.$v;
}
$numerical will then have:
Array
(
[0] => fieldnames:36771X21X198|36771X21X199|36771X21X200|36771X21X201|36771X21X202
[1] => 36771X21X198:3434343
[2] => display36771X21X198:on
[3] => 36771X21X199:5656565
[4] => display36771X21X199:on
[5] => 36771X21X200:89898989
[6] => display36771X21X200:on
[7] => 36771X21X201:90909090
[8] => display36771X21X201:on
[9] => 36771X21X202:12121212
[10] => display36771X21X202:on
[11] => move:movesubmit
[12] => move2:ONLINE Submit
[13] => thisstep:1
[14] => sid:36771
[15] => token:1234567890
)
or, for my final example:
$fieldnames_original = explode('|', $_POST['fieldnames']);
$fieldnames_actual = array();
$values = array();
foreach($_POST as $k=>$v)
{
if($k!='fieldnames')
{
$fieldnames_actual[] = $k;
$values[] = $v;
}
}
which will set 3 arrays:
$fieldnames_original:
Array
(
[0] => 36771X21X198
[1] => 36771X21X199
[2] => 36771X21X200
[3] => 36771X21X201
[4] => 36771X21X202
)
$fieldnames_actual:
Array
(
[0] => 36771X21X198
[1] => display36771X21X198
[2] => 36771X21X199
[3] => display36771X21X199
[4] => 36771X21X200
[5] => display36771X21X200
[6] => 36771X21X201
[7] => display36771X21X201
[8] => 36771X21X202
[9] => display36771X21X202
[10] => move
[11] => move2
[12] => thisstep
[13] => sid
[14] => token
)
and $values:
Array
(
[0] => 3434343
[1] => on
[2] => 5656565
[3] => on
[4] => 89898989
[5] => on
[6] => 90909090
[7] => on
[8] => 12121212
[9] => on
[10] => movesubmit
[11] => ONLINE Submit
[12] => 1
[13] => 36771
[14] => 1234567890
)
function
function array_default_key($array) {
$arrayTemp = array();
$i = 0;
foreach ($array as $key => $val) {
$arrayTemp[$i] = $val;
$i++;
}
return $arrayTemp;
}
Pass the associative array as a parameter and it will convert into the default index of the array. For example: we have Array('2014-04-30'=>43,'2014-04-29'=>41) after the call to the function the array will be Array(0=>43,1=>41).
Recursive assoc to indexed converter tested on a small array.
function assoc2indexedMulti($arr) {
// initialize destination indexed array
$indArr = array();
// loop through source
foreach($arr as $val) {
// if the element is array call the recursion
if(is_array($val)) {
$indArr[] = assoc2indexedMulti($val);
// else add the value to destination array
} else {
$indArr[] = $val;
}
}
return $indArr;
}

array_intersect() in php with special purpose

Hi need to intersect two arrays in a special function. the two arrays are:
Array A (
[0] => 104-20_140.1 [1] => 104-10_136.1 [2] => 104-40_121.1 [3] => 104-41_122.1
[4] => 200-42_951.1 [5] => 200-43_952.1 [6] => 200-44_123.1 [7] => 200-45_124.1
[8] => 300-46_125.1 [9] => 300-47_126.1 [10] => 300-48_127.1 [11] => 300-49_128.1
[9] => 380-56_125.1 [10] => 380-57_126.1 [11] => 380-58_127.1 [12] => 380-59_128.1
)
Array B (
[0] => 200 [1] => 300
)
I need two look at Array A's beginning of the value. Ex. [0] => 104-20_140 and see if the beginning '104' it excists in Array B. If not Array A shall remove it from the result array C.
the output with Array A and B shall have:
Array C (
[0] => 200-42_951.1 [1] => 200-43_952.1 [2] => 200-44_123.1 [3] => 200-45_124.1
[4] => 300-46_125.1 [5] => 300-47_126.1 [6] => 300-48_127.1 [7] => 300-49_128.1
)
All shall be calculated in Php
thx for all the help!
Try this:
function startsWith($haystack, $needle) {
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
$C = array();
foreach ($A as $ka => $va) {
foreach ($B as $kb => $vb) {
if (startsWith($va, $vb)) {
$C[] = $va;
}
}
}
example on codepad
Chances are, what you really need is array_uintersect. This will give the option to provide a custom callback which contains the logic on how to check if values intersect.
http://uk3.php.net/manual/en/function.array-uintersect.php
In the callback, you will need to parse out first section before the first "-" using substr or one of the preg functions.

Categories