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.
['apple', 'banana', 'cherry']and[0 => 'apple', 1 => 'banana', 2 => 'cherry']are both sequential (indexed) arrays, and are actually identical (if you store them in variables and compare them, you'd see they are equivalent,$sequential == $associative_sequentialistrue). If you omit the0 => ..., it still uses those indices, you just don't seem them until you access/view the array. And for accessing the first element of an array, its$arr[0]. It's only when you have non-sequential, non-numeric, etc. keys that you have a non-sequential (associative) array. Your output is correct.$associative_sequential = array(1 => "apple", 2 => "banana", 3 => "cherry");(I think)array(0 => "apple", 1 => "banana", 2 => "cherry")andarray(1 => "apple", 2 => "banana", 3 => "cherry")are different type of array in the sense that one is sequential by implementation and other associative as it does not start with 0. I assume in second array$arr[0]will not work, is that the case ?0, 1, 2, ..., Infinity. An Associative Array can have any other keys; sequential (as long as it doesn't start from 0, or skips indices) like1, 2, 4, 5, 9, ...or0, 2, 4, ..., etc, reversed or non-sequential, like2, 1, 0or2, 0, 1, etc., non-numeric, likea, b, c, etc.[0, 1, 2]and[0 => 0, 1 => 1, 2 => 2]are equivalent, one simply omits the keys since they are the defaults. And yes,$arr[0]will fail for[1 => 'apple', 2 => 'banana', 3 => 'cherry'], since there is no0key.