Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

9/1/2021 Storing Data in Arrays (Programming PHP)

home |
O'Reilly's CD bookshelfs |
FreeBSD | Linux | Cisco |
Cisco Exam
 

5.3. Storing Data in Arrays


Ads by
Storing a value in an array will create
the array if it didn't already exist, but trying to
retrieve a value from an array that hasn't
been
defined yet won't create the array. For example:
Send feedba
// $addresses not defined before this point
echo $addresses[0]; // prints nothing Why this ad? 
echo $addresses; // prints nothing
$addresses[0] = 'spam@cyberpromo.net';
echo $addresses; // prints "Array"

Using simple assignment


to initialize an array in your program leads to code
like this:

$addresses[0] = 'spam@cyberpromo.net';
$addresses[1] = 'abuse@example.com';
$addresses[2] = 'root@example.com';
Ads by // ...

That's an indexed array, with integer indexes


beginning at 0. Here's an
associative array:
Send feedback
$price['Gasket'] = 15.29;
Why this ad?  $price['Wheel'] = 75.25;
$price['Tire'] = 50.00;
// ...

An easier way to initialize an array is to use the array(


) construct, which builds an array from
its arguments:

$addresses = array('spam@cyberpromo.net', 'abuse@example.com',


'root@example.com');

To create an associative array with array( ), use


the => symbol to separate indexes from
values:

$price = array('Gasket' => 15.29,


'Wheel' => 75.25,
'Tire' => 50.00);

Notice the use of whitespace and alignment. We could have bunched up


the code, but it wouldn't have been as easy to read:

$price = array('Gasket'=>15.29,'Wheel'=>75.25,'Tire'=>50.00);

To construct an empty array, pass no arguments to


array( ):

$addresses = array( );

You can specify an initial key with => and then


a list of values. The values are inserted into the array starting
with that key,
with subsequent values having sequential keys:

$days = array(1 => 'Monday', 'Tuesday', 'Wednesday',


'Thursday', 'Friday', 'Saturday', 'Sunday');
// 2 is Tuesday, 3 is Wednesday, etc.

If the initial index is a non-numeric string, subsequent indexes are


integers beginning at 0. Thus, the following code is
probably a
mistake:

$whoops = array('Friday' => 'Black', 'Brown', 'Green');


// same as
$whoops = array('Friday' => 'Black', 0 => 'Brown', 1 => 'Green');

5.3.1. Adding Values to the End of an Array

To insert more values into the end of an


existing indexed array, use the [] syntax:

$family = array('Fred', 'Wilma');


$family[] = 'Pebbles'; // $family[2] is 'Pebbles'

This construct assumes the array's indexes are


numbers and assigns elements into the next available numeric index,
starting
from 0. Attempting to append to an associative array is
almost always a programmer mistake, but PHP will give the new
elements numeric indexes without issuing a warning:

Ads by
Trying for a Top consultancy? // - Akudemy

$person = array('name' => 'Fred');


$person[] = 'Wilma'; $person[0] is now 'Wilma'
Send feedback Why this ad? 
- Interview Coaching
OPEN
5.3.2. Assigning a Range of Values
Learn what you need to pass Top tech exams, start today! github.com

https://docstore.mik.ua/orelly/webprog/php/ch05_03.htm 1/3
9/1/2021 Storing Data in Arrays (Programming PHP)
The range( )
function creates an array of
consecutive integer or character values between the two values you
pass to it as
arguments. For example:

$numbers = range(2, 5); // $numbers = array(2, 3, 4, 5);


$letters = range('a', 'z'); // $numbers holds the alphabet
$reversed_numbers = range(5, 2); // $numbers = array(5, 4, 3, 2);

Only the first letter of a string argument is used to build the range:

range('aaa', 'zzz') /// same as range('a','z')

5.3.3. Getting the Size of an Array

The count( ) and sizeof( )


functions are identical in use and effect. They return the number of
elements in the array.
There is no stylistic preference about which
function you use. Here's an example:

$family = array('Fred', 'Wilma', 'Pebbles');


$size = count($family); // $size is 3

These functions do not consult any numeric indexes that might be


present:

$confusion = array( 10 => 'ten', 11 => 'eleven', 12 => 'twelve');


$size = count($confusion); // $size is 3

5.3.4. Padding an Array


Ads by
To create an array
initialized to the same value, use array_pad( ).
The first argument to array_pad(
) is the array, the
Send feedback second argument is
the minimum number of elements you want the array to have, and the
third argument is the value to give
any elements that are created.
The array_pad( ) function returns a new padded
array, leaving its argument array alone.
Why this ad? 
Here's array_pad( ) in action:

$scores = array(5, 10);


$padded = array_pad($scores, 5, 0); // $padded is now array(5, 10, 0, 0, 0)

Notice how the new values are appended to the end of the array. If
you want the new values added to the start of the array,
use a
negative second argument:

$padded = array_pad($scores, -5, 0);

Assign the results of array_pad( ) back to the


original array to get the effect of an in situ change:

$scores = array_pad($scores, 5, 0);

If you pad an associative


array, existing keys will be preserved. New elements will have
numeric keys starting at 0.

5.2. Identifying Elements of an 5.4. Multidimensional Arrays


Array

Copyright © 2003 O'Reilly & Associates. All rights reserved.

Ads by
Send feedback Why this ad? 

Write With Confidence

Check your grammar, spelling, and punctuation


instantly with Grammarly
Grammarly
Ads by
Trying for a Top consultancy? - Akudemy

Send feedback Why this ad? 


- Interview Coaching
OPEN

Learn what you need to pass Top tech exams, start today! github.com

https://docstore.mik.ua/orelly/webprog/php/ch05_03.htm 2/3
9/1/2021 Storing Data in Arrays (Programming PHP)

Ads by

Send feedback

Why this ad? 

Ads by
Trying for a Top consultancy? - Akudemy

Send feedback Why this ad? 


- Interview Coaching
OPEN

Learn what you need to pass Top tech exams, start today! github.com

https://docstore.mik.ua/orelly/webprog/php/ch05_03.htm 3/3

You might also like