How to Sort multidimensional array by value in php - php

Hi I need to Sort the multi dimensional array by its value My array is like this I need to group the array by its position to create a new array sorting is just confusing How do i create a sorted array from this
Array
(
[prodata] => Array
(
[4] => Array
(
[position] => 3
[products] => Array
(
[0] => 227
)
)
[3] => Array
(
[position] => 4
[products] => Array
(
[0] => 441
[1] => 54
)
)
)
[richtext] => Array
(
[1] => Array
(
[position] => 1
[header] => qwewqe
[content] => contentadaqsdas
)
)
)
I this array i need to sort by it position and create a new array,
Goal
Array
(
[0]=>Array(
[richtext] => Array
(
[position] => 1
[header] => qwewqe
[content] => contentadaqsdas
)
)
[1]=>Array(
[prodata] => Array
(
[position] => 3
[products] => Array
(
[0] => 227
)
)
)
[2]=>Array(
[prodata] => Array
(
[position] => 4
[products] => Array
(
[0] => 441
[1] => 54
)
)
)
)
How do I achieve this help

You can flat the multi dimension array first, then sort it. Demo
$flat_array = [];
foreach($multi_dimension_array as $k => $array){
foreach($array as $value){
$flat_array[] = array(
$k => $value
);
}
}
usort($flat_array,function($a,$b){return current($a)["position"] - current($b)["position"];});
print_r($flat_array);
Though this code works, But I suggest restruct the data source for better data format. Which can make it a easy work

Related

PHP: Add a dimension to an array using array_map?

I have an array and I want to add a dimension. I want to group it using the position column in the array
$view_rows = [];
Array
(
[row] => Array
(
[0] => Array
(
[position] => 1
[image_id] => 2809
[basename] => rj5ed2c90f609423.26673093.jpg
)
[1] => Array
(
[position] => 1
[image_id] => 2808
[basename] => rj5ed2c8eccc9c06.91700011.jpg
)
[2] => Array
(
[position] => 2
[image_id] => 2807
[basename] => rj5ed2c77ef32b76.96579137.jpg
)
)
)
After adding the dimension (group by location), it should look like this:
Array
(
[rows] => Array
(
[1] => Array
(
[0] => Array
(
[position] => 1
[image_id] => 2809
[basename] => rj5ed2c90f609423.26673093.jpg
)
[1] => Array
(
[position] => 1
[image_id] => 2808
[basename] => rj5ed2c8eccc9c06.91700011.jpg
)
)
[2] => Array
(
[0] => Array
(
[position] => 2
[image_id] => 2807
[basename] => rj5ed2c77ef32b76.96579137.jpg
)
)
)
)
Is there a PHP function to do this? If not, I will use the good old looping method.
I did some research here ( Add Dimension in PHP array ), and I believe it can be achieve using array_map.
I've tried it, with no success (I'm close)
$new_array = array_map(function($x) {
return [$x[0]['position'] => $x];
}, $view_rows);
Since you need to change the structure of the array, you can't use array_map, but you can use array_reduce to get the result you want:
$data['rows'] = array_reduce($data['rows'], function ($c, $a) {
$c[$a['position']][] = $a;
return $c;
}, array());
Output:
Array
(
[rows] => Array
(
[1] => Array
(
[0] => Array
(
[position] => 1
[image_id] => 2809
[basename] => rj5ed2c90f609423.26673093.jpg
)
[1] => Array
(
[position] => 1
[image_id] => 2808
[basename] => rj5ed2c8eccc9c06.91700011.jpg
)
)
[2] => Array
(
[0] => Array
(
[position] => 2
[image_id] => 2807
[basename] => rj5ed2c77ef32b76.96579137.jpg
)
)
)
)
Demo on 3v4l.org
#Nick answer works but the same thing can be achieved using a simple foreach loop easily so everyone can understand it.
$result = [];
foreach($data['rows'] as $row) {
$result[$row['position']][] = $row;
}

How to merge array by key values which are repeating

I have an array like this what i want is to sub array converts into unique key sub array with multiple unique keys. I have tried array_merge_recursive function which is not working because i have multiple arrays in loop and the function works with 2 or 3 arrays.
Array
(
[0] => Array
(
[fromWho] => abc#email.se
)
[1] => Array
(
[tid] => 2013-05-05 23:35
)
[2] => Array
(
[answer] => Company
)
[3] => Array
(
[name] => message
)
[4] => Array
(
[label] => Message
)
[5] => Array
(
[fromWho] => bcd#email.se
)
[6] => Array
(
[tid] => 2013-05-05 23:35
)
[7] => Array
(
[answer] => Star Company
)
[8] => Array
(
[name] => name
)
[9] => Array
(
[label] => Name
)
)
I want the array like this array keys order doesn't matter:
Array
(
[0] => Array
(
[label] => Message
[name] => message
[answer] => Company
[tid] => 2013-05-05 23:35
[fromWho] => abc#email.se
)
[0] => Array
(
[label] => Name
[name] => name
[answer] => Star Company
[tid] => 2013-05-05 23:35
[fromWho] => bcd#email.se
)
)
Use array_chunk() to group the array into chunks of 5 elements. Then you can convert each chunk into an associative array.
$chunks = array_chunk($data, 5);
$result = array_map(function($chunk) {
$arr = [];
foreach ($chunk as $entry) {
foreach ($entry as $key => $val) {
$arr[$key] = $val;
}
}
return $arr;
}, $chunks);

how to create multiple new arrays from existing multi dimensional array

I have the below multi dimensional array i want to make a new array from taking each array's first element and make one array than after second element of each array and make a new array, i did tried for loop and foreach but i can't able to make it help me to solve this please, Thank you in advance.
the below is my array
Array
(
[0] => Array
(
[0] => Array
(
[y] => 40
[label] => SCIENCE
)
[1] => Array
(
[y] => 100
[label] => SCIENCE
)
[2] => Array
(
[y] => 89
[label] => SCIENCE
)
)
[1] => Array
(
[0] => Array
(
[y] => 72
[label] => MATHEMATICS
)
[1] => Array
(
[y] => 20
[label] => MATHEMATICS
)
[2] => Array
(
[y] => 70
[label] => MATHEMATICS
)
[3] => Array
(
[y] => 85
[label] => MATHEMATICS
)
)
[2] => Array
(
[0] => Array
(
[y] => 66
[label] => ENGLISH
)
)
)
I want my first new array be like
[0] => Array
(
[0] => Array
(
[y] => 40
[label] => SCIENCE
)
[1] => Array
(
[y] => 72
[label] => MATHEMATICS
)
[2] => Array
(
[y] => 66
[label] => ENGLISH
)
)
My second new array be like
[1] => Array
(
[0] => Array
(
[y] => 100
[label] => SCIENCE
)
[1] => Array
(
[y] => 20
[label] => MATHEMATICS
)
)
Foreach loop the array with $key and you will be able to get the other items easily with array_column.
foreach($arr[0] as $key => $sub){
$new[$key] = array_column($arr, $key);
}
Now $new will have three items (the same as first subarray).
To fix what you asked for I have to loop and find the max count of the arrays.
$max =0;
foreach($arr as $sub){
$max = max($max, count($sub));
}
for($i=0;$i<$max;$i++){
$new[$i] = array_column($arr, $i);
}
You can transpose an array, remove empty entries and take needed row. In the case your result will be in $res[0] and $res[1] arrays
$res = array_map('array_filter', array_map(null, ...$a));
demo

Filter multidimensional php array with another array

I have an array $result as such:
[0] => Array (
[0] => Array (
[itemid] => 1
[name] => A
)
[1] => Array (
[itemid] => 2
[name] => B
)
)
[1] => Array (
[0] => Array (
[itemid] => 3
[name] => C
)
[1] => Array (
[itemid] => 2
[name] => B
)
)
and an array $items as such:
[0] => Array (
[itemid] => 2
[name] => B
)
[1] => Array (
[itemid] => 4
[name] => D
)
How do I remove all items from the $result array, that occur in the $items array? In this case, the $result would become:
[0] => Array (
[0] => Array (
[itemid] => 1
[name] => A
)
)
[1] => Array (
[0] => Array (
[itemid] => 3
[name] => C
)
)
Since the question is mostly code, here's some extra characters to make StackOverflow accept the question.
I think this is what you want. (Not tested yet)
<?php
foreach ($result as $key => $array) {
$result[$key] = array_diff($array, $items);
}
print_r($result);

PHP array slice multidimensional array

I have an multidimensional array but need to make it smaller.
This is an easy question I believe. I need to remove 1 array in
jsonresult array, the first one but preserve the other in the next array. I have tried array_splice but it only keeps one.
Array
(
[searchword] => search word
[jsonresult] => Array
(
[0] => Array // THIS ONE, KEEP ITS CHILDREN MOVE UP
(
[0] => Array
(
[id] => 14889770
)
[1] => Array
(
[id] => 14389720
)
[2] => Array
(
[id] => 14869723
)
)
[1] => Array // THIS ONE, KEEP ITS CHILDREN MOVE UP
(
[0] => Array
(
[id] => 14889722
)
[1] => Array
(
[id] => 14389711
)
[2] => Array
(
[id] => 14869329
)
)
)
)
Would like to get:
Array
(
[searchword] => search word
[jsonresult] => Array
(
[0] => Array
(
[id] => 14889770
)
[1] => Array
(
[id] => 14389720
)
[2] => Array
(
[id] => 14869723
)
[3] => Array
(
[id] => 14889722
)
[4] => Array
(
[id] => 14389711
)
[5] => Array
(
[id] => 14869329
)
)
)
Try this code. This may not be the correct method but it gives what you need. (As I understand from your question)
//creating a sample array similar to one you given in question.
$arr_test['searchword'] = 'search word';
$arr_test['jsonresult'] = array(array(array('id'=>14889770),array('id'=>14889720)),array(array('id'=>14889780),array('id'=>14889790)));
//creating new array
$arr_new = array();
//formatting array as you needed it
foreach($arr_test['jsonresult'] as $arr_jsonresult){
foreach($arr_jsonresult as $jsonresult){
$arr_new['jsonresult'][] = $jsonresult;
}
}
//overwriting the specific array key
$arr_test['jsonresult'] = $arr_new['jsonresult'];
//checking output
echo '<pre>';
print_r($arr_test);
This code produces the following output
Array
(
[searchword] => search word
[jsonresult] => Array
(
[0] => Array
(
[id] => 14889770
)
[1] => Array
(
[id] => 14889720
)
[2] => Array
(
[id] => 14889780
)
[3] => Array
(
[id] => 14889790
)
)
)

Categories