Php multidimensional array [matrix] sorting columns - php

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.

Related

Reverse sort array with rearranging of index

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

PHP : How to include another array's elements in another array's index?

I have two arrays:
array(3)
{
[0]=>
string(1) "1"
[1]=>
string(1) "1"
[2]=>
string(1) "2"
}
array(3)
{
[0]=>
string(1) "abc"
[1]=>
string(1) "def"
[2]=>
string(1) "ghi"
}
Is there any way I could put the first array's elements into the second array's index producing the following result :
array(3)
{
[1]=>
string(1) "abc"
[1]=>
string(1) "def"
[2]=>
string(1) "ghi"
}
How can I do this in PHP? Thanks in advance.
Since the expected output is impossible the next best thing it to make a multidimensional array where the question id is a subarray for answers.
$arr1 = [1,1,2];
$arr2 = ["abc","def","ghi"];
Foreach($arr1 as $key => $id){
$threads[$id][] = $arr2[$key];
}
Var_dump($threads)
Outputs:
array(2) {
[1]=>
array(2) {
[0]=> "abc"
[1]=> "def"
}
[2]=>
array(1) {
[0]=> "ghi"
}
}
https://3v4l.org/qpJDA
You can use "array_combine" method for this purpose.
http://php.net/array-combine
However, you cannot have same multiple indexes in an array
array(3)
{
[1]=> "abc"
[1]=> "def"
[2]=> "ghi"
}
You have index "1" twice in array. So "def" will replace "abc" in your array

How to sort this type of complex 3d array in php

The array is like this (I am using PHP)
array(2){[0]=>
{[0]=>"DEF"=>
{[0]=>"a",[1]=>"c",[2]=>"b"},
[1]=>"ABC"=>
{[0]=>"f",[1]=>"d",[2]=>"e"}},
[1]=>
{[0]=>"DEF"=>
{[0]=>"h",[1]=>"i",[2]=>"g"},
[1]=>"ABC"=>
{[0]=>"k",[1]=>"l",[2]=>"j"}
}
}
I wish to be sort it like the first entry i.e. [0] index has two entries DEF and ABC so it should be sorted ABC and DEF then in ABC also a b c should be sorted.
The final result should be this
array(2){[0]=>
{[0]=>"ABC"=>
{[0]=>"d",[1]=>"e",[2]=>"f"},
[1]=>"DEF"=>
{[0]=>"a",[1]=>"b",[2]=>"c"}},
[1]=>
{[0]=>"ABC"=>
{[0]=>"j",[1]=>"k",[2]=>"l"},
[1]=>"DEF"=>
{[0]=>"g",[1]=>"h",[2]=>"i"}
}
}
Thanks in advance
PHP has a custom sort option. Try this:
http://php.net/manual/en/function.usort.php
I haven't used it but I had found it previously and remember it existed.
Using sort will solve problem
$a = array(
array(
"ABC"=>array("d","e","f"),
"DEF"=>array("a","b","c")
),
array(
"ABC"=>array("j","k","l"),
"DEF"=>array("g","h","i")
)
);
sort($a);
var_dump($a);
The result
array(2) {
[0]=>
array(2) {
["ABC"]=>
array(3) {
[0]=>
string(1) "d"
[1]=>
string(1) "e"
[2]=>
string(1) "f"
}
["DEF"]=>
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
}
[1]=>
array(2) {
["ABC"]=>
array(3) {
[0]=>
string(1) "j"
[1]=>
string(1) "k"
[2]=>
string(1) "l"
}
["DEF"]=>
array(3) {
[0]=>
string(1) "g"
[1]=>
string(1) "h"
[2]=>
string(1) "i"
}
}
}

Sorting a subarray by subarray value?

I only found questions on how to sort arrays with subarray values, but I would like to know if you can sort a subarray with subarray values.
foreach ($el->getArray('plain') as $element){
foreach ($element as $data)
{
<?php echo $data['name']; ?>
<?php echo $data['value']; ?>
}
}
I think the best solution is to instantiate $array by doing:
$array = $el->getArray('plain');
before the foreach and then sort it immediately and then loop through it.
However, I am not sure you can sort an array inside an array. First, is this possible and second how would you do it?
As per my comment above, stop overthinking:
php > $arr = array(array('c', 'b', 'a'), array('r', 'q','p'));
php > var_dump($arr);
array(2) {
[0]=>
array(3) {
[0]=>
string(1) "c"
[1]=>
string(1) "b"
[2]=>
string(1) "a"
}
[1]=>
array(3) {
[0]=>
string(1) "r"
[1]=>
string(1) "q"
[2]=>
string(1) "p"
}
}
php > sort($arr[1]);
php > var_dump($arr);
array(2) {
[0]=>
array(3) {
[0]=>
string(1) "c"
[1]=>
string(1) "b"
[2]=>
string(1) "a"
}
[1]=>
array(3) {
[0]=>
string(1) "p"
[1]=>
string(1) "q"
[2]=>
string(1) "r"
}
}
Note how the r/q/p array has now become p/q/r - it's been sorted, even though it's an array nested in another array.

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