Array in php

Sharing is caring!

What is PHP Arrays

Arrays are complex variables that allow us to store more than one value or a group of values under a single variable name. Let’s suppose you want to store colors in your PHP script.

An array is a data structure that stores one or more similar type of values in a single value. For example if you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100 length.

There are three different kind of arrays and each array value is accessed using an ID c which is called array index.

  • Numeric array − An array with a numeric index. Values are stored and accessed in linear fashion.
  • Associative array − An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.
  • Multidimensional array − An array containing one or more arrays and values are accessed using multiple indices

NOTE − Built-in array functions is given in function reference PHP Array Functions.

Numeric Array

These arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero.

Example

Following is the example showing how to create and access numeric arrays.

Here we have used array() function to create array. This function is explained in function reference.

<html>
   <body>
   
      <?php
         /* First method to create array. */
         $numbers = array( 1, 2, 3, 4, 5);
         
         foreach( $numbers as $value ) {
            echo "Value is $value <br />";
         }
         
         /* Second method to create array. */
         $numbers[0] = "one";
         $numbers[1] = "two";
         $numbers[2] = "three";
         $numbers[3] = "four";
         $numbers[4] = "five";
         
         foreach( $numbers as $value ) {
            echo "Value is $value <br />";
         }
      ?>
      
   </body>
</html>

This will produce the following result −

Value is 1 
Value is 2 
Value is 3 
Value is 4 
Value is 5 
Value is one 
Value is two 
Value is three 
Value is four 
Value is five 

Associative Arrays

The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index. Associative array will have their index as string so that you can establish a strong association between key and values.

To store the salaries of employees in an array, a numerically indexed array would not be the best choice. Instead, we could use the employees names as the keys in our associative array, and the value would be their respective salary.

NOTE − Don’t keep associative array inside double quote while printing otherwise it would not return any value.

Example

<html>
   <body>
      
      <?php
         /* First method to associate create array. */
         $salaries = array("mohammad" => 2000, "qadir" => 1000, "zara" => 500);
         
         echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
         echo "Salary of qadir is ".  $salaries['qadir']. "<br />";
         echo "Salary of zara is ".  $salaries['zara']. "<br />";
         
         /* Second method to create array. */
         $salaries['mohammad'] = "high";
         $salaries['qadir'] = "medium";
         $salaries['zara'] = "low";
         
         echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
         echo "Salary of qadir is ".  $salaries['qadir']. "<br />";
         echo "Salary of zara is ".  $salaries['zara']. "<br />";
      ?>
   
   </body>
</html>

This will produce the following result −

Salary of mohammad is 2000
Salary of qadir is 1000
Salary of zara is 500
Salary of mohammad is high
Salary of qadir is medium
Salary of zara is low

Multidimensional Arrays

A multi-dimensional array each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. Values in the multi-dimensional array are accessed using multiple index.

Example

In this example we create a two dimensional array to store marks of three students in three subjects −

This example is an associative array, you can create numeric array in the same fashion.

<html>
   <body>
      
      <?php
         $marks = array( 
            "mohammad" => array (
               "physics" => 35,
               "maths" => 30,	
               "chemistry" => 39
            ),
            
            "qadir" => array (
               "physics" => 30,
               "maths" => 32,
               "chemistry" => 29
            ),
            
            "zara" => array (
               "physics" => 31,
               "maths" => 22,
               "chemistry" => 39
            )
         );
         
         /* Accessing multi-dimensional array values */
         echo "Marks for mohammad in physics : " ;
         echo $marks['mohammad']['physics'] . "<br />"; 
         
         echo "Marks for qadir in maths : ";
         echo $marks['qadir']['maths'] . "<br />"; 
         
         echo "Marks for zara in chemistry : " ;
         echo $marks['zara']['chemistry'] . "<br />"; 
      ?>
   
   </body>
</html>

This will produce the following result −

Marks for mohammad in physics : 35
Marks for qadir in maths : 32
Marks for zara in chemistry : 39

 

Comments 21

  • Currently it looks like Expression Engine is the best blogging platform out there right
    now. (from what I’ve read) Is that what you’re using on your blog? http://www.facebook.com/Vitamin-Shoppe-Coupon-Codes-1880283042254350/

  • I every time emailed this webpage post page to all my friends, for the reason that if like to read it afterward my links will too.

  • Hi, I do think this is a great site. I stumbledupon it 😉 I will return once again since i have book marked it. Money and freedom is the best way to change, may you be rich and continue to help others.

  • Hi, i read your blog from time to time and i own a similar one and i was just wondering if you get a lot of spam feedback? If so how do you protect against it, any plugin or anything you can advise? I get so much lately it’s driving me insane so any support is very much appreciated.

  • Do you have a spam problem on this site; I also am a blogger, and I was wanting to know your situation; many of us have developed some nice practices and we are looking to swap solutions with others, be sure to shoot me an email if interested.

  • Hi, i think that i saw you visited my blog thus i came to go back the choose?.I am trying to in finding issues to improve my web site!I assume its ok to make use of a few of your ideas!!

  • Article writing is also a fun, if you be familiar with afterward you can write if not it is difficult to write.

  • Asking questions are in fact fastidious thing if you are not understanding something completely, except this paragraph provides pleasant understanding yet.

  • Appreciating the time and energy you put into your website and in depth information you present. It’s great to come across a blog every once in a while that isn’t the same unwanted rehashed information. Excellent read! I’ve bookmarked your site and I’m including your RSS feeds to my Google account.

  • [url=http://samorazvite.ru/ob-ustanovkax-gorizontalnogo-napravlennogo-bureniya/]расчет работы гнб[/url]
    [url=http://yo-robot.ru/kak-ppavilyno-vbpaty-obopydovanie-dlya-ghb.html]бур ручной для горизонтального бурения купить[/url]
    [url=http://gala3d.ru/stati-i-novosti/kak-vybpat-ghb-yctanovky.html]гост на гнб[/url]
    [url=http://gala3d.ru/stati-i-novosti/kak-vybpat-ghb-yctanovky.html]гнб схемы[/url]
    [url=http://yo-robot.ru/kak-ppavilyno-vbpaty-obopydovanie-dlya-ghb.html]горизонтальное бурение объявления[/url]
    [url=http://gala3d.ru/stati-i-novosti/kak-vybpat-ghb-yctanovky.html]ремонт зондов гнб[/url]
    [url=http://gala3d.ru/stati-i-novosti/kak-vybpat-ghb-yctanovky.html]vermeer 2010[/url]
    [url=http://svet19.ru/shou-biznes/gnb-yctanovki.html]всн 116 93 гнб[/url]
    [url=http://phenom-tour.ru/zabavno/vidy-stenovyx-materialov.html]буровая машина пбу 2[/url]
    [url=http://gala3d.ru/stati-i-novosti/kak-vybpat-ghb-yctanovky.html]гнб китайского производства отзывы[/url]
    [url=http://svet19.ru/shou-biznes/gnb-yctanovki.html]аренда гнб в саратове[/url]
    [url=http://gala3d.ru/stati-i-novosti/kak-vybpat-ghb-yctanovky.html]метод управления горизонтально направленного бурения[/url]

  • Google

    The facts mentioned within the article are several of the top obtainable.

  • Buy at low price generic pills no Rx, Low prices!, [url=http://www.rsssecurity.net/index.php/gain-cheap-pills-no-prescription]cheap pills[/url]

  • แนะนำเว็บให้บริการ [url=https://www.sbobet7x.com/live-sbobet][u][b]sbobet ทางเข้า มือถือ[/b][/u][/url]

    บริการดีมากครับ พนักงานพูดสุภาพมากๆ ประทับใจมากครับ ไม่เคยคิดจะเปลี่ยนเว็ปเลย มีปัญหาโทรได้ตลอด 24 ชั่วโมง ใช้บริการกับ sbobet7x มาได้สักพัก ไม่มีปัญหาอะไร โปรโมชั่นก็ได้จริง ขอบคุณมากครับ บริการดีครับ ทำรายการอะไรต่าง ๆ ก็รวดเร็ว ขอให้บริการดี ๆ แบบนี้ตลอดไปนะครับ
    ประทับใจการฝากถอน บริการได้เร็วดี แม้จะเป็นช่วงเย็นๆ เห็นบอกว่าคนทำรายการเยอะก็ยังรอไม่เกิน 5 นาทีใช้บริการออนไลน์ผ่านหน้าเว็บไซต์ ทั้ง สมัครสมาชิก ฝากเงิน ถอนเงิน สะดวกดี ดีตรงไม่ต้องพูดคุยกับคน ผมชอบมาก มาสมัครแรกๆ ตอนแรกนึกว่าบบริการไม่ดี ไปไงไปมาบริการดีโครตครับ สอนผมตั้งแต่เล่นไม่เป็นและแทงบอลได้จนรวยทุกวันนี้

    [url=https://www.sbobet7x.com/ดูบอลสดออนไลน์][b]ดูบอลยูโรออนไลน์[/b][/url]

    อัพเดตผลบอลสดๆ ทันเวลาทุกวัน ทุกลีก ไม่ต้องรอนานเหมือนเว็บอื่น มาตรฐานสากล ดูบอลสด ดูบอลออนไลน์ ดูบอล warp football ลิ้งดูบอล warpfootball บอลสด บอลออนไลน์ ลิงค์ดูบอล ดบอลสด ดูบอลสด youtube ดูบอล ออนไลน์ ดูบอลออนไลน์ cth ดูฟุตบอลออนไลน์ ดูบอลออนไล preview football ลิ้งดูบอลสด ฟุตบอลออนไลน์ wrap football ลิ้งค์ดูบอลสด วาปฟุตบอล link ดูบอล ดูฟุตบอลสด ดูบอลผ่านเน็ต ถ่ายทอดสด ถ่ายทอดสดบอล ดูบอลสดออนไลน์ เว็บดูบอล ดูบอลสด วันนี้ ดูบอลออนไลน์ hd ดูบอลสด youtube มือถือ fดูบอลสด ดูบอลไทย วาร์ปฟุตบอล ดูบอลออนไลน์ฟรี ถ่ายทอดสดฟุตบอล วาปบอล ลิงดูบอล ดู บอล สด ดูบอล cth ดูบอลสด cth ลิ้งค์ดูบอล wap football fดูบอลออนไลน์

  • That is really fascinating, You’re a very skilled blogger. I have joined your rss feed and look forward to in the hunt for extra of your fantastic post. Additionally, I’ve shared your website in my social networks!

  • You created some decent points there. I looked on the net for the problem and discovered most people will go along with with your web page.

  • I’m extremely impressed with your writing skills and also with the layout on your weblog. Is this a paid theme or did you customize it yourself? Either way keep up the nice quality writing, it is rare to see a great blog like this one today..

Leave a Reply

Your email address will not be published. Required fields are marked *