PHP Tutorial #01 Indexed Arrays, Associative Arrays and Multidimensional Arrays

in utopian-io •  5 months ago

Image Source

Repository

https://github.com/php/php-src

What Will I Learn?

I will learn the arrays , I will cover 3 types of arrays indexed arrays, associative arrays and multidimensional arrays with examples .

  • What's the Indexed Arrays and how to use it.
  • The definition of Associative Arrays and  their uses. 
  •  The Multidimensional Arrays.

Requirements

  • Server support PHP , Xampp or Wamp for example
  • An IDE like Sublime text.
  • Browser (Chrome for example)

Difficulty

  • Basic

Description

In this tutorial we will start the PHP programming language, we will talk about the arrays ,the types of arrays and the difference between them.

Introduction To Arrays :

Arrays are types of structured data that group information together. The information can be in many types (integers, reals, floats, Booleans, strings ..etc), arrays can store one or more values at a time and the values can be different ( integer with string for example ). 

When declaring a table, it is useless to specify its size and the type of data it will contain. PHP does it automatically. The arrays are dynamic tables. At each new entry saved in the table, PHP expands its size by 1 element. There is three types of arrays: numeric index arrays, associative arrays and multidimensional arrays.  

1 - Indexed Arrays :

An indexed array is simply a list of elements each identified by a unique numeric index. The first element of the array will be indexed by index 0, the second by index 1, the third by index 2, and so on. The indexes are automatically set by PHP.  

To declare an indexed array we use this syntax : 

$name = array(value1, value2, value3 ..etc);

In my example I will declare an array of friends , the type of values that will contain is "String" 

$Friends = array(

"Alexendre",

"Alex",

"Aldwin",

"Arden",

"Mick"

);

To show the array you have many ways, you can use the print_r($array) method, you can also use a for loop or foreach loop ..etc

for ($i = 0; $i < $nub; $i++ ) {

     echo $Friends[$i] . " <br/>";

}

print_r() method :

echo "<pre>";

print_r($Friends);

echo "</pre>";

I have used two methods and this is the result of each one : 

2- Associative Arrays :  

Associative arrays work on the same principle, except that instead of numbering the boxes, we will label them by giving each a different name. 

To declare an indexed array we use this syntax : 

$name = array(

   key => value 

);

The difference between the associative and the indexed arrays is the keys, in the indexed arrays the PHP give for each value an index, in the associative array that's you who give each value a key.

$languages = array(

"en" => "English",

"fr" => "French",

"es" => "Spanish",

"pt" => "Portuguese"

);

You have many ways to show the values of the associative arrays, let's use the print_r method again and the foreach loop.

echo "<pre>";

print_r($languages);

echo "</pre>";

Foreach Loop :

foreach ($languages as $lang => $langname) {

echo "[" . $lang . "] is the key of " . $langname . "<br/>";

}

And this is the result :

3-  Multidimensional Arrays : 

The multidimensional array is an array contains one or more arrays , the keys of this array can be automatically from 0 to the last item , or by giving each a different name as associative array .

To declare an indexed array we use this syntax : 

$name = array(

    array(

             array(),

             array()

    ),

    array()

);

The main array contains many other arrays , it will be multidimensional array more than 2D.

$materials = array( //associative array

"Phones" => array(//associative array

"Samsung" => array ( //indexed array

"Samsung S",

"Samsung Note",

"Samsung G"

),

"iPhone" => array ( //indexed array

"iPhone SE",

"iPhone 8",

"iPhone X"

),

)

);

I will use the foreach and print_r to show the values of this array and this is the result :

Video Tutorial

Curriculum

This is the first video in this tutorial . 

Proof of Work Done

https://github.com/alexendre-maxim/PHP-Tutorial

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

I thank you for your contribution. Here are my thoughts;

  • There were lots of structure and grammar mistakes in your speech. I suggest you structuring your speech before recording it and practicing it until it is reliable. Having these mistakes makes your speech hard to understand. A (video) tutorial must be formal and easily understood.

  • Instead of writing examples while you are recording, prepare them beforehand or cut the parts of the video while you are writing it. By doing that, you can greatly reduce the time and filler content which results in content/time efficiency.


Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

·

Thank you for your review, @yokunjon!

So far this week you've reviewed 2 contributions. Keep up the good work!

·

Thank you @yokunjon, I will try to structure my speech before recording .

Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!

Reply !stop to disable the comment. Thanks!

Hi @alexendre-maxim!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @alexendre-maxim!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Congratulations @alexendre-maxim! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of posts published

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Do you like SteemitBoard's project? Then Vote for its witness and get one more award!