{"id":22720,"date":"2024-03-02T05:50:11","date_gmt":"2024-03-02T04:50:11","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/funciones-de-arrays-en-php-guia-completa-para-principiantes\/"},"modified":"2024-06-03T18:40:40","modified_gmt":"2024-06-03T17:40:40","slug":"funciones-de-arrays-en-php-guia-completa-para-principiantes","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/blog\/array-functions-in-php-complete-guide-for-beginners\/","title":{"rendered":"Array Functions in PHP: Complete Guide for Beginners"},"content":{"rendered":"<p>In the world of programming, arrays are a fundamental data structure that allows us to store and manipulate information efficiently. And in PHP, one of the most popular programming languages on the web, it is no different. In this article, I will guide you through the array functions in PHP, from the most basic to the most advanced, so that you can master this powerful tool.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ffffff;color:#ffffff\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ffffff;color:#ffffff\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/nelkodev.com\/en\/blog\/array-functions-in-php-complete-guide-for-beginners\/#%C2%BFQue_es_un_array_en_PHP\" >What is an array in PHP?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/nelkodev.com\/en\/blog\/array-functions-in-php-complete-guide-for-beginners\/#Funciones_basicas_de_arrays_en_PHP\" >Basic array functions in PHP<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/nelkodev.com\/en\/blog\/array-functions-in-php-complete-guide-for-beginners\/#count\" >count()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/nelkodev.com\/en\/blog\/array-functions-in-php-complete-guide-for-beginners\/#array_push\" >array_push()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/nelkodev.com\/en\/blog\/array-functions-in-php-complete-guide-for-beginners\/#array_pop\" >array_pop()<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/nelkodev.com\/en\/blog\/array-functions-in-php-complete-guide-for-beginners\/#Funciones_avanzadas_de_arrays_en_PHP\" >Advanced array functions in PHP<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/nelkodev.com\/en\/blog\/array-functions-in-php-complete-guide-for-beginners\/#array_merge\" >array_merge()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/nelkodev.com\/en\/blog\/array-functions-in-php-complete-guide-for-beginners\/#array_key_exists\" >array_key_exists()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/nelkodev.com\/en\/blog\/array-functions-in-php-complete-guide-for-beginners\/#array_search\" >array_search()<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/nelkodev.com\/en\/blog\/array-functions-in-php-complete-guide-for-beginners\/#Preguntas_frecuentes\" >Frequently asked questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/nelkodev.com\/en\/blog\/array-functions-in-php-complete-guide-for-beginners\/#%C2%BFQue_es_un_array_en_PHP-2\" >What is an array in PHP?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/nelkodev.com\/en\/blog\/array-functions-in-php-complete-guide-for-beginners\/#%C2%BFCual_es_la_diferencia_entre_los_arrays_indexados_y_los_arrays_asociativos\" >What is the difference between indexed arrays and associative arrays?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/nelkodev.com\/en\/blog\/array-functions-in-php-complete-guide-for-beginners\/#%C2%BFCual_es_la_funcion_de_la_funcion_count_en_PHP\" >What is the function of the count() function in PHP?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/nelkodev.com\/en\/blog\/array-functions-in-php-complete-guide-for-beginners\/#%C2%BFComo_puedo_agregar_un_elemento_al_final_de_un_array_en_PHP\" >How can I add an element to the end of an array in PHP?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/nelkodev.com\/en\/blog\/array-functions-in-php-complete-guide-for-beginners\/#%C2%BFComo_puedo_combinar_dos_arrays_en_PHP\" >How can I combine two arrays in PHP?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFQue_es_un_array_en_PHP\"><\/span>What is an array in PHP?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>An array in PHP is an ordered collection of elements, in which each element is associated with a unique key. This means that we can store several different values within a single array, and access them using their corresponding key. For example:<\/p>\n<pre>\n$cars = array(&quot;Volvo&quot;, &quot;BMW&quot;, &quot;Ford&quot;);\n<\/pre>\n<p>In this case, &quot;cars&quot; is the name of the array, and the values &quot;Volvo&quot;, &quot;BMW&quot; and &quot;Ford&quot; are the elements stored in it. We can access these elements using their position in the array, starting with 0. For example, to access the &quot;BMW&quot; element, we would use the following syntax:<\/p>\n<pre>\necho $cars[1];\n<\/pre>\n<p>This will print &quot;BMW&quot; on the screen. Easy, right?<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Funciones_basicas_de_arrays_en_PHP\"><\/span>Basic array functions in PHP<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In PHP, there are a series of predefined functions that allow us to perform various operations with arrays. Next, I will show you some of the most used functions:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"count\"><\/span>count()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The count() function allows us to obtain the number of elements contained in an array. For example:<\/p>\n<pre>\n$cars = array(&quot;Volvo&quot;, &quot;BMW&quot;, &quot;Ford&quot;); echo count($cars); \/\/ Output: 3\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"array_push\"><\/span>array_push()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The array_push() function allows us to add one or more elements to the end of an array. For example:<\/p>\n<pre>\n$cars = array(&quot;Volvo&quot;, &quot;BMW&quot;, &quot;Ford&quot;); array_push($cars, &quot;Toyota&quot;); print_r($cars); \/\/ Output: Array ( [0] =&gt; Volvo [1] =&gt; BMW [2] =&gt; Ford [3] =&gt; Toyota )\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"array_pop\"><\/span>array_pop()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The array_pop() function allows us to remove the last element from an array and return it. For example:<\/p>\n<pre>\n$cars = array(&quot;Volvo&quot;, &quot;BMW&quot;, &quot;Ford&quot;); $last_car = array_pop($cars); echo $last_car; \/\/ Output: Ford print_r($cars); \/\/ Output: Array ( [0] =&gt; Volvo [1] =&gt; BMW )\n<\/pre>\n<p>These are just some of the basic functions of arrays in PHP. However, there are many more that you can explore to expand your knowledge.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Funciones_avanzadas_de_arrays_en_PHP\"><\/span>Advanced array functions in PHP<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In addition to the basic functions, PHP also has more advanced functions that allow you to perform more complex operations with arrays. Next, I will show you some of them:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"array_merge\"><\/span>array_merge()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The array_merge() function allows us to combine two or more arrays into one. For example:<\/p>\n<pre>\n$cars = array(&quot;Volvo&quot;, &quot;BMW&quot;); $colors = array(&quot;red&quot;, &quot;blue&quot;); $result = array_merge($cars, $colors); print_r($hresult); \/\/ Output: Array ( [0] =&gt; Volvo [1] =&gt; BMW [2] =&gt; red [3] =&gt; blue )\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"array_key_exists\"><\/span>array_key_exists()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The array_key_exists() function allows us to check if a given key exists in an array. For example:<\/p>\n<pre>\n$person = array(&quot;name&quot; =&gt; &quot;John&quot;, &quot;age&quot; =&gt; 30, &quot;country&quot; =&gt; &quot;USA&quot;); if (array_key_exists(&quot;name&quot;, $person)) { echo &quot;Name exists!&quot;; } else { echo &quot;Name does not exist!&quot;; }\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"array_search\"><\/span>array_search()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The array_search() function allows us to search for a given value within an array and return the corresponding key. For example:<\/p>\n<pre>\n$cars = array(&quot;Volvo&quot;, &quot;BMW&quot;, &quot;Ford&quot;); $key = array_search(&quot;BMW&quot;, $cars); echo $key; \/\/ Output: 1\n<\/pre>\n<p>These are just some of the advanced array functions in PHP. As you gain more experience in the language, you will discover many more that will be very useful to you.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Preguntas_frecuentes\"><\/span>Frequently asked questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFQue_es_un_array_en_PHP-2\"><\/span>What is an array in PHP?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>An array in PHP is a data structure that allows us to store multiple values in a single variable. Each value is associated with a unique key that allows us to access it individually.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFCual_es_la_diferencia_entre_los_arrays_indexados_y_los_arrays_asociativos\"><\/span>What is the difference between indexed arrays and associative arrays?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In indexed arrays, elements are arranged in a numerical sequence, starting from 0. On the other hand, in associative arrays, elements are organized using custom keys instead of numerical positions.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFCual_es_la_funcion_de_la_funcion_count_en_PHP\"><\/span>What is the function of the count() function in PHP?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The count() function allows us to obtain the number of elements contained in an array. This is useful when we need to know how many elements are in an array at a certain time.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFComo_puedo_agregar_un_elemento_al_final_de_un_array_en_PHP\"><\/span>How can I add an element to the end of an array in PHP?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Using the array_push() function, you can add one or more elements to the end of an existing array. Simply provide the array and the elements you want to add as arguments to the function.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFComo_puedo_combinar_dos_arrays_en_PHP\"><\/span>How can I combine two arrays in PHP?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To merge two or more arrays into one, you can use the array_merge() function. This function accepts the arrays you want to combine as arguments and returns a new array containing all the elements of the original arrays.<\/p>\n<p>I hope this guide has given you a complete overview of array functions in PHP. Remember to practice and experiment with them to become more and more familiar with their use. Good luck on your programming journey!<\/p>","protected":false},"excerpt":{"rendered":"<p>En el mundo de la programaci\u00f3n, los arrays son una estructura de datos fundamental que nos permite almacenar y manipular informaci\u00f3n de manera eficiente. Y en PHP, uno de los lenguajes de programaci\u00f3n m\u00e1s populares en la web, no es diferente. En este art\u00edculo, te guiar\u00e9 a trav\u00e9s de las funciones de arrays en PHP, [&hellip;]<\/p>","protected":false},"author":1,"featured_media":22721,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[420,2206,16],"tags":[584,205,500,147,358,60,15,175,101],"class_list":["post-22720","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-php","category-programacion","tag-arrays","tag-blog","tag-completa","tag-funciones","tag-guia","tag-para","tag-php","tag-principiantes","tag-programacion"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/22720","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/comments?post=22720"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/22720\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/22721"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=22720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=22720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=22720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}