php multi-dimensional array sorting with case insensitive - php

I have an array as below..
Array
(
[0] => Array
(
[0] => Array
(
[name] => Henry
)
)
[1] => Array
(
[0] => Array
(
[name] => jack
)
)
[2] => Array
(
[0] => Array
(
[name] => Albert
)
)
[3] => Array
(
[0] => Array
(
[name] => bunny
)
)
I need to sort this array by name in asc & desc order of case insensitive. Please help me..

I wrote a php function a few years ago which does such a thing.
function subval_sort($a, $subkey, $reverse = false)
{
if (!$a)
{
return array();
}
$b = array();
foreach ($a as $k => $v)
{
$b[$k] = strtolower($v[$subkey]);
}
if ($reverse)
{
arsort($b);
}
else
{
asort($b);
}
$c = array();
foreach ($b as $key => $val)
{
$c[] = $a[$key];
}
return $c;
}
Use it like subval_sort($array, 'name')

sorting function :
Use my custom function to achieve your solution it is working
function multisort (&$array, $key) {
$valsort=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
$valsort[$ii]=$va[$key];
}
asort($valsort);
foreach ($valsort as $ii => $va) {
$ret[$ii]=$array[$ii];
}
$array=$ret;
}
multisort($multiarr,"order");
Hope this will sure help you.

$array = array(
array('Henry'),
array('Michael'),
array('Steve'),
array('Daniel'),
array('Albert'),
);
// Comparison function
function cmp($a, $b) {
if ($a[0] == $b[0]) {
return 0;
}
return ($a[0] < $b[0]) ? -1 : 1;
}
// Array to be sorted
print_r($array);
// Sort and print the resulting array
uasort($array, 'cmp');
print_r($array);
Output:
Array
(
[0] => Array
(
[0] => Henry
)
[1] => Array
(
[0] => Michael
)
[2] => Array
(
[0] => Steve
)
[3] => Array
(
[0] => Daniel
)
[4] => Array
(
[0] => Albert
)
)
Array
(
[4] => Array
(
[0] => Albert
)
[3] => Array
(
[0] => Daniel
)
[0] => Array
(
[0] => Henry
)
[1] => Array
(
[0] => Michael
)
[2] => Array
(
[0] => Steve
)
)

Related

PHP array sort by number of items

I need to sort my array-based on the number of elements in the inner array.
It's my current array.
Array
(
[0] => Array
(
)
[1] => Array
(
[0] => 2-3
)
[2] => Array
(
[0] => 5-9
)
[3] => Array
(
[0] => 5-9
[1] => 2-3
)
)
I need to sort the array by this format.
Array
(
[3] => Array
(
[0] => 5-9
[1] => 2-3
)
[2] => Array
(
[0] => 5-9
)
[1] => Array
(
[0] => 2-3
)
[0] => Array
(
)
)
This seems to do it:
<?php
$a = [
[], ['2-3'], ['5-9'], ['5-9', '2-3']
];
$f = fn ($b, $c) => count($c) <=> count($b);
usort($a, $f);
print_r($a);
https://php.net/function.usort
i hope this answer help you
$result = array();
$sorter = array();
foreach($values as $key => $vals){
$sorter[$key]= count($vals);
}
arsort($sorter);
foreach ($sorter as $ii => $va) {
$result[$ii]=$values[$ii];
}
output

Sort a 3 level multi-dimension array by value in PHP

Here is the array,
array(
[0] => Array
(
[IdRedeemProduct] => Item-A
[RedeemOptions] => Array
(
[0] => Array
(
[Points] => 1000
)
[1] => Array
(
[Points] => 2000
)
[2] => Array
(
[Points] => 43000
)
)
[ProductType] => 1
)
[1] => Array
(
[IdRedeemProduct] => Item-B
[RedeemOptions] => Array
(
[0] => Array
(
[Points] => 6200
)
[1] => Array
(
[Points] => 53000
)
)
[ProductType] => 1
)
)
most of the usort examples are just 2 level dimension array. I couldn't find any example for 3 level.
In this case i wanted to sort the smallest points to show first. Item-A will be the first and Item-B will be the 2nd.
foreach ($filteredResults as $key => $row)
{
foreach ($row['RedeemOptions'] as $key2 => $option) {
$vc_array_name[$key] = $option['Points'];
}
}
array_multisort($vc_array_name, SORT_ASC, $filteredResults);
this is working...
Try this:
function sort_2d_desc($array, $key) {
usort($array, function($a, $b) use ($key) {
return strnatcasecmp($b[$key], $a[$key]);
});
return $array;
}
$a = [];
foreach($arr as $key => $val){
$a[$key] = $this->sort_2d_desc($val['RedeemOptions'], 'Points');
}
$newArr = [];
foreach($arr as $key => $val){
$newArr[] = ['IdRedeemProduct' => $val['IdRedeemProduct'], 'RedeemOptions' => $a, 'ProductType' => $val['ProductType']];
}
print_r($newArr);

Sort multidimensional array by value unique array keys

I want to sort Multi dimensional array based up on the value, please check following array,
Array
(
[1] => Array
(
[70000] => Aceh
)
[2] => Array
(
[70024] => Sumatera Utara
)
[3] => Array
(
[70058] => Barat
)
[4] => Array
(
[70078] => Riau
)
[5] => Array
(
[70091] => Jambi
)
)
I want it to be like this after sort, please check below array.
Array
(
[1] => Array
(
[70000] => Aceh
)
[2] => Array
(
[70024] => Barat
)
[3] => Array
(
[70058] => Jambi
)
[4] => Array
(
[70078] => Riau
)
[5] => Array
(
[70091] => Sumatera Utara
)
)
Can any one help me with the good solution please. Thank you!.
Very strange output that you're after. Use something like this:
function weirdSort($array) {
$out = [];
$keys = [];
$values = [];
foreach($array as $k => $v) {
$values[] = $v;
$keys[] = $k;
}
usort($values);
foreach($keys as $i => $key) {
$out[] = [$key => $values[$i];
}
return $out;
}
I don't know this way is good or bad but I got wanted output by usort
function cmp($a, $b) {
if ($a[key($a)] == $b[key($b)]) return 0;
return ($a[key($a)] > $b[key($b)]) ? 1 : -1;
}
usort( $levelOneArray, 'cmp' );
print_r($levelOneArray);

how to intersect 2 multidimensional array into third multimensional array

i have 2 arryas and i want them to intersect and store the finding matches into third array with values from first array and second array.
the first array looks like this:
Array
(
[0] => Array
(
[0] => 45
[1] => 10640
[2] => 1041-0567041700116
)
[1] => Array
(
[0] => 46
[1] => 10640
[2] => 1041-0567041700318
)
[2] => Array
(
[0] => 207
[1] => 10645
[2] => 03320103000052
)
and the second array:
Array
(
[0] => Array
(
[0] => 03320103000052
[1] => 0
)
[1] => Array
(
[0] => 10013800805001
[1] => 12
)
[2] => Array
(
[0] => 1090-0360141758201
[1] => 3
)
the out put should be:
Array
(
[0] => Array
(
[0] => 207 =>value from first array
[1] => 10645 =>value from first array
[2] => 03320103000052 =>value from first and second array (this is what i need to compare)
[3] => 0 =>value from second array
)
this is similar to this post
but i have problems to store data into multidimensional array
thanks in forward for any suggestions and help
You can do this with only two foreach loops and one if statement:
$combined = array();
foreach ($array1 as $a) {
foreach ($array2 as $b) {
if ($a[2] == $b[0]) {
$combined[] = array($a[0], $a[1], $a[2], $b[1]);
}
}
}
The following is the test I set up to try this:
<?php
$array1 = array();
$array1[] = array('45', '10640', '1041-0567041700116');
$array1[] = array('46', '10640', '1041-0567041700318');
$array1[] = array('207', '10645', '03320103000052');
$array2 = array();
$array2[] = array('03320103000052', '0');
$array2[] = array('10013800805001', '12');
$array2[] = array('1090-0360141758201', '3');
$combined = array();
foreach ($array1 as $a) {
foreach ($array2 as $b) {
if ($a[2] == $b[0]) {
$combined[] = array($a[0], $a[1], $a[2], $b[1]);
}
}
}
print_r($combined);
?>

Arrange PHP Multidimensional Array By Inner Index

How to arrange this array by last inner index ( 0, 1, 2 ) and get the value of the last inner index as the value of each second index:
Array
(
[text] => Array
(
[grid] => Array
(
[0] => 3
[1] => 4
[2] => 5
)
[image] => Array
(
[0] =>
[1] =>
[2] =>
)
[align] => Array
(
[0] => left
[1] => right
[2] => left
)
[title] => Array
(
[0] =>
[1] =>
[2] =>
)
[content] => Array
(
[0] =>
[1] =>
[2] =>
)
)
)
And have the results as below:
Array
(
[text] => Array
(
[0] => Array
(
[grid] => 3
[image] =>
[align] => left
[title] =>
[content] =>
)
[1] => Array
(
[grid] => 4
[image] =>
[align] => right
[title] =>
[content] =>
)
[2] => Array
(
[grid] => 5
[image] =>
[align] => left
[title] =>
[content] =>
)
)
)
This will do the work
function restructure($arr){
$newArr = array();
foreach($arr as $k => $v){
foreach($v as $k1 => $v1){
foreach($v1 as $k2 => $v2){
$newArr[$k][$k2][$k1] = $v2;
}
}
}
return $newArr;
}
As SiGanteng suggested, i dont see other ways than a for/foreach loop:
function buildArray($source, $key = false)
{
// Build the new array
$new_array = array();
// Define groups
$groups = $key === false ? array_keys($source) : array($key);
foreach($groups AS $group)
{
// Get the keys
$keys = array_keys($array[$group]);
// Count the values
$num_entries = count($array[$group][$keys[0]]);
for($i = 0; $i < $num_entries; $i++)
{
foreach($keys AS $key)
{
$new_array[$group][$i][$key] = $array[$group][$key][$i];
}
}
}
return $new_array;
}
This allow you to define the key you need to build; If not specified, the function build the array for every key.
This should work.
function formatit($arr) {
$new = array();
foreach($arr as $k=>$v) {
foreach($v as $k1=>$v1) {
$new[$k1][$k] = $v1;
}
}
return $new;
}
Tested. Call it as
$arr['text'] = formatit($arr['text']);
http://ideone.com/rPzuR

Categories