
Remove array keys and values if it does not exist in other array in Laravel
Laravel provides an array helper method to get the keys and values from the array which is specified by another array. In this way you can remove array keys and values if it does not exist in another array.
-
Laravel array_only helper method
$allArrayItems = array('175' => 20, '179' => 30, '181' => 80); $arrayItemsToGet = array( '0' => 175, '1' => 179); $arrayItems = array_only($allArrayItems, $arrayItemsToGet); print_r($arrayItems); //Another method: //Use can pass keys as index key (second parameter values) $allArrayItems = array('175' => 20, '179' => 30, '181' => 80); $arrayItemsToGet = array('175', '179'); $arrayItems = array_only($allArrayItems, $arrayItemsToGet); print_r($arrayItems);
0//Output
Array passed as first argument: Array ( [175] => 20 [179] => 30 [181] => 80 )
Array passed as second argument: Array ( [0] => 175 [1] => 179 )
Output return by function: Array ( [175] => 20 [179] => 30 )Method array_only() is the helper method in Laravel which returns only the specified key-value pair from the array.
The array_only() method will remove all other keys and values from the array (from the first argument) if it does not exist in another array using keys which is passed as a second argument to this method. You have to pass the keys in array form as the second argument which you want to get from the existing array.
Random Code Snippet Queries: Laravel
- Undefined property: stdClass::$title
- Automatically remove records using Prunable trait in Laravel
- Convert multidimensional array to single array in Laravel
- Trying to get property 'title' of non-object
- How to customize pagination view in laravel
- Trying to access array offset on value of type null error in laravel
- How to display pivot table column value in laravel
- How to add dynamic page title in Laravel view
- Laravel 9 pagination with search filter
- Call to undefined function Illuminate\Encryption\openssl_cipher_iv_length()
- Conditional validation in laravel
- Credit card validation in laravel
- How to return error message from controller to view in laravel
- Get domain name in laravel
- Permanently delete a record in laravel
- How to add a key value pair to existing array in laravel
- How to get random string in Laravel
- Method Illuminate\Http\Request::validated does not exist
- 419 page expired error in Laravel
- Touch parent updated_at in Laravel
- How to decrypt laravel password
- Pagination in laravel
- How to display 1 day ago in comments in laravel view
- How to check column value of a record is null or not in laravel
- How to print form data in laravel