I am using Laravel 10 with Livewire v3. I have a method that accepts array as one of the parameter. If the parameter is just an array then the method will behave differently, if the parameter is associative array then the method will use the keys and values both and behave differently. The passed associative array can have sequential keys as shown in the code below.
Below is the extracted logic and I don't want it to detect $associative_sequential
array as a sequential array. What do I need to add/change for that to happen because I don't want $sequential
array to be identified as associative array either.
public function doSomething($passedArray) {
if (is_array($passedArray)) {
if ($passedArray == array_values($passedArray)) {
echo "The passed array is a sequential array.\n";
} elseif (array_keys($passedArray) == array_filter(array_keys($passedArray), 'is_int')) {
echo "The passed array is an associative array with integer keys.\n";
} else {
echo "The passed array is an associative array with non-integer keys.\n";
}
} else {
echo "The passed variable is not an array.\n";
}
}
$sequential = array("apple", "banana", "cherry");
$associative_sequential = array(0 => "apple", 1 => "banana", 2 => "cherry");
$associative_non_sequential = array(10 => "apple", 20 => "banana", 25 => "cherry");
$strict_associative = array("fruit1" => "apple", "fruit2" => "banana", "fruit3" => "cherry");
doSomething($sequential);
doSomething($associative_sequential);
doSomething($associative_non_sequential);
doSomething($strict_associative);
Outputs:
// This is what I am getting
The passed array is a sequential array.
The passed array is a sequential array.
The passed array is an associative array with integer keys.
The passed array is an associative array with non-integer keys.
// This is what I want to happen
The passed array is a sequential array.
The passed array is an associative array with integer keys.
The passed array is an associative array with integer keys.
The passed array is an associative array with non-integer keys.
PHP's "array" type is slightly unusual in its flexibility. Depending on your point of view, it's either very powerful, or very muddled.
Every array in PHP tracks, among other things:
This is easiest to see when you explicitly list both keys and values. The following are all different array values:
$a = [0 => 'a', 1 => 'b'];
$b = [1 => 'a', 2 => 'b']; // same value, same order, but different keys
$c = [1 => 'a', 0 => 'b']; // same keys and values, in a different order
If you don't specify the key explicitly, PHP automatically assigns the next unused integer. This is most obvious when appending items to a list:
$a = [];
$a[] = 'a';
$a[] = 'b';
var_export($a);
// array ( 0 => 'a', 1 => 'b', )
The first element in the empty list was given key 0
, but if there were existing items, PHP would start at the highest:
$a = [42 => 'answer'];
$a[] = 'a';
var_export($a);
// array ( 42 => 'answer', 43 => 'a', )
$a = [-100 => 'answer'];
$a[] = 'a';
var_export($a);
// array ( -100 => 'answer', -99 => 'a', )
$a = [5 => 'five', 3 => 'three'];
$a[] = 'a';
var_export($a);
// array ( 5 => 'five', 3 => 'three', 6 => 'a', )
The same thing happens when you write an array literal without keys:
$a = ['a', 'b'];
var_export($a);
// array ( 0 => 'a', 1 => 'b', )
You can even specify some keys, and let PHP choose others:
$a = [0 => 'a', 'b'];
var_export($a);
// array ( 0 => 'a', 1 => 'b', )
$a = ['a', 1 => 'b'];
var_export($a);
// array ( 0 => 'a', 1 => 'b', )
So, in short, your function is working correctly: array("apple", "banana", "cherry")
and array(0 => "apple", 1 => "banana", 2 => "cherry")
are just different ways of writing the same thing.