Jump to content

[SOLVED] array_splice issue


glenelkins

Recommended Posts

Hi

 

I have two arrays;

 

$winners = array ( $players_object_array[4] ); // this places the object at positoon 4 in this array in $winners

$round_players[$r] = array of player objects; // $r refers to the round number and there are 3 players in there at keys 0,1,2

 

Now, i have to loop through thr $winners array and add each player to a specific position in the $round_players[$r] array as such

 

$c = 1;

foreach ( $winners as $key => $object ) {

    $position = ( $num_bye + $c ) - 1; // this will give 3 so the object should be placed at position 4

    array_splice ( $round_players[$r], $position, 0, $object );

}

 

but now when i loop throuh $round_players[$r] as $key => $object  i get the following errors

 

Notice: Trying to get property of non-object in C:\PHP\www\morebrackets\test.php on line 205

3=>

 

Notice: Trying to get property of non-object in C:\PHP\www\morebrackets\test.php on line 205

4=>

 

Notice: Trying to get property of non-object in C:\PHP\www\morebrackets\test.php on line 205

5=>

 

whats confusing here is, there should be no array key 4 or 5, it should only be 0,1,2,3.

 

now if i do some manual tests like:

 

$array = array ( 'test1', 'test2','test3' );
array_splice ( $array, 2,0, 'test4' );

 

then loop through $array it works fine and outputs test1, test2, test4, test3

 

 

Link to comment
https://forums.phpfreaks.com/topic/152212-solved-array_splice-issue/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.