PHP-Associative Array tutorial
How to use PHP-Associative Array
You can visit the part1 of php arrays if not yet.
- An associative array, each value associated with key.
- Key can be numeric or a string.
- Keys should be unique should not have duplicates.
- values can be repeated.
In this tutorial you are going to learn following points on Associative arrays
Creating associative array
Printing associative array elements
Printing associative array using foreach loop
Removing array element
Searching in an array
Array Manipulations
1. Creating associative array
example:
In this example all keys are string type and values can be any type.
$info=array( 'company' => 'e-trainings' , 'city' => 'Hyderabad' );
2. Printing associative array elements
<?php
$info=array( 'company' => 'e-trainings' , 'city' =>'Hyderabad' );
print "<br>Company=". $info['company'] ;
print "<br>City=". $info['city'] ;
?>
Result:
Company=e-trainings
City=Delhi
3. Printing associative array using foreach loop
foreach loop access the array elements with key and values pairs.ex:
example.php
<?php
$info=array( 'company' => 'e-trainings' , 'city' => 'Hyderabad' );
foreach ($info as $key => $value) {
print $key . " => " . $value . "<BR>";
}
?>
Result:
company => e-trainings
city=> Hyderabad
4. Removing array element
unset($info["city" ]) ;
<?php
$info=array( 'company' => 'e-trainings' , 'city' => Hyderabad' );
unset($info["city" ]) ;
foreach ($info as $key => $value) {
print $key . " => " . $value . "<BR>";
}
?>
Result:
company => e-trainingsUsing numeric key:
array can have values of different type. but all keys are numeric
ex:
<?php
$numbers = array(1 => 10, 2 => 20, 3 => 30, 4 => 40);
echo $numbers[2] ; // prints 20
?>
key value
1 => 10,
2 => 20,
3 => 30,
4 => 40
More Associative Array examples:
$studentInfo = array("student_name" => "Rakesh", "course" =>" PHP", "score" => 299, "qualified" => TRUE );
$time = array('hours' => 10 , 'minutes' => 30, , 'seconds' => 47);
$db_record = array(id => 1, 'username' => 'bill' , 'password' => 'gates');
$file = array('name' => 'logo.jpg' , 'size' => 30, 'type' => 'file');
Using <pre> preformat/pretty format
ex:
<?php
$x = array('a'=>'A', 'b'=>'B', 'C');
echo '<pre>';
print_r($x);
echo '</pre>';
?>
Result:
Array
(
[a] => A
[b] => B
[0] => C
)
5. Searching in an array
<?php
$score['rita'] = 300;
$score['lilly'] = 259;
$score['dany'] = 356;
$score['sony'] = 376;
$score['rinku'] = 546;
$search_name = "rita";
foreach ($score as $key => $value)
{
if($key == $search_name )
print $value ."<br>";
}
?>
<?php
$ages = array("John"=>32, "Rita"=>30, "Mona"=>34);
echo "<br> John is " . $ages['John'] . " years old." . "<br>";
?>
o/p: John is 32 years old
6. Array Manipulations:
ex
<?php
$items = array( "pen" => 5, "pencil" => 3, "notebook" => 10 );
$total_price = ( $items['pen'] + $items['pencil'] + $items['notebook'] );
foreach ($items as $key => $value) {
print $key."=". $value. "<br>"; }
print "<br>Total Amount=". $total_price;
?>
**While accessing array elements the keys must be used with exact cases
ex:
$items['pen'] is correct , not $items['Pen']
PHP Multidimensional Array
Arrays inside another array are called Multi dimensional or nested arrays.
ex:
<?php
$college= array (
"batch-2017" => array("Sofie", "Robert", "Danial"),
"batch-2018" => array("Vinny"),
"batch-2019" => array("Brett", "Rita", "Junior")
);
print $college['batch-2017'][2];
?>
Result: Danial
Want to Learn more on arrays? visit link give below
Practice Interview Questions on PHP Arrays
Comments
Post a Comment