Array
(
[0] => 1
[1] => Array
(
[id] => 59576
[user_id] => 40151
[username] => ashmenon
[title] => Beginner PHP Chapter 5 - Arrays - Code Block 1
[language] => PHP
[comment] =>
[created] => 2011-10-09 20:54:04
[updated] => 2013-05-23 18:09:14
[source] => <?php
$variable_name = array(value1, value2, value3);
?>
[tags] => php array beginner
[snipplr_url] => http://snipplr.com/view/59576/beginner-php-chapter-5--arrays--code-block-1
)
)
Array
(
[0] => 1
[1] => Array
(
[id] => 67113
[user_id] => 40151
[username] => ashmenon
[title] => Beginner PHP Chapter 5 - Arrays - Code Block 2
[language] => PHP
[comment] => Beginner PHP Chapter 5 - Arrays
[created] => 2012-09-09 21:37:00
[updated] => 2013-05-23 18:09:14
[source] => <?php
$variable_name = array(); //Create a blank array, or empties an array.
$variable_name[] = 'valueA'; //Add a value to the array. If you do this again, it will add a second value to it, not replace the first one.
array_push($variable_name,'valueB'); //Another way to add a value to an array.
echo count($variable_name); //Displays the number of values in the array. This will input 2, because we added valueA and valueB to the array.
sort($variable_name); //Sorts the values within an array. There are several types of sorting, each with their own options.
?>
[tags] =>
[snipplr_url] => http://snipplr.com/view/67113/beginner-php-chapter-5--arrays--code-block-2
)
)
Array
(
[0] => 1
[1] => Array
(
[id] => 67114
[user_id] => 40151
[username] => ashmenon
[title] => Beginner PHP Chapter 5 - Arrays - Code Block 3
[language] => PHP
[comment] => Beginner PHP Chapter 5 - Arrays
[created] => 2012-09-09 21:42:06
[updated] => 2013-05-23 18:09:14
[source] => <?php
$array = array('dolphin','llama');
echo($array);
?>
[tags] =>
[snipplr_url] => http://snipplr.com/view/67114/beginner-php-chapter-5--arrays--code-block-3
)
)
Array
(
[0] => 1
[1] => Array
(
[id] => 67115
[user_id] => 40151
[username] => ashmenon
[title] => Beginner PHP Chapter 5 - Arrays - Code Block 4
[language] => PHP
[comment] => Beginner PHP Chapter 5 - Arrays
[created] => 2012-09-09 21:44:29
[updated] => 2013-05-23 18:09:14
[source] => <?php
$array = array('dolphin','llama');
print_r($array);
?>
[tags] =>
[snipplr_url] => http://snipplr.com/view/67115/beginner-php-chapter-5--arrays--code-block-4
)
)
Array
(
[0] => 1
[1] => Array
(
[id] => 67116
[user_id] => 40151
[username] => ashmenon
[title] => Beginner PHP Chapter 5 - Arrays - Code Block 5
[language] => PHP
[comment] => Beginner PHP Chapter 5 - Arrays
[created] => 2012-09-09 21:45:33
[updated] => 2013-05-23 18:09:14
[source] => <?php
$array = array('dolphin','llama');
echo($array[0]); //This will display the string 'dolphin'.
echo $array[1]; //This will display the string 'llama'.
?>
[tags] =>
[snipplr_url] => http://snipplr.com/view/67116/beginner-php-chapter-5--arrays--code-block-5
)
)
Array
(
[0] => 1
[1] => Array
(
[id] => 67117
[user_id] => 40151
[username] => ashmenon
[title] => Beginner PHP Chapter 5 - Arrays - Code Block 6
[language] => PHP
[comment] => Beginner PHP Chapter 5 - Arrays
[created] => 2012-09-09 21:47:02
[updated] => 2013-05-23 18:09:14
[source] => <?php
$name = 'Sandra James';
$age = 45;
$bloodType = 'B+';
$height = 175;
$weight = 65;
$patient1 = array($name,$age,$bloodType, $height, $weight); //This creates an array with Sandra's name, age, and blood type.
print_r($patient1);
/* That will show you the following:
Array(
[0] => Sandra James
[1] => 45
[2] => B+
[3] => 175
[4] => 65
)
*/
?>
[tags] =>
[snipplr_url] => http://snipplr.com/view/67117/beginner-php-chapter-5--arrays--code-block-6
)
)
Array
(
[0] => 1
[1] => Array
(
[id] => 67118
[user_id] => 40151
[username] => ashmenon
[title] => Beginner PHP Chapter 5 - Arrays - Code Block 7
[language] => PHP
[comment] => Beginner PHP Chapter 5 - Arrays
[created] => 2012-09-09 21:48:20
[updated] => 2013-05-23 18:09:14
[source] => <?php
$name = 'Sandra James';
$age = 45;
$bloodType = 'B+';
$height = 175;
$weight = 65;
$patient1 = array(
'name' => $name,
'age' => $age,
'bloodType' => $bloodType,
'height' => $height,
'weight' => $weight
);
//You can do that in a single line like shown previously, I just broke it into multiple lines to make it easier to see.
print_r($patient1);
/* That will show you the following:
Array(
[name] => Sandra James
[age] => 45
[bloodType] => B+
[height] => 175
[weight] => 65
)
*/
?>
[tags] =>
[snipplr_url] => http://snipplr.com/view/67118/beginner-php-chapter-5--arrays--code-block-7
)
)
Array
(
[0] => 1
[1] => Array
(
[id] => 67119
[user_id] => 40151
[username] => ashmenon
[title] => Beginner PHP Chapter 5 - Arrays - Code Block 8
[language] => PHP
[comment] => Beginner PHP Chapter 5 - Arrays
[created] => 2012-09-09 21:51:30
[updated] => 2013-05-23 18:09:14
[source] => <?php
print_r($patient1);
/* That will show you the following:
Array(
[name] => Sandra James
[age] => 45
[bloodType] => B+
[height] => 175
[weight] => 65
)
*/
$patient1['weight'] = 62; //Been working out, Sandra?
print_r($patient1);
/* That will show you the following:
Array(
[name] => Sandra James
[age] => 45
[bloodType] => B+
[height] => 175
[weight] => 62
)
*/
?>
[tags] =>
[snipplr_url] => http://snipplr.com/view/67119/beginner-php-chapter-5--arrays--code-block-8
)
)
Array
(
[0] => 1
[1] => Array
(
[id] => 67120
[user_id] => 40151
[username] => ashmenon
[title] => Beginner PHP Chapter 5 - Arrays - Code Block 9
[language] => PHP
[comment] => Beginner PHP Chapter 5 - Arrays
[created] => 2012-09-09 21:52:07
[updated] => 2013-05-23 18:09:14
[source] => <?php
$visits = '11/12/09-11.02AM,13/01/10-2.15PM,16/01/10-10.57AM';
?>
[tags] =>
[snipplr_url] => http://snipplr.com/view/67120/beginner-php-chapter-5--arrays--code-block-9
)
)
Array
(
[0] => 1
[1] => Array
(
[id] => 67121
[user_id] => 40151
[username] => ashmenon
[title] => Beginner PHP Chapter 5 - Arrays - Code Block 10
[language] => PHP
[comment] => Beginner PHP Chapter 5 - Arrays
[created] => 2012-09-09 21:52:59
[updated] => 2013-05-23 18:09:14
[source] => <?php
$visit1 = array(
'date' => '11/12/09',
'time' => '11.02AM'
);
$visit2 = array(
'date' => '13/01/10',
'time' => '2.15PM'
);
$visit3 = array(
'date' => '16/01/10',
'time' => '10.57AM'
);
?>
[tags] =>
[snipplr_url] => http://snipplr.com/view/67121/beginner-php-chapter-5--arrays--code-block-10
)
)
Array
(
[0] => 1
[1] => Array
(
[id] => 67122
[user_id] => 40151
[username] => ashmenon
[title] => Beginner PHP Chapter 5 - Arrays - Code Block 11
[language] => PHP
[comment] => Beginner PHP Chapter 5 - Arrays
[created] => 2012-09-09 21:58:47
[updated] => 2013-05-23 18:09:14
[source] => <?php
//First let's combine the visits together.
$visits = array($visit1, $visit2, $visit3);
print_r($visits);
/* That will display like this:
Array(
[0] => Array(
[date] => 11/12/09
[time] => 11.02AM
)
[1] => Array(
[date] => 13/01/10
[time] => 2.15PM
)
[2] => Array(
[date] => 16/01/10
[time] => 10.57AM
)
)
*/
//And then we add this value to the patient variable.
$patient1['visits'] = $visits;
?>
[tags] =>
[snipplr_url] => http://snipplr.com/view/67122/beginner-php-chapter-5--arrays--code-block-11
)
)
Array
(
[0] => 1
[1] => Array
(
[id] => 67123
[user_id] => 40151
[username] => ashmenon
[title] => Beginner PHP Chapter 5 - Arrays - Code Block 12
[language] => PHP
[comment] => Beginner PHP Chapter 5 - Arrays
[created] => 2012-09-09 22:00:12
[updated] => 2013-05-23 18:09:14
[source] => <?php
print_r($patient1);
/* That will show you the following:
Array(
[name] => Sandra James
[age] => 45
[bloodType] => B+
[height] => 175
[weight] => 62
[visits] => Array(
[0] => Array(
[date] => 11/12/09
[time] => 11.02AM
)
[1] => Array(
[date] => 13/01/10
[time] => 2.15PM
)
[2] => Array(
[date] => 16/01/10
[time] => 10.57AM
)
)
)
*/
?>
[tags] =>
[snipplr_url] => http://snipplr.com/view/67123/beginner-php-chapter-5--arrays--code-block-12
)
)
Array
(
[0] => 1
[1] => Array
(
[id] => 67124
[user_id] => 40151
[username] => ashmenon
[title] => Beginner PHP Chapter 5 - Arrays - Code Block 13
[language] => PHP
[comment] => Beginner PHP Chapter 5 - Arrays
[created] => 2012-09-09 22:01:01
[updated] => 2013-05-23 18:09:14
[source] => <?php
echo $patient1['name']; //This will display the string 'Sandra James'.
echo $patient1['visits'][0]; //This will still display 'Array', because the returned value is an array.
echo $patient1['visits'][1]['time']; //This will display the string '2.15PM'.
?>
[tags] =>
[snipplr_url] => http://snipplr.com/view/67124/beginner-php-chapter-5--arrays--code-block-13
)
)
In the previous chapter of this beginner PHP series, I mentioned that I would be covering loop statements and nesting in the next chapter. This was originally true, but the more I wrote about it, the more I realized I was making references to arrays, which I haven’t covered yet. So consider this a last-minute [...]