Multidimensional array sorting by value in php - php

I have this multidimensional result array in php. I want to sort this array by the values of [name] without using foreach loop. Plz help me.
Array has to sory by [name]. Thanks in advance.
Array
(
[result] => Array
(
[projects] => Array
(
[0] => Array
(
[name] => Project-3
[releases] => Array
(
[0] => Array
(
[id] => 752676125
)
)
)
[1] => Array
(
[name] => Project-1
[releases] => Array
(
[0] => Array
(
[id] => 752676126
)
)
)
[2] => Array
(
[name] => Project-2
[releases] => Array
(
[0] => Array
(
[id] => 752676127
)
)
)
)
)
)

First of all extract $mult_arry['result']['projects'] and run the sorting function as follows.
<?php
function msort($array, $key, $sort_flags = SORT_REGULAR) {
if (is_array($array) && count($array) > 0) {
if (!empty($key)) {
$mapping = array();
foreach ($array as $k => $v) {
$sort_key = '';
if (!is_array($key)) {
$sort_key = $v[$key];
} else {
// #TODO This should be fixed, now it will be sorted as string
foreach ($key as $key_key) {
$sort_key .= $v[$key_key];
}
$sort_flags = SORT_STRING;
}
$mapping[$k] = $sort_key;
}
asort($mapping, $sort_flags);
$sorted = array();
foreach ($mapping as $k => $v) {
$sorted[] = $array[$k];
}
return $sorted;
}
}
return $array;
}
$mult_arry=array('result'=>array('projects'=>array(
array('name'=>'Project-3','releases'=>array(array('id' => 752676125))),
array('name'=>'Project-1','releases'=>array(array('id' => 752676126))),
array('name'=>'Project-2','releases'=>array(array('id' => 752676127)))
)));
$mult_arry_extracted=$mult_arry['result']['projects'];
echo "<pre>";
print_r($mult_arry_extracted);
$mult_arry_sorted_byname = msort($mult_arry_extracted, array('name'));
print_r($mult_arry_sorted_byname);
echo "</pre>";
?>
More information here

Related

2 dimensional array to sort using PHP

MY CODE IS :
$arr = array(
array(
"title" => "Volvo"
),
array(
"title" => "BMW"
),
array(
"title" => "Saab"
),
array(
"title" => "Aam"
)
);
$a = aasort($arr,"title");
echo "<pre>",print_r($a),"</pre>";
function aasort (&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
$sorter[$ii]=$va[$key];
}
natcasesort($sorter);
foreach ($sorter as $ii => $va) {
$ret[$ii]=$array[$ii];
}
$array=$ret;
return $array;
}
and the output is :
Array
(
[3] => Array
(
[title] => Aam
)
[1] => Array
(
[title] => BMW
)
[2] => Array
(
[title] => Saab
)
[0] => Array
(
[title] => Volvo
)
)
1
But i want output like :
Array
(
[0] => Array
(
[title] => Aam
)
[1] => Array
(
[title] => BMW
)
[2] => Array
(
[title] => Saab
)
[3] => Array
(
[title] => Volvo
)
)
1
How to do it ?
You can reindex an array with array_values():
$array = array_values($ret);
Try this it will help you :
$result = array();
$result = sortArray($array,'key');
Here, $array is unsorted array. So you can sort it by call sortArray function.
<?php
function sortArray($arrData, $p_sort_field, $p_sort_type = false )
{
if(!empty($arrData))
{
foreach($arrData as $data)
{
$newData [] = $data;
}
for($i=0; $i<count($newData); $i++)
{
$ar_sort_field[$i]=$newData[$i][$p_sort_field];
}
array_multisort($ar_sort_field, ($p_sort_type ? SORT_DESC : SORT_ASC), $newData);
return $newData;
}
}
?>
Try this..
$arr = array(
array(
"title" => "Volvo"
),
array(
"title" => "BMW"
),
array(
"title" => "Saab"
),
array(
"title" => "Aam"
)
);
$arr = msort($arr, array('title'));
print_r($arr);
function msort($array, $key, $sort_flags = SORT_REGULAR) {
if (is_array($array) && count($array) > 0) {
if (!empty($key)) {
$mapping = array();
foreach ($array as $k => $v) {
$sort_key = '';
if (!is_array($key)) {
$sort_key = $v[$key];
} else {
// #TODO This should be fixed, now it will be sorted as string
foreach ($key as $key_key) {
$sort_key .= $v[$key_key];
}
$sort_flags = SORT_STRING;
}
$mapping[$k] = $sort_key;
}
asort($mapping, $sort_flags);
$sorted = array();
foreach ($mapping as $k => $v) {
$sorted[] = $array[$k];
}
return $sorted;
}
}
return $array;
}
Result
Array ( [0] => Array ( [title] => Aam ) [1] => Array ( [title] => BMW )
[2] => Array ( [title] => Saab ) [3] => Array ( [title] => Volvo ) )`

How to sum array values based on keys?

The first array
Array
(
[0] => Array
(
[1] => 2
)
[1] => Array
(
[1] => 2
)
[2] => Array
(
[2] => 1
)
[3] => Array
(
[3] => 1
)
)
I want output like
Array
(
[0] => Array
(
[1] => 4
)
[1] => Array
(
[2] => 1
)
[2] => Array
(
[3] => 1
)
)
How can i do this?
Seems like a good case for array_reduce():
$res = array_chunk(array_reduce($arr, function(&$current, $item) {
foreach ($item as $key => $value) {
if (!isset($current[$key])) {
$current[$key] = 0;
}
$current[$key] += $value;
}
return $current;
}, []), 1, true);
For the final result I'm using array_chunk(); it takes an array and creates single element sub arrays of each element.
$result = array();
foreach ($input as $subarray) {
foreach ($subarray as $key => $value) {
if (isset($result[$key])) {
$result[$key][$key] += $value;
} else {
$result[$key] = array($key => $value);
}
}
}
$result = array_values($result); // Convert from associative array to indexed array

php multi-dimensional array sorting with case insensitive

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
)
)

Recursive PHP function that copies multidimensional array but replaces empty values

I have a multidimensional array that could be any size or depth. I'm basically trying replace empty values with a value but only in certain cases. Here is an example of the array, it's quite large but I want to illustrate my point well:
[field_ter] =>
[field_title] => Array
(
[0] => Array
(
[value] =>
)
)
[field_firstnames] => Array
(
[0] => Array
(
[value] => test9
)
)
[field_birth] => Array
(
[0] => Array
(
[value] =>
)
)
[field_postal] => Array
(
[0] => Array
(
[value] =>
)
)
[group_certificates] => Array
(
[0] => Array
(
[_delta] => 0
[field_cert_details] => Array
(
[value] =>
)
[field_cert_issuedate] => Array
(
[value] => Array
(
[date] =>
)
)
[field_cert_expiry] => Array
(
[value] => Array
(
[date] =>
)
)
[field_cert_issue_country] => Array
(
[value] =>
)
[field_cert_limits] => Array
(
[value] =>
)
)
[1] => Array
(
[_delta] => 1
[field_cert_details] => Array
(
[value] =>
)
[field_cert_issuedate] => Array
(
[value] => Array
(
[date] =>
)
)
[field_cert_expiry] => Array
(
[value] => Array
(
[date] =>
)
)
[field_cert_issue_country] => Array
(
[value] =>
)
[field_cert_limits] => Array
(
[value] =>
)
)
)
What I'm trying to do is find any element in the array that is empty, then replace the empty value with a value. I have an array of exceptions where the empty element is not replaced. This is the function I'm working on at the moment:
function check_empty(&$array) {
$exceptions = array('changed', 'form_build_id','date', 'status', 'op');
// This is the array we will return at the end of the function
$new_array = array();
foreach($array as $key => $value) {
if(is_array($value)) {
check_empty($value);
}
elseif ($value == '' && !in_array($key, $exceptions)) {
$new_array[$key] = '$$$';
}
else {
$new_array[$key] = $value;
}
}
return $new_array;
}
Could someone help me tweak my function or point me in the direction of a better way? I tried array_walk_recursive but it doesn't work with my arrays.
You need to assign the return value of the recursive check_empty:
function check_empty($array) {
$exceptions = array('changed', 'form_build_id','date', 'status', 'op');
// This is the array we will return at the end of the function
$new_array = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$new_array[$key] = check_empty($value);
}
elseif ($value == '' && !in_array($key, $exceptions)) {
$new_array[$key] = '$$$';
}
else {
$new_array[$key] = $value;
}
}
return $new_array;
}
if(is_array($value)) {
check_empty($value);
}
Here you don't return resulting value to anywhere. Probably that's the problem

extract values from multidemensional array

I have two arrays that are structured like this
$array1 = Array
(
[0] => Array
(
['story_id'] => 47789
)
[1] => Array
(
['story_id'] => 47779
)
[2] => Array
(
['story_id'] => 47776
)
[3] => Array
(
['story_id'] => 47773
)
[4] => Array
(
['story_id'] => 47763
)
)
$array2 = Array
(
[0] => Array
(
['story_id'] => 47789
)
[1] => Array
(
['story_id'] => 47777
)
[2] => Array
(
['story_id'] => 47776
)
[3] => Array
(
['story_id'] => 47773
)
[4] => Array
(
['story_id'] => 47763
)
)
and I want to get the difference of array1 from array2 so I tried using
$results = array_diff($array1, $array2);
but it turns up empty is there any easy way around this or would it be best for me to get the arrays boiled down and if so is there easy way to do that ?
It because that the array_diff is only use for 1 dimension array. For your 2 array, let use some code from php.net
function multidimensional_array_diff($a1, $a2)
{
$r = array();
foreach ($a2 as $key => $second) {
foreach ($a1 as $key => $first) {
if (isset($a2[$key])) {
foreach ($first as $first_value) {
foreach ($second as $second_value) {
if ($first_value == $second_value) {
$true = true;
break;
}
}
if (!isset($true)) {
$r[$key][] = $first_value;
}
unset($true);
}
} else {
$r[$key] = $first;
}
}
}
return $r;
}

Categories