array_flip : How to use with examples

What is PHP array_flip? Description PHP array_flip exchanges array keys with their values. Function Signature array_flip(array $array): array Argument $array - Input array Return Type This function returns the flipped array. Notes The values must be either int or string types; otherwise, the function raises a warning. The function excludes such a key/value pair from the flipped array. A recurring value will get the latest key, and the other pairs will be discarded. Introduction - PHP array_flip PHP array_flip swaps the keys and values of an array. The keys in the associative arrays are always unique, while values can repeat. Secondly, associative array keys are either string or int types. So, the array_flip discards a value and its key if the value type is other than string or int.  As the keys are supposed to be unique, the function assigns the latest key as a value to the recurring values…

read more

array_slice: How to use with examples

What is PHP array_slice? Description PHP array_slice extracts a slice of the array. Function Signature array_slice( array $array, int $offset, ?int $length = null, bool $preserve_keys = false): array Arguments $array - Input array.$offset - Position in the array.Positive offset, the slicing will start at the offset in the array.Negative offset, the slicing will start that far from the end of the array.$lengthOmitted length: The slice will include everything from the offset to the end of the array.Positive length: The slice will include up to that many elements.Negative length: The slice will exclude up to that many elements from the array’s end.$preserve key - If true, the slice retains the original numeric index in the slice Return Type The function returns the slice or an empty array if the offset is larger than the array’s length Notes Only the available elements will be returned if the $length is greater than the array’s length.The…

read more

array_splice: How to use with examples

array_splice: How to use with examples
What is array_splice? Description PHP array_splice removes a portion of an array and replaces it with something else. Function Signature array_splice( array &$array, int $offset, ?int $length = null, mixed $replacement = [] ) : array Arguments $array - Input array $offset Positive offset: The replacement will start from the start of the array.Negative offset: The replacement will start from the end of the array. $lengthOmitted length: It removes everything from offset to the end of the array.Positive length: It removes that many elements will be removed.Negative length: It removes that many elements from the end of the array.Zero length: No elements will be removed. $replacementIf specified, it replaces the removed elements with the replacement array.If nothing is removed, the replacement elements are inserted at the offset. Return Type Returns the removed/replaced array. Notes Keys in the array and replacement are not preserved. If the replacement is not an array, it will…

read more

array_chunk: How to use with examples

What is PHP array_chunk? Description PHP array_chunk function splits an array into chunks. Function Signature array_chunk(array $array, int $length, bool $preserve_keys = false): array Arguments $array - The input array$length - The size of each chunk$preserve_keys - Array keys will be preserved if it is true. Return Types This function returns a multidimensional array with a numeric index, each numeric index containing a chunk.  Warnings/Errors If the $length is less than 1, the function throws E_Warning and returns null. Note If the $array could not be partitioned into even chunks, the last chunk will have less than the $length number of elements. Introduction - PHP array_chunk PHP array_chunk function takes an array and splits it into chunks based on the $length argument. Each chunk has the $length count except for the last chunk which could have less than the $length number of elements if the array could not be partitioned…

read more

PHP array_values: how to use in PHP with examples

PHP array_values: Detailed Guide Description - PHP array_values PHP array_values function returns all the values of an array. Function Signature array_values(array $array): array Arguments array - The input array Return Type - PHP array_value The function returns an array of values. Note PHP array_values function indexes the resulting array numerically. So, it doesn’t preserve the actual key values. Introduction PHP has a wide range of functions, including those dealing with the PHP associative array. PHP associative array is a hash map like data structure, keeping key and value pairs. The array is indexed on these keys to fetch the values. PHP gives us two important functions to separate the keys and values of an array. The array_keys function in PHP returns all the keys of an associative array. A counterpart of this function is array_values, except that it returns all the values of an array. In this article, we’ll see…

read more

PHP array_keys: How to use with examples

PHP array_keys: Detailed Guide Description - PHP array_keys PHP array_keys function returns all the keys or subset of keys of an array. Function Signature array_keys(array $array): array array_keys(array $array, mixed $search_value, bool $strict = false): array Arguments array - The input array search_value - If specified, keys corresponding to the matching values are returned strict - If true, the function performs a strict comparison Return Type - PHP array_keys The function returns an array containing all the keys. Introduction PHP array_keys function is a convenient function for isolating the keys of a PHP associative array. This function is usually coupled with other PHP functions like the array_search to overcome inherent drawbacks in these functions, especially if a function doesn’t keep a reference for the keys of an array. Besides, the array_keys function returns a subset of keys for one or more values matching the search_value argument. This article explores the…

read more

PHP array_search: How to use in PHP with examples

PHP array_search : Detailed Guide Description - PHP array_search PHP array_search searches an array for a given value and returns the first matching key if successful. Function Signature array_search(mixed $needle, array $haystack, bool $strict = false): int|string|false Arguments needle - The value to be searchedhaystack - The input arraystrict - If this parameter is set to true, then the function performs strict type comparison Notes If strict is set to true, then the function matches identical elements. Identical elements have the same value and datatype.If there is more than one match, the function returns the key of the first matching value only. Return Type - PHP array_search This function returns the matched key for the needle, else return false. Warning The function may return a falsey value instead of false. A falsey value is a non-boolean value that evaluates to false. It is safer to use the === operator for…

read more

PHP array_map: How to use in PHP with examples

What is PHP array_map? Description PHP array_map function applies the callback to the elements of the given arrays. Function Signature array_map(?callable $callback, array $array, array ...$arrays): array  Arguments callback - A callable (function) to apply to the elements of the given arrays. array - An array to run through the callable. arrays - An optional variable list of arrays to run through the callable.  Note The count of the number of arrays passed as arguments and the parameters of the callback function should match. Excess arrays are ignored. The function preserves the keys if and only if one array is passed. In case of more than one array,  the returned array has sequential integer keys. Return Type The function returns an array derived as a result of applying the callback to the input arrays. Errors/Exceptions The function throws ArgumentCountError if an insufficient number of arguments is provided. Introduction Just as…

read more

PHP array_merge_recursive: How to use in PHP with examples

What is PHP array_merge_recursive? Description PHP array_merge_recursive merges one or more arrays recursively. Function Signature array_merge_recursive(array ...$arrays): array Arguments $arrays - One or more arrays to be merged Return Type This function merges the $arrays passed in the argument and returns the merged array. If there is no argument, it returns an empty array. Introduction - PHP array_merge_recursive PHP array_merge_recursive is a robust function capable of merging arrays recursively. The recursive merge creates an array for similar keys rather than overwriting, a feature that sets it apart for array_merge and makes it favorable than it.  The array_merge_recursive doesn’t overwrite values of similar keys. Instead, it creates an array and appends the values to that array. That’s why you do not lose any data to overwriting. We will review an example from the array_merge article and see the differences in the output. So, in this article, we will explore array_merge_recursive in…

read more

PHP array_combine: how to use in PHP with examples

PHP array_combine
What is PHP array_combine? Description PHP array_combine function creates an array by combining two arrays, one for key and another for values. Function Signature array_combine(array $keys, array $values): array Arguments $keys - An array of keys$values - An array of values Return Type The function returns the combined array or false if the number of elements in $keys and $values are not equal. Introduction - PHP array_combine PHP array_combine is apparently a straightforward function. As the name suggests, it combines two arrays. However, the way it combines the two arrays is different than what we have seen while learning the PHP array_merge function. There the function merged the two arrays by joining the values while catering for similar keys using varying logic. The array_combine function takes two arrays, one for keys and another for values. So, it takes creates an associative array using keys from the first array argument and…

read more

Page 1 of 2
1 2