Saturday, 8 March 2014

Associative arrays in php

php arrays
Associative arrays in php programming languages.  Now in last tutorials we talk about arrays in this tutorials we looking for associative arrays. Now associative arrays stores data in same way but allows you to change keys from 0,1,2,.. to name of your choice.

Code for associative arrays (Screenshot1) :-

associative arrays
Screenshot1

<?php

$food = array('Pasta'=>300, 'Pizza'=>1000,'salad'=>150,'Vegetables'=>50);

print_r($food);

?>

Output Screenshot 2

Associative arrays in php
Screenshot2


If i want to add some elements to array such as foods name with his calories you many find in food for example now in basic array we seen that pasta had key value 0. In this example what i want to do is rather then having pasta key 0. i want to assign value to the pasta as well as same for pizza salad and vegetables for assigning key to variable i will equal and
greater then sign so, for pasta we may say 300 calories, pizza we can say equal and greater then 1000 calories,salad we may say 150 calories and in vegetables we may say 50 calories. so, in output you can see our key of  pasta is holding key of 300,our key of pizza is holding key of 1000, our key of salad is holding key of 150 and vegetables holding key of 50. Now if you echo $food[0]; it will echo blank because technically now its not the value  of 0. Now its became a associative array because we are associating pasta with value of 300.

code for associative arrays (screenshot 3):-

Associative arrays
Screenshot3

<?php

$food = array('Pasta'=>300, 'Pizza'=>1000,

'salad'=>150, 'Vegetables'=>50);

echo $food['pasta']

//print_r($food);

?>

output (Screenshot 4):-

associative arrays in php
Screenshot4
so, now we echo out contain of pasta it will give 300. In same way you can see the calories in pizza , salad and vegetables. so, this we created associative array to associating values to different keys and its by association so, now you will understand why it is called associative arrays. so, this is a best way in php specially when you do with databases later on. In database you know what fields are and columns are you have option to use associative array when you gain data from database. so, associative array is really really good to use specially when you don’t want to remember 0 , 1
,2 because that can be bit annoying so, this is basically associative array in php.


No comments:

Post a Comment

Thanks For Comment Will get you Soon..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates