Reverse sort array with rearranging of index - php

So currently I'm having a problem where I have an array setup like this
array(8) {
[0]=> array(2) {
[0]=> float(2.1166666666667)
[1]=> string(7) "9434493"
}
[1]=> array(2) {
[0]=> float(2.07)
[1]=> string(7) "8591971"
}
[2]=> array(2) {
[0]=> float(2.0566666666667)
[1]=> string(8) "17015102"
}
[3]=> array(2) {
[0]=> float(2.0366666666667)
[1]=> string(7) "9637191"
}
[4]=> array(2) {
[0]=> float(2.015)
[1]=> string(8) "11405473"
}
[5]=> array(2) {
[0]=> float(1.9833333333333)
[1]=> string(8) "28233403"
}
[6]=> array(2) {
[0]=> float(2.0366666666667)
[1]=> string(8) "14248330"
}
[7]=> array(2) {
[0]=> float(2.0933333333333)
[1]=> string(8) "14987165"
}
}
After I use the function arsort() it looks like this:
array(8) {
[0]=> array(2) {
[0]=> float(2.1166666666667)
[1]=> string(7) "9434493"
}
[7]=> array(2) {
[0]=> float(2.0933333333333)
[1]=> string(8) "14987165"
}
[1]=> array(2) {
[0]=> float(2.07)
[1]=> string(7) "8591971"
}
[2]=> array(2) {
[0]=> float(2.0566666666667)
[1]=> string(8) "17015102"
}
[6]=> array(2) {
[0]=> float(2.0366666666667)
[1]=> string(8) "14248330"
}
[3]=> array(2) {
[0]=> float(2.0366666666667)
[1]=> string(7) "9637191"
}
[4]=> array(2) {
[0]=> float(2.015)
[1]=> string(8) "11405473"
}
[5]=> array(2) {
[0]=> float(1.9833333333333)
[1]=> string(8) "28233403"
}
}
So all cool that my array is sorted by the value of [0] index. But.....
When I try to loop through like this...
$x = 0;
while ($x < count($sorted_array)) {
$sorted_array[$x][0];
$x++;
}
It kept printing out the original array order. I then realized when I used the function arsort() it kept the original order of the indexes so that's why it was printing in the original array order.
Is there a function to fix this so I can loop it with an index? Any help will be much appreciated.

When you use arsort() you preserve the keys.
Because you are iterating using $x, you are effectively ignoring your sort call.
Either use rsort() with your loop.
Or use a foreach() loop after your arsort() call.
Or best, just call array_column() instead of looping.
Here are some demonstrations: (Demo Link)
$array=$copy=[
[2.1166666666667,9434493],
[2.07,8591971],
[2.0566666666667,17015102],
[2.0366666666667,9637191],
[2.015,11405473],
[1.9833333333333,28233403],
[2.0366666666667,14248330],
[2.0933333333333,14987165]
];
arsort($array);
var_export(array_column($array,0)); // <-- you lose the keys you preserved
echo "\n---\n";
foreach($array as $index=>$row){ // <-- you keep the keys you preserved
echo "$index : {$row[0]}\n";
}
echo "\n---\n";
rsort($copy); // you don't preserve the keys
for($x=0, $count=sizeof($copy); $x<$count; ++$x){ // you should cache the count instead of calling count() on every iteration
echo "$x : {$copy[$x][0]}\n";
}
Output:
array (
0 => 2.1166666666667,
1 => 2.0933333333333,
2 => 2.07,
3 => 2.0566666666667,
4 => 2.0366666666667,
5 => 2.0366666666667,
6 => 2.015,
7 => 1.9833333333333,
)
---
0 : 2.1166666666667
7 : 2.0933333333333
1 : 2.07
2 : 2.0566666666667
6 : 2.0366666666667
3 : 2.0366666666667
4 : 2.015
5 : 1.9833333333333
---
0 : 2.1166666666667
1 : 2.0933333333333
2 : 2.07
3 : 2.0566666666667
4 : 2.0366666666667
5 : 2.0366666666667
6 : 2.015
7 : 1.9833333333333

rsort() is great fit here.
Sort an array in reverse order
This function does not maintain index association

A quick look at PHP documentation suggests rsort() would work.
http://php.net/manual/en/array.sorting.php

Related

Array with strings to integers

I have some trouble with an array (php, wordpress) like shown below:
array(2) {
[0] => array(1) { [0]=> string(3) "416" }
[1]=> array(1) { [0]=> string(4) "1591" }
}
How to convert it to an array with integers?
The problem is that values are also arrays and not values like this:
array(2) {
[0] => "416" ,
[1]=> "1591"
}
I'm trying to get id of some posts using get_post_meta().
It is only a piece of my code:
$course_product = array();
foreach ($comment_ids as $comment_id) {
$course_product[] = get_post_meta( intval($comment_id), '_llms_wc_product_id', true );
}
It is giving me this strange array:
array(2) {
[0]=>
array(1) {
[0]=>
string(3) "416"
}
[1]=>
array(1) {
[0]=>
string(4) "1591"
}
}
Apparently, your meta data _llms_wc_product_id is itself an array. So, get the first value from it by appending [0]:
get_post_meta( intval($comment_id), '_llms_wc_product_id', true )[0]
This array can looks like that:
array(2) {
[0]=>
array(1) {
[0]=>
string(3) "416"
}
[1]=>
array(2) {
[0]=>
string(4) "1591"
[1]=>
string(3) "416"
}
}
It can't be deeper and I only need values so I'm using:
call_user_func_array('array_merge', $course_products);
to flatten it and after that it looks like:
array(3) {
[0]=> int(416)
[1]=> int(1591)
[2]=> int(416)
}
Then I can do what I wanted.
BIG THX.

Transform Static Array into a generated by foreach loop

Im a bit lost with this array here, and i need some "light" if you could.
My situation is: change some static data from a array to one generated by an foreach.
The static php code example is the following:
$p->data = array(
array(
array("A",148), //value_1 for letter A
array("B",238), //value_1 for letter B
array("C",151) //value_1 for letter C
),
array(
array("A",238), //value_2 for letter A
array("B",338), //value_2 for letter B
array("C",285) //value_2 for letter C
),
array(
array("A",278), //value_3 for letter A
array("B",138), //value_3 for letter B
array("C",205) //value_3 for letter C
)
);
My problem is: when i try to generate the same result with a foreach, i'm doing it wrong.. i already tried nested foreach's and get nothing (or doing it wrong too), but this is the closest I reach from the result i need:
foreach($result as $reg){
$p->data = array(
array(
array($reg['letters'],$reg['value_1'])
),
array(
array($reg['letters'],$reg['value_2'])
),
array(
array($reg['letters'],$reg['value_3'])
)
);
};
Instead, i get only the first Letter with the right values. If i use "$p->data[]" to show the other letters, it doesn't work.
The var_dump() i get from the code above is the following:
array(3) {
[0]=> array(1) {
[0]=> array(2) {
[0]=> string(1) "A"
[1]=> int(148) }
}
[1]=> array(1) {
[0]=> array(2) {
[0]=> string(1) "A"
[1]=> int(238) }
}
[2]=> array(1) {
[0]=> array(2) {
[0]=> string(1) "A"
[1]=> int(278) }
}
}
As you see, only the first variable 'letter' comes from the array. I need the others, like this:
array(3) {
[0]=> array(3) {
[0]=> array(2) {
[0]=> string(1) "A"
[1]=> int(148)
}
[1]=> array(2) {
[0]=> string(1) "B"
[1]=> int(238)
}
[2]=> array(2) {
[0]=> string(1) "C"
[1]=> int(151)
}
}
[1]=> array(3) {
[0]=> array(2) {
[0]=> string(1) "A"
[1]=> int(238)
}
[1]=> array(2) {
[0]=> string(1) "B"
[1]=> int(338)
}
[2]=> array(2) {
[0]=> string(1) "C"
[1]=> int(285)
}
}
[2]=> array(3) {
[0]=> array(2) {
[0]=> string(1) "A"
[1]=> int(278)
}
[1]=> array(2) {
[0]=> string(1) "B"
[1]=> int(138)
}
[2]=> array(2) {
[0]=> string(1) "C"
[1]=> int(205)
}
}
}
Explaing the code: in the foreach im using.. im getting the values from a database. Each variable "Letter" has 3 variable "Values". What im trying to do is show one same variable Value for each Letter separated on each array.
Can someone point me to the right direction? Thank you all o/
what is $reg['letters']? where does $result come from?
you will need an array of letters, and you will need to iterate that. with a little more info i could provide an example.

Php multidimensional array [matrix] sorting columns

If I have a matrix
[3,1,2,4]
[a,b,c,d]
And I need sort first row with usort key. But when I want reorder first array how do column moves at well
So output will be like this in this case described top
[1,2,3,4]
[b,c,a,d]
You can use array_multisort:
$x = [[3,1,2,4],['a','b','c','d']];
array_multisort($x[0], $x[1]);
var_dump($x);
Output:
array(2) {
[0]=>
array(4) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
}
[1]=>
array(4) {
[0]=>
string(1) "b"
[1]=>
string(1) "c"
[2]=>
string(1) "a"
[3]=>
string(1) "d"
}
}
I think what you're looking for is ksort.

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

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.

Categories