Get value of previous array key [duplicate] - php

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

Related

Array is not getting sorted with sort() function PHP [duplicate]

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

Laravel WhereIn array returns only first index results [duplicate]

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 10 months ago.
$dean_ids = Auth::user()->dean_id; // "9,11"
$subjects = Subject::whereIn('dean_id', [$dean_ids])->select('id')->get();
returns only data for "9" but when i trying like this:
$subjects = Subject::whereIn('dean_id', [9,11])->select('id')->get();
//it returns all data that what i want.
how can i fix it?
As I see, this line $dean_ids = Auth::user()->dean_id; returns a comma-separated string. So when you make $dean_ids array by using [$dean_ids] it actually makes an array like:
array(
'9,11'
)
Instead of
array(
9,
11
)
There is only one value inside the array. So what you can do just use explode for splitting the string by using a comma and it also returns an array.
You can try this:
$subjects = Subject::whereIn('dean_id', explode(',', $dean_ids))->select('id')->get();

PHP: Find highest index of numeric array that has missing elements [duplicate]

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

Easy way to parse array values [duplicate]

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

PHP sort not working [duplicate]

This question already has answers here:
Sort array not working
(2 answers)
Closed 10 months ago.
I'm trying to sort a string alphabetically. I thought I could explode a string into an array and sort it, but the echo is returning nothing.
$schools = "high*low*other*";
$schools = explode("*", $schools);
$schools = sort($schools);
echo $schools[0];
sort() sorts in place (i.e. modifies the array itself A.K.A. the $schools variable is passed by reference) so no array is returned. A boolean value is however returned to determine if the sort was successful.
bool sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
$schools = "high*low*other*";
$schools = explode("*", $schools);
sort($schools);
echo $schools[0];
Your problem can be solved by the following code example:
<?php
$schools = "c*d*a";
$alpha_sorted_array = explode("*", $schools);
sort($alpha_sorted_array);
foreach($alpha_sorted_array as $itemToPrint){
echo("Item: $itemToPrint\n");
}
?>
Basically, you reasign what happens when you use the explode function on the string variable held inside schools, as you know, explode uses the first delimiter, in this case * found inside the initial string $schools to return an array. From this point you can call the sort function on the new array and it will return a sorted array(no resasinging needed for this one, just call sort() on it)
The given output is:
Item: a
Item: c
Item: d
The solution is natcasesort
This function sorts values with natural algorithm and case-insensitively.

Categories