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);
Related
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);
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
)
)
Array
(
[0] => Array
(
[ADADCD] =>
)
[1] => Array
(
[ADADCD] => ?
)
[2] => Array
(
[ADADCD] => HOSP1
)
[3] => Array
(
[ADADCD] => HOSP2
)
[4] => Array
(
[ADADCD] => H1
)
)
We have an array like this ,I want to search a specific value like HOSP2,What is the process to get the value at index.
Just try with array_search
$key = array_search(array('ADADCD' => 'HOSP1'), $inputArray);
Loop trough the array and return the index on which you find the value you are looking for.
$searchIndex = -1;
foreach ( $yourArray as $k => $v ) {
if ( $v['ADADCD'] == 'search value' ) {
$searchIndex = $k;
break;
}
}
You can use a combination of foreach() and in_array().
So, first looping through all the indices of the array using foreach().
foreach ($array as $key => $subarray)
if (in_array($string, $subarray))
return $key;
So, now for an array like this:
Array
(
[0] => Array
(
[ADADCD] =>
)
[1] => Array
(
[ADADCD] => ?
)
[2] => Array
(
[ADADCD] => HOSP1
)
[3] => Array
(
[ADADCD] => HOSP2
)
[4] => Array
(
[ADADCD] => H1
)
)
Output
2
Fiddle: http://phpfiddle.org/main/code/t6b-g9r
<?php
$array = your array;
$key = array_search('HOSP2', $array);
echo $key;
?>
Output: ADADCD
http://php.net/manual/en/function.array-search.php
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
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;
}