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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.