This question already has answers here:
PHP: Sort an array by the length of its values?
(12 answers)
Closed 5 years ago.
I have an array like the following:
$a= $array('PHP','HTML','JS','LARAVEL');
I want to sort the elements in the array, descending by the total number of characters of an element
$b= $array('LARAVEL','HTML','PHP','JS');
Please help me to descend the elements of the array, based on the number of characters in an array.
I see you have the Laravel tag, so you can use Laravel collections with the sortByDesc function for that.
$array = collect(['PHP','HTML','JS','LARAVEL'])->sortByDesc(function($value) {
return strlen($value);
});
You need to use usort():
function sort($a,$b){
return strlen($b)-strlen($a);
}
$array = ['PHP','HTML','JS','LARAVEL'];
usort($array,'sort');
Related
This question already has answers here:
Sort array not working
(2 answers)
Closed 10 months ago.
I'm trying to sort an array numerically. Here's my code
<?php
$data = '9#Saul,7#Jesse,1#Skyler,6#Walter';
$exp = explode(",",$data);
$expsort = sort($exp);
print_r($expsort);
?>
But it is not working. The output is showing only "1".
You are assigning the value of the sort function -which sorts the argument array itself, and it always returns true and thus you got 1 as a result.
So if you print your original exploded array, it will be sorted. Please note, sort overrides your original array
$data = '9#Saul,7#Jesse,1#Skyler,6#Walter';
$exp = explode(",",$data);
sort($exp);
print_r($exp);
This question already has answers here:
How to filter an array by a condition
(9 answers)
get specific column with specific condition from multidimensional array using array function only in php
(4 answers)
Closed 3 years ago.
I have a multi-column PHP array as
Array (
Array ('col1', 'col2', 'col3'),
);
How can I get an array of col2 if col1 is greater than a specific value?
I can do it by a foreach loop, but I think it should be possible with a native function like array_filter.
Simply put, you're better with a foreach loop, as you're not going to have to make multiple iterations of the array. You want to both filter and modify the array, which requires using both array_filter() and array_map() in order to get what you want:
<?php
$arr = [
[5, 'col2-1', 'col3'],
[10, 'col2-2', 'col3']
];
$res = array_filter($arr, function($item) {
return $item[0] > 7;
});
$res = array_map(function($item) {
return $item[1];
}, $res);
var_dump($res);
Test here: https://3v4l.org/HRTOJ
This question already has answers here:
Search for highest key/index in an array
(7 answers)
Closed 4 years ago.
Say I have the following array:
$n[5] = "hello";
$n[10]= "goodbye";`
I would like to find out the highest index of this array.
In Javascript, I could do $n.length - 1 which would return 10.
In PHP, though, count($n) returns 2, which is the number of elements in the array.
So how to get the highest index of the array?
Use max() and array_keys()
echo max(array_keys($n));
Output:-https://eval.in/997652
$n = [];
$n[5] = "hello";
$n[10]= "goodbye";
// get the list of key
$keyList = array_keys($n);
// get the biggest key
$maxIndex = max($keyList);
echo $n[$maxIndex];
output
goodbye
This question already has answers here:
How to remove duplicate values from a multi-dimensional array in PHP
(18 answers)
Closed 5 years ago.
"2017-08-31":["5948a0dd21146a43fdcfef5a","5948a0dd21146a43fdcfef5a"]
"2017-08-22":["5948a0dd21146a43fdcfef5a"]
"2017-08-09":["59461ceae6179b19403c6a19","59461ceae6179b19403c6a19"]
"2017-08-08":["59461ceae6179b19403c6a19","59461ceae6179b19403c6a19"]
I have an array like this, key is a date and multiple value associated with that date, but I need unique value with that key.how to do that ?
I've tried with array_unique but no luck!
Just use array_map and array_unique together.
<?php
$a = json_decode('{"2017-08-31":["5948a0dd21146a43fdcfef5a","5948a0dd21146a43fdcfef5a"],
"2017-08-22":["5948a0dd21146a43fdcfef5a"],
"2017-08-09":["59461ceae6179b19403c6a19","59461ceae6179b19403c6a19"],
"2017-08-08":["59461ceae6179b19403c6a19","59461ceae6179b19403c6a19"]}', true);
echo json_encode(array_map("array_unique", $a));
$array = your array
$array = array_map(function($val){return array_unique($val);}, $array);
You better do this using SQL if it's possible. Otherwise, you can try this:
$array = array_unique($array, SORT_REGULAR);
This question already has answers here:
Applying a function for each value of an array
(5 answers)
Closed 6 years ago.
Is there util that parses array content without the need of iterating it and parsing each value?
input: ['2','3','7']
output: [2, 3, 7]
This obviously iterates internally, but you don't have to code an iterator:
$output = array_map('intval', $input);
Maps every value in $input to the intval() function and returns the result. For things that cannot be converted into an integer you'll get 0 or for objects, a notice and that value will not be returned.
I can't tell if you want to remove 0 values or not from your comment, but if so:
$output = array_filter(array_map('intval', $input));
You can use array_map
array = ["2","3","4"];
$intArray = array_map('intval', $array);