{"id":7041,"date":"2023-09-25T11:12:51","date_gmt":"2023-09-25T11:12:51","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=7041"},"modified":"2023-11-21T06:35:45","modified_gmt":"2023-11-21T06:35:45","slug":"laravel-array-helpers","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/laravel-array-helpers\/","title":{"rendered":"Laravel Array Helpers"},"content":{"rendered":"<p>Elegant, straightforward, and expressive are all characteristics of Laravel. Laravel&#8217;s helper functions are among its most effective features. The array helpers in Laravel are a crucial tool for developers dealing with data arrays.<br \/>\nThe helper methods for Laravel arrays that every developer should be familiar with are covered in this post. Working with arrays is made easier and more efficient by these helpers. We&#8217;ll go through join(), keyBy(), get(), first(), last(), and pluck ().<\/p>\n<h2>Array Join<\/h2>\n<p>You may be asking yourself, &#8220;Why do I need this helper when I can just use join() or implode()?&#8221;<br \/>\n<code>use Illuminate\\Support\\Arr;<br \/>\n$stack = ['Tailwind', 'Alpine', 'Laravel', 'Livewire'];<br \/>\nArr::join($stack, ', ');<br \/>\n\/\/ Tailwind, Alpine, Laravel, Livewire<br \/>\nimplode($stack, ', ');<br \/>\n\/\/ Tailwind, Alpine, Laravel, Livewire<\/code><\/p>\n<p>The above method is exactly the same, so it&#8217;s up to you to decide which method you prefer. When you wish the last value to use a different joining string, the join() helper is beneficial:<br \/>\n<code>use Illuminate\\Support\\Arr;<br \/>\n$stack = ['Tailwind', 'Alpine', 'Laravel', 'Livewire'];<br \/>\nArr::join($stack, ', ', ', and');<br \/>\n\/\/ Tailwind, Alpine, Laravel, and Livewire<\/code><\/p>\n<p><strong>Keyed Array data<\/strong><br \/>\nYou may take an array of data (for example, many products) and key the data by a certain product attribute. This allows you to easily focus data on a certain key. You could have typed something like this, establishing a new variable and inserting data into it using a keyed value:<br \/>\n<code>$array = [<br \/>\n['product_id' =&gt; 'prod-100', 'name' =&gt; 'Desk'],<br \/>\n['product_id' =&gt; 'prod-200', 'name' =&gt; 'Chair'],<br \/>\n];<br \/>\n$keyed = [];<br \/>\nforeach ($array as $value) {<br \/>\n$keyed[$value['product_id']] = $value;<br \/>\n}<br \/>\n\/*<br \/>\n[<br \/>\n'prod-100' =&gt; ['product_id' =&gt; 'prod-100', 'name' =&gt; 'Desk'],<br \/>\n'prod-200' =&gt; ['product_id' =&gt; 'prod-200', 'name' =&gt; 'Chair'],<br \/>\n]\n*\/<\/code><\/p>\n<p>The following can be done with just one line of code by using the Arr::keyBy() method:<br \/>\n<code>$keyed = Arr::keyBy($array, 'product_id');<\/code><\/p>\n<h3>Checking and Getting Data from an Array<\/h3>\n<p>The Arr::get() function is straightforward to use, but it has a strong &#8220;dot notation&#8221; that you may use to easily access nested data:<br \/>\n<code>use Illuminate\\Support\\Arr;<br \/>\n$data = [<br \/>\n'products' =&gt; [<br \/>\n'desk' =&gt; [<br \/>\n'name' =&gt; 'Oakendesk'<br \/>\n'price' =&gt; 599.00,<br \/>\n'description' =&gt; 'Solid oak desk built from scratch.'<br \/>\n],<br \/>\n],<br \/>\n];<br \/>\n\/\/ 599.00<br \/>\nArr::get($data, 'products.desk.price');<br \/>\n\/\/ Returns false<br \/>\nArr::has($data, 'products.desk.discount');<br \/>\n\/\/ Returns null<br \/>\nArr::get($data, 'products.desk.discount');<br \/>\n\/\/ Returns custom default value if not found.<br \/>\nArr::get($data, 'products.desk.discount', ['type' =&gt; 'percent', 'value' =&gt; 10]);<\/code><\/p>\n<p><strong>Getting the first or last element in an array<\/strong><br \/>\nWhen you have an array and wish to obtain the last element, use PHP&#8217;s end() function:<br \/>\n<code>$array = [100, 200, 300, 110];<br \/>\nend($array);<\/code><\/p>\n<p>If your array is empty, though, you will obtain false:<br \/>\n<code>$array = [];<br \/>\nend($array); \/\/ false<\/code><\/p>\n<p>When an array is empty, you have many alternatives using Laravel&#8217;s last() helper:<br \/>\n<code>use Illuminate\\Support\\Arr;<br \/>\n$array = [];<br \/>\nArr::last($array); \/\/ null<br \/>\n\/\/ Provide a default<br \/>\nArr::last($array, null, 100); \/\/ 100<\/code><\/p>\n<p>Using Laravel&#8217;s helper also enables you to pass a closure as a second argument as the condition for which element to return first or last respectively:<br \/>\n<code>$array = [100, 200, 300, 110];<br \/>\nArr::last($array, fn ($e) =&gt; $e &gt; 110); \/\/ 300<br \/>\nArr::first($array, fn ($e) =&gt; $e &gt; 110); \/\/ 200<\/code><\/p>\n<p>Straightforward yet powerful API for accessing the first or final member in an array of data.<\/p>\n<p><strong>Data Extraction from an Array<\/strong><br \/>\nOccasionally you need to extract one scalar item of data from a collection of data (for example, emails from users):<br \/>\n<code>$array = [<br \/>\n['user' =&gt; ['id' =&gt; 1, 'name' =&gt; 'User 1', 'email' =&gt; 'user1@example.com']],<br \/>\n['user' =&gt; ['id' =&gt; 2, 'name' =&gt; 'User 2', 'email' =&gt; 'user2@example.com']],<br \/>\n];<br \/>\n$emails = [];<br \/>\nforeach ($array as $result) {<br \/>\n$emails[] = $result['user']['email'];<br \/>\n}<br \/>\n\/*<br \/>\n[<br \/>\n\"user1@example.com\",<br \/>\n\"user2@example.com\",<br \/>\n]\n*\/<\/code><\/p>\n<p>Laravel&#8217;s Arr::pluck() helper makes this useful:<br \/>\n<code>Arr::pluck($array, 'user.email');<\/code><\/p>\n<p><small><em>If you have skills in PHP programming and you want to enhance your career in this field, a PHP certification from StudySection can help you reach your desired goals. Both beginner level and expert level <a href=\"https:\/\/www.studysection.com\/php-web-developer-foundation-diploma\">PHP Certification Exams<\/a> are offered by StudySection along with other programming certification exams. <\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Elegant, straightforward, and expressive are all characteristics of Laravel. Laravel&#8217;s helper functions are among its most effective features. The array<\/p>\n","protected":false},"author":1,"featured_media":7042,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[518,65],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Laravel Array Helpers - StudySection Blog<\/title>\n<meta name=\"description\" content=\"Elegant, straightforward, and expressive are all characteristics of Laravel. Laravel array helpers functions are among its features.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/laravel-array-helpers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Laravel Array Helpers - StudySection Blog\" \/>\n<meta property=\"og:description\" content=\"Elegant, straightforward, and expressive are all characteristics of Laravel. Laravel array helpers functions are among its features.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/laravel-array-helpers\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2023-09-25T11:12:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-21T06:35:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2023\/09\/Laravel-Array1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/laravel-array-helpers\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/laravel-array-helpers\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Laravel Array Helpers\",\"datePublished\":\"2023-09-25T11:12:51+00:00\",\"dateModified\":\"2023-11-21T06:35:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/laravel-array-helpers\/\"},\"wordCount\":404,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"Array\",\"laravel\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/laravel-array-helpers\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/laravel-array-helpers\/\",\"url\":\"https:\/\/studysection.com\/blog\/laravel-array-helpers\/\",\"name\":\"Laravel Array Helpers - StudySection Blog\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2023-09-25T11:12:51+00:00\",\"dateModified\":\"2023-11-21T06:35:45+00:00\",\"description\":\"Elegant, straightforward, and expressive are all characteristics of Laravel. Laravel array helpers functions are among its features.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/laravel-array-helpers\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/laravel-array-helpers\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/laravel-array-helpers\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Laravel Array Helpers\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Laravel Array Helpers - StudySection Blog","description":"Elegant, straightforward, and expressive are all characteristics of Laravel. Laravel array helpers functions are among its features.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/studysection.com\/blog\/laravel-array-helpers\/","og_locale":"en_US","og_type":"article","og_title":"Laravel Array Helpers - StudySection Blog","og_description":"Elegant, straightforward, and expressive are all characteristics of Laravel. Laravel array helpers functions are among its features.","og_url":"https:\/\/studysection.com\/blog\/laravel-array-helpers\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2023-09-25T11:12:51+00:00","article_modified_time":"2023-11-21T06:35:45+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2023\/09\/Laravel-Array1.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/laravel-array-helpers\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/laravel-array-helpers\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Laravel Array Helpers","datePublished":"2023-09-25T11:12:51+00:00","dateModified":"2023-11-21T06:35:45+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/laravel-array-helpers\/"},"wordCount":404,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["Array","laravel"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/laravel-array-helpers\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/laravel-array-helpers\/","url":"https:\/\/studysection.com\/blog\/laravel-array-helpers\/","name":"Laravel Array Helpers - StudySection Blog","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2023-09-25T11:12:51+00:00","dateModified":"2023-11-21T06:35:45+00:00","description":"Elegant, straightforward, and expressive are all characteristics of Laravel. Laravel array helpers functions are among its features.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/laravel-array-helpers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/laravel-array-helpers\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/laravel-array-helpers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Laravel Array Helpers"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":183,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/7041"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=7041"}],"version-history":[{"count":3,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/7041\/revisions"}],"predecessor-version":[{"id":7180,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/7041\/revisions\/7180"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/7042"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=7041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=7041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=7041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}