Sort multidimensional array in PHP using foreach - php

For example i have an array named $slice like this :
Array
(
[0] => Array
(
[0] => 12
[1] => 4
[2] => 2
[3] => 8
[4] => 20
)
[1] => Array
(
[0] => 9
[1] => 7
[2] => 1
[3] => 10
[4] => 23
)
)
I want to sort array above so the output will be like this :
Array
(
[0] => Array
(
[0] => 2
[1] => 4
[2] => 8
[3] => 12
[4] => 20
)
[1] => Array
(
[0] => 1
[1] => 7
[2] => 9
[3] => 10
[4] => 23
)
)
Then i tried to use foreach and array_multisort, and when i use print_r the result is 1 for each col :
foreach ($slice1 as $col) {
$slicesort[] = array_multisort($col);
}
output :
Array
(
[0] => 1
[1] => 1
)

array_multisort sorts the array in place, it does not return the sorted array. You need to use it like this:
foreach ($slice1 as $col) {
array_multisort($col);
$slicesort[] = $col;
}
Having said this, array_multisort is somewhat overkill here, and I'm not sure that you really need to create a copy of the array. This will do just fine:
foreach ($slice1 as &$col) {
sort($col);
}
This applies sort to each array within $slice1 by reference, and thereby orders $slice1 in place.

PHP array_multisort, as per the documentation, is for sorting multiple or multi-dimensional arrays, in your case you don't really need it.
In your case you just need sort, you can find the documentation here
$slicesort = array();
foreach ($slice1 as $col) {
sort($col);
$slicesort[] = $col;
}

$slice = array(
array(12,4,8,2,10),
array(9,7,1,10,13)
);
foreach ($slice as &$arr) {
sort($arr);
}
print_r($slice);

array_multisort return a boolean value, true for success, false otherwise.
Change your code this way:
foreach ($slice1 as $col) {
if (array_multisort($col)) {
$slicesort[] = $col;
}
}

Related

Convert Multidimensional array to single array in php [duplicate]

This question already has answers here:
Convert multidimensional array into single array [duplicate]
(24 answers)
Closed 5 years ago.
It's probably beginner question but I'm going through documentation for
longer time already and I can't find any solution and i have an array which
is multidimensional given below format.
/* This is how my array is currently */
Array
(
[0] => Array
(
[0] => Array
(
[title] => a
[title_num] =>1
[status] => 1
)
[1] => Array
(
[title] => Mr
[title_num] => 82
[status] => 1
)
)
[1] => Array
(
[0] => Array
(
[title] => b
[title_num] =>25
[status] => 2
)
[1] => Array
(
[title] => c
[title_num] =>45
[status] => 2
)
)
)
I want to convert this array into this form
/*Now, I want to simply it down to this*/
Array
(
[0] => Array
(
[title] => a
[title_num] =>1
[status] => 1
)
[1] => Array
(
[title] => Mr
[title_num] => 82
[status] => 1
)
[2] => Array
(
[title] => b
[title_num] =>25
[status] => 2
)
[3] => Array
(
[title] => c
[title_num] =>45
[status] => 2
)
)
I have tried array_flatten,array_map PHP built in function
A link or anything to point me in the right direction will be highly
appreciated
Here is another trick to solve your problem,
function custom_filter($array) {
$temp = [];
array_walk($array, function($item,$key) use (&$temp){
foreach($item as $value)
$temp[] = $value;
});
return $temp;
}
array_walk — Apply a user supplied function to every member of an array
Here is working demo.
here you go
$result = [];
foreach($array as $arr)
{
$result = array_merge($result , $arr);
}
var_dump($result);
Like this:
$i=0;
foreach ($array as $n1) {
foreach ($n1 as $n2) {
$newArr[$i]['title']=$n2['title'];
$newArr[$i]['title_num']=$n2['title_num'];
$newArr[$i]['status']=$n2['status'];
}
$i++;
}
Or simpler as suggested in the comments
for ($i=0; $i < count($array); $i++) {
foreach ($array[$i] as $n2) {
$newArr[$i]['title']=$n2['title'];
$newArr[$i]['title_num']=$n2['title_num'];
$newArr[$i]['status']=$n2['status'];
}
}

Using array_flip() to return an array

I have an array:
$input = array(1,2,3,4,6,5,3,6)
and I want the key/value pairs to be flipped.
This can be done by using the array_flip() function.
$flipped = array_flip($input)
If in the original array has 2 or more same values (in this case number 6)how can I return it in an array?
array= ([1]=0,[2]=>1,[4]=>2,[6]=>array(3,6),[5]=>4,[3]=>5)
I tried to use array_count_values() but can't figure out how to do it?
You cannot do that using the array_flip() function. Probably you look for something like that:
<?php
function array_flip_and_collect($input) {
$output = [];
foreach ($input as $key=>$val) {
$output[$val][] = $key;
}
return $output;
}
$input = array(1,2,3,4,6,5,3,6);
print_r(array_flip_and_collect($input));
The output:
Array
(
[1] => Array
(
[0] => 0
)
[2] => Array
(
[0] => 1
)
[3] => Array
(
[0] => 2
[1] => 6
)
[4] => Array
(
[0] => 3
)
[6] => Array
(
[0] => 4
[1] => 7
)
[5] => Array
(
[0] => 5
)
)
Note that the output differs slightly from what you suggested in your question. That is by purpose because this appears more logical to me. If you really want that keys with only one element really are scalars and not arrays with one element, then you have to add an additional conversion step to the code.

PHP: put array entries in new order

I try to take the following array:
Array
(
[0] => Array
(
[1] => 12
[2] => 21
[3] => 33
)
[1] => Array
(
[1] => 5
[2] => 5
[3] => 4
)
[2] => Array
(
[1] => 1
[2] => 10
[3] => 11
)
[3] => Array
(
[3] => 2
)
)
and create a new array with it, taking the first entries of the source array. So the first array from this one would look like this:
Array
(
[0] => 12
[1] => 5
[2] => 1
[3] => 2
)
And the second one obviously like this:
Array
(
[0] => 21
[1] => 5
[2] => 10
)
I tried around with 2 for loops, it somewhat works with
for ($i = 1; $i < count($month_array[$i]) + 1; $i++)
{
unset($temp_array);
for ($i2 = 0; $i2 < count($month_array); $i2++)
{
if (isset($month_array[$i2][$i]))
{
$temp_array[] = $month_array[$i2][$i];
}
}
}
But it leaves out some of the elements and if the source array is not complete, meaning only the 3rd array has a value for key 3, it also fails.
Any help ? Many thanks !
Try it like this:
$org_array = array(array(1 => 12,2 => 21,3 => 33),array(1 => 5,2 => 5,3 => 4),array(1 => 1,2 => 10,3 => 11),array(3 => 2),); // your original array
$new_array = array(); // the output array
foreach($org_array as $sub_org_value) { // for each sub array of your original array do this
foreach($sub_org_value as $key => $value) { // for each entry in a sub array do this
$new_array[$key][] = $value; // add the entry into the new_array in the right sub array
}
}
print_r($new_array[1]); // yields 12,5,1
print_r($new_array[2]); // yields 21,5,10
print_r($new_array[3]); // yields 33,4,11,2
Afterwards you have an array $new_array that contains three arrays. The first sub array has all 1 values, the second sub array has all 2 values and the third sub array has all 3 values.

PHP three dimensional array, reset key if second element different

Let say i have this print_r output, this is dynamic and not same each condition
Array
(
[2] => Array
(
[1] => 24
[2] => 23,25
)
[3] => Array
(
[3] => 27
[4] => 27,26
[5] => 28,27,26
)
)
As you can see, array element [3] starts from [3][4][5], how do it make it start from [1][2]...[n] if the 2nd element is not same.
Ideally what i am looking for is something like
Array
(
[2] => Array
(
[1] => 24
[2] => 23,25
)
[3] => Array
(
[1] => 27
[2] => 27,26
[3] => 28,27,26
)
)
How do i achieve that? Thanks
array_values returns the values of an array with new numeric indices:
foreach($a as $k => $v) {
$a[$k] = array_values($v);
}
Add conditions if you only want to re-index some of your sub-arrays.
Functional approach:
$a = array_map(function($v) {
return array_values($v);
}, $a);

Changing values of all similar sub arrays in PHP

I've got the following 2D array, stored inside the array variable $my_array
Array
(
[0] => Array
(
[0] => 3
[1] => 6
[2] => 3
)
[1] => Array
(
[0] => 3
[1] => 6
[2] => 3
)
[2] => Array
(
[0] => 3
[1] => 6
[2] => 3
)
)
I wanted to decrement all the [1] sub array values by 3. Tried the following code, with no success.
$my_array[$i]['1']=($my_array[$i]['1'])-3;
print_r($my_array);
Ideas?
foreach ($my_array as &$val) {
$val[1] -= 3
}
Something like this is what you're after.
foreach($my_array as $k=>$v){
if (isset($my_array[$k][1]) && is_numeric($my_array[$k][1])){
$my_array[$k][1] -= 3;
}
}

Categories