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
Related
This question already has answers here:
Selecting every nth item from an array
(9 answers)
Remove every 3rd and 4th element from array in php
(1 answer)
How to access N-th element of an array in PHP
(3 answers)
Remove every nth item from array
(6 answers)
foreach step 5 steps forward in an array
(3 answers)
Closed 4 months ago.
i have a array like this
$names = [a,b,c,d,e,f,g,h,i,j,k,l,m];
what i wan to remove
$remove = "b,c,e,f,h,i,k,l";
then i need a new array from the remaining elements like below
$new_arr = [a,d,g,j,m];
Use array chunk to split by 3 and take out first element.
<?php
$names = [a,b,c,d,e,f,g,h,i,j,k,l,m];
$chunked =array_chunk($names, 3);
$filtered = [];
foreach($chunked as $chunk){
$filtered[] = $chunk[0];
}
var_dump($filtered);
?>
Instead of removing number 2 and number 3 from each 3 elements, the task is simply written like this:
Keep the first of every 3 items.
This can be determined using the index and the modulo operator %.
Using array_filter saves the foreach loop. This allows the solution to be implemented as a one-liner.
$names = ['a','b','c','d','e','f','g','h','i','j','k','l','m'];
$result = array_filter($names,fn($k) => $k%3 == 0, ARRAY_FILTER_USE_KEY );
var_dump($result);
Demo: https://3v4l.org/sKTSQ
The $names array needs to be noted as in the code above. A notation without quotes like in the question produces error messages.
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:
PHP get previous array element knowing current array key
(10 answers)
Closed 4 years ago.
So I tried doing this :
$array = array('first'=>'111', 'second'=>'222', 'third'=>'333');
var_dump(prev($array["second"]));
Hoping to get 111 but I get NULL. Why?
$array["second"] returns 222. Shouldn't we get 111 when we use PHP's prev() function?
How to get the previous value of an array if it exists using the key?
Your current value from $array["second"] is not an array and prev takes an array as a parameter.
You have to move the internal pointer of the $array and then get the previous value.
$array = array('first'=>'111', 'second'=>'222', 'third'=>'333');
while (key($array) !== "second") next($array);
var_dump(prev($array));
prev function expects an array as an argument but you're passing a string.
$array["second"] evaluates to '222'
In this case, you point directly the previous value, without iterating the array.
$array = array('first'=>'111', 'second'=>'222', 'third'=>'333');
$keys = array_flip(array_keys($array));
$values = array_values($array);
var_dump($values[$keys['second']-1]);
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');
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);