How to get Keys from Array in PHP Laravel?
Few days ago, I was working on my new Laravel 5.4 application and i require to get keys name from my array. i was thing how we can get it and laravel provide us for to do it. Finally i found array helper function in Laravel docs. There are several array helper function available for array.
I found array_keys() helper for getting keys name from our array. array_keys() will return array of keys. It could be easy to work with keys name in array. So in this example i want to share with you small example using array_keys() helper.
I created one route "myarraykeys" with my $myArray variable, and in this route i used array_keys() helper and get keys array.
Example:
Route::get('myarraykeys', function () {
$myArray = [
'name'=>'Hardik Savani',
'email'=>'itsolutionstuff@gmail.com',
'website'=>'itsolutionstuff.com'
];
$myArrayKey = array_keys($myArray);
dd($myArrayKey);
});
Output:
Array
(
[0] => name
[1] => email
[2] => website
)
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Array Length Validation Example
- How to Check Query Execution Time in Laravel?
- Laravel Password and Confirm Password Validation Example
- How to Get Current Date Time and Day in Laravel?
- Laravel Remove All Spaces from String Example
- Laravel withSum() with Where Condition Example
- Laravel withCount() with Where Condition Example
- Laravel Ajax Request with Validation Example
- AngularJS Convert Comma Separated String to Array Example
- How to Remove Duplicate Values from Array in PHP?
- How to Remove Key from Array in Laravel?
- Laravel Create Custom Error Page Example
- How to Get Last Element of Array in Javascript?
- How to Remove Null Values from Array in PHP?