note 39643 modified in function.array-splice by vrana

From: Date: Tue, 17 Aug 2004 14:49:42 +0000
Subject: note 39643 modified in function.array-splice by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-80062@lists.php.net to get a copy of this message
While trying to find a way to insert some array into another at some index, array_splice seemed like the function that I wanted. The results weren't quite as expected though. Of course, I can be using it wrong. My desired results turned out be $array1 after calling array_splice() and no tuning got me to have $array3 with what was showing up in $array1. [code] <?php $array1 = array(1, 2, 3, 7, 8, 9); $array2 = array(4, 5, 6); echo '<br />array1 :: '; print_r( $array1 ); echo '<br />array2 :: '; print_r( $array2 ); $index = 3; // $length = count($array1); // $length = -1; $length = 0; // array1 is call by reference it seems $array3 = array_splice($array1, $index, $length, $array2); echo '<br />array1 :: '; print_r( $array1 ); echo '<br />array2 :: '; print_r( $array2 ); echo '<br />array3 :: '; print_r( $array3 ); ?> [/code] Results in [code] array1 :: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 7 [4] => 8 [5] => 9 ) array2 :: Array ( [0] => 4 [1] => 5 [2] => 6 ) array1 :: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 ) array2 :: Array ( [0] => 4 [1] => 5 [2] => 6 ) array3 :: Array ( ) [/code] Cheers, Michael --was-- While trying to find a way to insert some array into another at some index, array_splice seemed like the function that I wanted. The results weren't quite as expected though. Of course, I can be using it wrong. My desired results turned out be $array1 after calling array_splice() and no tuning got me to have $array3 with what was showing up in $array1. [code] $array1 = array(1, 2, 3, 7, 8, 9); $array2 = array(4, 5, 6); echo '<br />array1 :: '; print_r( $array1 ); echo '<br />array2 :: '; print_r( $array2 ); $index = 3; // $length = count($array1); // $length = -1; $length = 0; // array1 is call by reference it seems $array3 = array_splice($array1, $index, $length, $array2); echo '<br />array1 :: '; print_r( $array1 ); echo '<br />array2 :: '; print_r( $array2 ); echo '<br />array3 :: '; print_r( $array3 ); [/code] Results in [code] array1 :: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 7 [4] => 8 [5] => 9 ) array2 :: Array ( [0] => 4 [1] => 5 [2] => 6 ) array1 :: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 ) array2 :: Array ( [0] => 4 [1] => 5 [2] => 6 ) array3 :: Array ( ) [/code] Cheers, Michael http://php.net/manual/en/function.array-splice.php

« previous php.notes (#80062) next »