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

Programming PHPProgramming PHPSearch this book

5.5. Extracting Multiple Values

To copy all of an array's values into variables, use the list( ) construct:

list($variable, ...) = $array;

The array's values are copied into the listed variables, in the array's internal order. By default that's the order in which they were inserted, but the sort functions described later let you change that. Here's an example:

$person = array('name' => 'Fred', 'age' => 35, 'wife' => 'Betty');
list($n, $a, $w) = $person;            // $n is 'Fred', $a is 35, $w is 'Betty'

If you have more values in the array than in the list( ), the extra values are ignored:

$person = array('name' => 'Fred', 'age' => 35, 'wife' => 'Betty');
list($n, $a) = $person;                // $n is 'Fred', $a is 35

If you have more values in the list( ) than in the array, the extra values are set to NULL:

$values = array('hello', 'world');
list($a, $b, $c) = $values;            // $a is 'hello', $b is 'world', $c is NULL

Two or more consecutive commas in the list( ) skip values in the array:

$values = range('a', 'e');
list($m,,$n,,$o) = $values;            // $m is 'a', $n is 'c', $o is 'e'

5.5.5. Removing and Inserting Elements in an Array

The array_splice( ) function can remove or insert elements in an array:

$removed = array_splice(array, start [, length [, replacement ] ]);

We'll look at array_splice( ) using this array:

$subjects = array('physics', 'chem', 'math', 'bio', 'cs', 'drama', 'classics');

We can remove the math, bio, and cs elements by telling array_splice( ) to start at position 2 and remove 3 elements:

$removed = array_splice($subjects, 2, 3);
// $removed is array('math', 'bio', 'cs')
// $subjects is array('physics', 'chem');

If you omit the length, array_splice( ) removes to the end of the array:

$removed = array_splice($subjects, 2);
// $removed is array('math', 'bio', 'cs', 'drama', 'classics')
// $subjects is array('physics', 'chem');

If you simply want to delete the elements and you don't care about their values, you don't need to assign the results of array_splice( ):

array_splice($subjects, 2);
// $subjects is array('physics', 'chem');

To insert elements where others were removed, use the fourth argument:

$new = array('law', 'business', 'IS');
array_splice($subjects, 4, 3, $new);
// $subjects is array('physics', 'chem', 'math', 'bio', 'law', 'business', 'IS')

The size of the replacement array doesn't have to be the same as the number of elements you delete. The array grows or shrinks as needed:

$new = array('law', 'business', 'IS');
array_splice($subjects, 2, 4, $new);
// $subjects is array('physics', 'chem', 'math', 'law', 'business', 'IS')

To get the effect of inserting new elements into the array, delete zero elements:

$subjects = array('physics', 'chem', 'math');
$new = array('law', 'business');
array_splice($subjects, 2, 0, $new);
// $subjects is array('physics', 'chem', 'law', 'business', 'math')

Although the examples so far have used an indexed array, array_splice( ) also works on associative arrays:

$capitals = array('USA'           => 'Washington',
                  'Great Britain' => 'London',
                  'New Zealand'   => 'Wellington',
                  'Australia'     => 'Canberra',
                  'Italy'         => 'Rome');
$down_under = array_splice($capitals, 2, 2); // remove New Zealand and Australia
$france = array('France' => 'Paris');
array_splice($capitals, 1, 0, $france);      // insert France between USA and G.B.

Library Navigation Links

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