Associative array sort numeric - arrays

I have an associative array in following format.I need to sort the array in descending order and sort the array with same value as ascending order.
$numArray = array();
$numArray[0]['Word'] = 'One';
$numArray[0]['Number'] = 10.00;
$numArray[1]['Word'] = 'Two';
$numArray[1]['Number'] = 10.00;
$numArray[2]['Word'] = 'Three';
$numArray[2]['Number'] = 10.00;
$numArray[3]['Word'] = 'Four';
$numArray[3]['Number'] = 30.00;
$numArray[4]['Word'] = 'Five';
$numArray[4]['Number'] = 20.00;
$numArray[5]['Word'] = 'Six';
$numArray[5]['Number'] = 10.00;
$numArray[6]['Word'] = 'Seven';
$numArray[6]['Number'] = 20.00;
$numArray[7]['Word'] = 'Eight';
$numArray[7]['Number'] = 20.00;
Please help me to sort the array in following order.
Array
(
[0] => Array
(
[Word] => Four
[Number] => 30
)
[1] => Array
(
[Word] => Five
[Number] => 20
)
[2] => Array
(
[Word] => Seven
[Number] => 20
)
[3] => Array
(
[Word] => Eight
[Number] => 20
)
[4] => Array
(
[Word] => One
[Number] => 10
)
[5] => Array
(
[Word] => Two
[Number] => 10
)
[6] => Array
(
[Word] => Three
[Number] => 10
)
[7] => Array
(
[Word] => Six
[Number] => 10
)
)

foreach ($numArray as $key => $row) {
$array[$key] = $row['Number'];
}
array_multisort($array, SORT_DESC, $numArray);
This little php code will at least sort your array desc according to the Number.
guess thats half of what you want .

Related

How to remove first dimension of an array without loosing keys?

How can i remove the first dimansion of a multidemensional Array without loosing the keys?
i have an Array that have multiple arrays inside
the firstkey is a Date and the secondkey is the hour.
my Output is:
Array
(
[0] => Array
(
[firstkey] => Array
(
[secondkey] => Array
(
[0] => 7
[1] => 8
)
)
)
[1] => Array
(
[firstkey] => Array
(
[secondkey] => Array
(
[0] => 7
[1] => 8
)
)
)
)
and i want this:
Array
(
[firstkey] => Array
(
[secondkey] => Array
(
[0] => x
[1] => y
[2] => z
[3] => r
)
)
)
i also tried array_merge_recursive() but instead of putting the values to the secondkey it creates a new array with an incremental key
okay found a solution on:
PHP : multidimensional array merge recursive
function array_merge_recursive_ex(array $array1, array $array2)
{
$merged = $array1;
foreach ($array2 as $key => & $value) {
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
$merged[$key] = array_merge_recursive_ex($merged[$key], $value);
} else if (is_numeric($key)) {
if (!in_array($value, $merged)) {
$merged[] = $value;
}
} else {
$merged[$key] = $value;
}
}
return $merged;
}
and with this function i can merge that mutlidemensional array without loosing keys:
$newarray =[];
foreach($array as $firstkey){
$newarray = array_merge_recursive_ex($newarray, $firstkey);
}

Outer Array Index

Array looks like below :
Array
(
[0] => Array
(
[memberid] => 5203
)
[1] => Array
(
[memberid] => 494
)
[2] => Array
(
[memberid] => 1053
)
[3] => Array
(
[memberid] => 1081
)
)
How can i find-out the outer array index (such as 0,1,2) using inner array.
You can iterate the outer array, and save the key if the current inner array value matches like this:
$outerKey = null;
foreach($outerArray as $key => $member){
if($member['memberid'] == 1081){
$outerKey = $key;
break;
}
}
echo $outerKey; // it will print 3

Array reduce find the small distance and return id

I use google matrix for calculate the best distance with the multiple address.
I want display the id of a small distance
My array return by the API
$com = Array(
[0] => Array (
[id] => 12
[km] => 833 km
)
[1] => Array (
[id] => 4
[km] => 546km
)
[1] => Array (
[id] => 45
[km] => 1200km
)
)
I want display the ID of small distance ( for this exemple the id is "4")
I have tried this :
$min = array_reduce($commercant,
function ($min, $item) {
if ($item['km'] < $min['km']) {
return $item;
}
return $min;
},
array('id' => -1, 'km' => PHP_INT_MAX));
echo $min['id'];
This code doesn't work and i don't know why !
if you have an idea....
Thx
use this code
usort($com, function($a, $b) {
return $a['km'] - $b['km'];
});
echo !empty($com[0]['id']) ? $com[0]['id'] : "";
it will sort your array in desc order of distance, from this sorted array you can pick first element of array.
Hope this will help you!
With ordinary sorting, you can achieve result.
http://php.net/manual/en/function.usort.php
$x = [
['id' => 12, 'km' => '833km'],
['id' => 4, 'km' => '546km'],
['id' => 45, 'km' => '1200km']
];
function cmp_by_kms($arr1, $arr2) {
$km1 = intval($arr1['km']);
$km2 = intval($arr2['km']);
if ($km1 == $km2) { return 0; }
return ($km1<$km2) ? -1 : 1;
}
usort($x, "cmp_by_kms");
print_r($x[0]);
#=> Array
(
[id] => 4
[km] => 546km
)

Convert 3 dimension to 2 dimension

I got a query result like this : -
Array(
[0] => Array
(
[0] => Array
(
[id] => 1
[name] => Japan
)
)
[1] => Array
(
[0] => Array
(
[id] => 2
[name] => Nepal
)
)
}
How can I convert it to
Array
(
[0] => Japan
[1] => Nepal
)
The query used to get first array is 'select id , name from country;'
Please help me.
foreach ($element as $array) {
$element = $element[0]["name"];
}
Or,
function extractName($element) {
return $element[0]["name"];
}
$newArray = array_map("extractName", $array);
Extract!
$newArray = Set::extract($oldArray, '{n}.0.name');
More here.

MIN() in an array

So here is my array, what I want to do is unset the [detail][$x] keys leaving just the lowest total. Can anyone assist? Thank you in advance..
There is alot of Products to loop though, what I mean is that its not just one item in the array.
[1] => Array
(
[name] => Product Name 1
[detail] => Array
(
[1] => Array
(
[total] => 10.14
)
[2] => Array
(
[total] => 12.18
)
[3] => Array
(
[total] => 9.90
)
You can find out the lowest total and overwrite the whole detail. Something like that:
$lowestValue = false;
foreach ($array[1]['detail'] as $detail) {
if ($lowestValue === false || $lowestValue > $detail['total']) {
$lowestValue = $detail['total'];
}
}
$array[1]['detail'] = array(0 => array('total' => $lowestValue));

Resources