PHP-Arrays
PHP-Arrays
AGENDA
Easy to remember
More logical
And work better for large code and multiple persons team
Associative Arrays for Feedback
<?php
$feedback['google'] = "Best Search Engine";
$feedback['yahoo'] = "Good for basic news";
$feedback['instagram'] = "Love for pics";
$feedback['facebook'] = "Stay connected with your friends";
echo $feedback['yahoo'];
?>
Output : Good for basic news
Assignment Using the array
Keyword (format index => value)
<?php
$feedback = array(
'google' =>"Best Search Engine",
'yahoo' => "Good for basic news",
'instagram' => "Love for pics",
'facebook' => "Stay connected with your friends“
);
echo $feedback['yahoo'];
?>
foreach loop
$j = 0;
foreach($feedback as $item)
{
echo "$j: $item \n";++$j;
}
0: Best Search Engine
1: Good for basic news
2: Love for pics
3: Stay connected with your friends
Accessing Key and Value
<?php
$feedback = array('google' =>"Best Search Engine",
'yahoo' => "Good for basic news",
'instagram' => "Love for pics",
'facebook' => "Stay connected with your friends");
while (list($item, $description) = each($feedback))
echo "$item: $description \n";
?>
Output
google: Best Search Engine
yahoo: Good for basic news
instagram: Love for pics
facebook: Stay connected with your friends
Multi-D Array
<?php Array
$feedback = (
array(1,2,array(3,4)); [0] => 1
[1] => 2
print_r($feedback);
[2] => Array
?>
(
[0] => 3
[1] => 4
)
)
M-D Associative Array
$feedback = array(
'user1' => array('google' =>"Best Search Engine",
'yahoo' => "Good for basic news",
'instagram' => "Love for pics",
'facebook' => "Stay connected with your friends"),
'user2' => array('google' =>"Search Engine",
'yahoo' => "I am not using",
'instagram' => "For young",
'facebook' => "For friends"),
'user3' => array('google' =>"I love it",
'yahoo' => "use only yahoo mail",
'instagram' => "I follow celebraity",
'facebook' => "I like it")
);
Accessing M-D Associative Array
foreach($feedback as $user => $users)
foreach($users as $key => $value)
echo "$user:\t$key\t($value) \n";
Output
user1: google (Best Search Engine)
user1: yahoo (Good for basic news)
user1: instagram (Love for pics)
user1: facebook (Stay connected with your
friends)
user2: google (Search Engine)
user2: yahoo (I am not using)
user2: instagram (For young)
user2: facebook (For friends)
user3: google (I love it)
user3: yahoo (use only yahoo mail)
user3: instagram (I follow celebraity)
user3: facebook (I like it)
Array Functions
is_array
Count
Sort
Shuffle
Explode
Extract
Compact
Reset
End
is_array()
To check variable is array or not
echo (is_array($fruits)) ? "Is an array" : "Is
not an array";
count($array)
Count all the top elements in a array
count($feedback) ; # 3
count($feedback); # 3
count($feedback); # 15 (12+3)
Reverse Order
rsort($fred, SORT_NUMERIC);
rsort($fred, SORT_STRING);
shuffle(array)
Array
Return true on success (
Return false on error [0] => 7
[1] => 2
[2] => 6
<?php [3] => 3
$nos = [4] => 4
array(1,2,3,4,5,6,7,8,9); [5] => 8
shuffle($nos); [6] => 1
[7] => 9
print_r($nos)
[8] => 5
?> )
explode()
Convert string into array using some separator
Separator may be any char
<?php
$temp = explode(' ', "I am a coder");
print_r($temp);
?>
Array ( [0] => I [1] => am [2] => a [3] => coder )
explode()
<?php
$temp = explode('#', "BCCD#ADDF#EEFG#AAAA");
print_r($temp);
?>
Array ( [0] => BCCD [1] => ADDF [2] => EEFG [3] => AAAA )
extract()
Convert key/value pairs from an array into PHP
variables
Example : $_GET or $_POST variables as sent to
a PHP script by a form
extract($_GET);
compact()
Inverse of extract()
<?php
$fname = "Krish";
$sname = "Richard";
$address = "A-3,NY, USA";
$contact = "854716632";
$contact = compact('fname', 'sname', 'address', 'contact');
print_r($contact);
?>
output
Array
(
[fname] => Krish
[sname] => Richard
[address] => A-3,NY, USA
[contact] => 854716632
)
Thank You
Q& A