PHP sorting an array with uasort - php

So I sorted a multidimensional array with uasort in descending order. I did a var_dump($winrateArray) and it is sorted properly. The highest value is in the first returned array. However when I try a var_dump($winrateArray[0][3]) which is where I expect the highest value to be it isn't there. Instead it is in $winrateArray[1][3]. Am I using uasort properly?
Unsorted dump:
array(2) { [0]=> array(4) { [0]=> string(2) "18" [1]=> string(1) "1" [2]=> int(0) [3]=> int(1) } [1]=> array(4) { [0]=> string(2) "31" [1]=> string(1) "1" [2]=> int(100) [3]=> int(101) } }
Sorted dump:
array(2) { [1]=> array(4) { [0]=> string(2) "31" [1]=> string(1) "1" [2]=> int(100) [3]=> int(101) } [0]=> array(4) { [0]=> string(2) "18" [1]=> string(1) "1" [2]=> int(0) [3]=> int(1) } }
Specific dump:
int(1)
.
$winrateArray[0][0] = '18';
$winrateArray[0][1] = '1';
$winrateArray[0][2] = 0;
$winrateArray[0][3] = 1;
$winrateArray[1][0] = '31';
$winrateArray[1][1] = '1'
$winrateArray[1][2] = 100;
$winrateArray[1][3] = 101;
var_dump($winrateArray);
function cmp($a, $b){
if ($a[3] == $b[3]){
return 0;
}
return ($a[3] < $b[3]) ? 1 : -1;
}
uasort($winrateArray, 'cmp');
var_dump($winrateArray);
var_dump($winrateArray[0][3]);

You can do:
$newArray = $winrateArray;
uasort($winrateArray, 'cmp');
$i = 0;
foreach($winrateArray as $key => $item) {
$newArray[$i][3] = $item[3];
$i++;
}
and use the new array after that. You will preserve keys and you will reorder and replace 3rd elements only

Related

Taking every n-th element of one array and putting it into another array PHP

So I have an array like this:
array(6) { [0]=> string(11) "12323423423" [1]=> string(4) "tito" [2]=> string(6) "235345" [3]=> string(14) " 564534534534" [4]=> string(5) "kralj" [5]=> string(6) "435345" }
Depending on number of elements from another array called $anotherArray, let's say $anotherArray has 3 elements, I should take first 3 elements of first array, then if there are second 3 elements and so on, and put them into another array. I tried it like so:
$lengthManuelni=count($string);// $string being array displayed uphere
$lengthAnothera=count($anotherArray);
for ($i = 0; $i < $lengthManuelni; $i += $lengthAnothera) {
for ($j = 0; $j < $lengthAnothera; $j++) {
$restructured [$j] = $string[$i + $j];
var_dump($restructured);
}
}
So i would like this $restructured array to look like this:
array(2) { [0]=> string(23) "12323423423,tito,235345" [1]=> string(28) " 564534534534,kralj,435345" }
Instead it when I do var_dump($restructured) it looks like this:
array(1) { [0]=> string(11) "12323423423" } array(2) { [0]=> string(11) "12323423423" [1]=> string(4) "tito" } array(3) { [0]=> string(11) "12323423423" [1]=> string(4) "tito" [2]=> string(6) "235345" } array(3) { [0]=> string(14) " 564534534534" [1]=> string(4) "tito" [2]=> string(6) "235345" } array(3) { [0]=> string(14) " 564534534534" [1]=> string(5) "kralj" [2]=> string(6) "235345" } array(3) { [0]=> string(14) " 564534534534" [1]=> string(5) "kralj" [2]=> string(6) "435345" }
Please help, I'm stuck with this.
It's much simpler to achieve this using array_chunk and array_map functions:
$restructured = array_map(function($v){
return implode(",", $v);
}, array_chunk($lengthManuelni, 3));
print_r($restructured);
The output:
Array
(
[0] => 12323423423,tito,235345
[1] => 564534534534,kralj,435345
)
http://php.net/manual/en/function.array-chunk.php

Count multidimensional array

I have my main array:
array(6) {
[1]=> array(3) {
[0]=> string(15) "Extension"
[1]=> int(1)
[2]=> string(6) "3,00 "
}
[2]=> array(3) {
[0]=> string(32) "Physics "
[1]=> string(1) "1"
[2]=> string(6) "3,00 "
}
[3]=> array(3) {
[0]=> string(31) "Physics "
[1]=> int(1)
[2]=> string(6) "6,00 "
}
[4]=> array(3) {
[0]=> string(34) "Desk"
[1]=> int(4)
[2]=> string(8) "127,00 "
}
[5]=> array(3) {
[0]=> string(18) "assistance"
[1]=> int(1)
[2]=> string(7) "12,50 "
}
[6]=> array(3) {
[0]=> string(15) "Extension"
[1]=> int(1)
[2]=> string(6) "3,00 "
}
}
My expected output is:
Extension 2
Physics 2
Desk 1
Assistance 1
The result must be in an resultarray
How can I do? I tried with array_count_values function but don't work.
How can I stock answear:
I tried this code but It doesn't work
$tabrecap = array();
foreach($counts as $key=>$value){
//echo $key." qte".$value;
$tabrecap = array ($key,$value,$valueOption);
}
As you asked in comment,Please try this:-
<?php
$array = array( '1'=> array('0'=>"Extension", '1'=> 1, '2'=>"3,00 " ), '2'=> array('0'=>"Physics",'1'=>"1","3,00 " ),'3'=> array('0'=>"Physics",'1'=>1,"6,00 "),'4'=> array('0'=>"Desk",'1'=>4,"127,00 "),'5'=> array('0'=>"assistance",'1'=>1,"12,50 " ),'6'=> array('0'=>"Extension",'1'=>1,"3,00 "));
$count = array();
$i = 0;
foreach ($array as $key=>$arr) {
// Add to the current group count if it exists
if (isset($count[$i][$arr[0]])) {
$count[$i][$arr[0]]++;
}
else $count[$i][$arr[0]] = 1;
$i++;
}
print_r($count);
?>
Output:- https://eval.in/379176
Looping is the answer.
<?php
// untested
$counts = Array();
foreach( $array as $subArray ){
$value = $subArray[0];
$counts[ $value ] = ( isset($counts[ $value ]) )
? $counts[ $value ] + 1
: 1;
}
var_dump( $counts);
Just make a loop and use first item of each array as key :
$array = array(
array("Extension", 1, "3,00"),
array("Physics", "1", "3,00"),
array("Physics", 1, "6,00 ")
);
$count = array();
foreach($array as $a)
$count[$a[0]]++;
var_dump($count); // array(2) { ["Extension"]=> int(1) ["Physics"]=> int(2) }

How right replace value element in array?

I want to compare the key values ​​in the array and if a match is found to increase the value of the element.
For this I use code:
// var_dump $all_array
array(2) {
[0]=> array(6) {
[0]=> string(8) "art_7880"
[1]=> string(11) "Арт.7880"
[2]=> string(1) "1"
[3]=> NULL
[4]=> string(45) "png"
[5]=> int(1372269755)
}
[1]=> array(6) {
[0]=> string(8) "art_7880"
[1]=> string(11) "Арт.7880"
[2]=> string(1) "1"
[3]=> NULL
[4]=> string(45) "png"
[5]=> int(1372269874)
}
}
// var_dump $count
array(2) { [0]=> string(2) "10" [1]=> string(1) "1" }
// var_dump $product
array(2) { [0]=> string(10) "1372269755" [1]=> string(10) "1372269874" }
$count=$_POST['count'];
$product=$_POST['product'];
$count_arr_products=count($product);
for ($i=0; $i<=$count_arr_products; $i++){
foreach ($all_array as $keys => $elms) {
if ($product[$i]==$elms[5]) {
if($count[$i] > 0) {
$elms[2] = $count[$i];
} else {
unset($keys);
}
}
}
}
but step $elms[2] = $count[$i]; not work - in result value $elms[2] not change...
You need to majke $elms a reference. By default it will be a copy of the sub-array, so assignment won't update the original array.
$all_array = array(array("art_7880", "Арт.7880", "1", NULL, "png", 1372269755),
array("art_7880", "Арт.7880", "1", NULL, "png", 1372269874));
$count = array("10", "1");
$product = array("1372269755", "1372269874");
$count = array("10", "1");
$product = array("1372269755", "1372269874");
$count_arr_products = count($product);
for($i=0; $i<$count_arr_products; $i++){ // Use < not <=
foreach ($all_array as $keys => &$elms) { // Use a reference so we can update it
if ($product[$i]==$elms[5]){
if ($count[$i] > 0) {
$elms[2] = $count[$i];
} else {
unset($all_array[$keys]); // not unset($keys)
}
}
}
}
var_dump($all_array);
Output:
array(2) {
[0]=>
array(6) {
[0]=>
string(8) "art_7880"
[1]=>
string(11) "Арт.7880"
[2]=>
string(2) "10"
[3]=>
NULL
[4]=>
string(3) "png"
[5]=>
int(1372269755)
}
[1]=>
&array(6) {
[0]=>
string(8) "art_7880"
[1]=>
string(11) "Арт.7880"
[2]=>
string(1) "1"
[3]=>
NULL
[4]=>
string(3) "png"
[5]=>
int(1372269874)
}
}

How to get a count each unique element in the general array

Good day.
Code:
array(4) {
[0]=> array(1) {
[0]=> array(3) {
[0]=> string(11) "art_7880" [1]=> string(1) "1" [2]=> int(2950)
}
[1]=> array(3) {
[0]=> string(8) "art_7880" [1]=> string(1) "1" [2]=> int(2955)
}
[2]=> array(3) {
[0]=> string(8) "art_7880" [1]=> string(1) "1" [2]=> int(1335)
}
[3]=> array(3) {
[0]=> string(8) "art_7883" [1]=> string(1) "1" [2]=> int(4335)
}
}
I get array unique elements:
$arr_uniq = array();
foreach ($all_array as $keys => $elms ) {
if(!in_array($elms[0], $arr_uniq)) {
$arr_uniq[] = $elms[0];
}
}
Tell me pleasse how to get a count each unique element in the general array?
result should been next:
art_7880 - 3
art_7883 - 1
Assuming $all_array is subarray of your main array in your var_dump snipett, the general idea is
$result = array();
foreach ($all_array as $elms)
$result[$elms[0]]++;
array_count_values()
http://php.net/array_count_values
You should be able to easily apply this function.

How to re-sort a multidimensional array?

How to sort the array alphabetically below using as the key criterion label? I tried using array_multisort, usort, rsort, and sort, but it did not work.
array(3) {
[0]=>
array(2) {
["id"]=>
string(1) "9"
["label"]=>
string(26) "ffffff"
}
[1]=>
array(2) {
["id"]=>
string(2) "10"
["label"]=>
string(25) "aaaaaaaaa"
}
[2]=>
array(2) {
["id"]=>
string(1) "6"
["label"]=>
string(5) "dddddd"
}
}
You can sort the array using both usort() and strcmp()
usort($arr, function($e1, $e2)
{
$cmp = strcmp($e1['label'], $e2['label']);
if($cmp == 0) { return 0; }
return $cmp > 0 ? 1 : -1;
});

Categories