Jump to content
MakeWebGames

out come error


modernmafia

Recommended Posts

@modernmafia

I'll write something that's more pleasing to the eyes for you, regarding arrays and the key indexing problem you're having.

<?php
/*
* View outcome; http://codepad.org/0rxosNoK
*/
// Initiate array
// Add elements (key => value)
$cars = array(0 => "Car1",
             1 => "Car2",
             2 => "Car3",
             3 => "Car4",
             4 => "Car5",
             6 => "Car6");

print_r( $cars );

?>

 

As you see, it's easier to index the elements, using numbers, referred to as keys.

As for the random selection, why not do something like;

/*
* View outcome; http://codepad.org/27qAS5EW
*/
<?php

// Initiate array
// Add elements (key => value)
$cars = array(0 => "Car1",
             1 => "Car2",
             2 => "Car3",
             3 => "Car4",
             4 => "Car5",
             6 => "Car6");

// Get random element from the array
$won = array_rand( $cars, 2 );
// Output it
echo $cars[$won[0]];

?>

 

@Djkanna

Arrays start from 0 not from 1.

I believe this to be not entirely true, as if you look at the above code, I could have started the key at 1, not 0.

Link to comment
Share on other sites

@Djkanna

I believe this to be not entirely true, as if you look at the above code, I could have started the key at 1, not 0.

Specifed keys can be anything you like.

However not specifying keys (See MM's OP code), or using some function that doesn't leave the keys in-tact will regard my statement to be entirely true.

As an example we'll take your cars array from the second code snippet(bumped up by 1.).

$cars = array(1 => "Car1",              
             2 => "Car2",             
             3 => "Car3",             
             4 => "Car4",              
             5 => "Car5",             
             6 => "Car6");

Dump:

array   
1 => string 'Car1' (length=4) 
2 => string 'Car2' (length=4) 
3 => string 'Car3' (length=4) 
4 => string 'Car4' (length=4) 
5 => string 'Car5' (length=4) 
6 => string 'Car6' (length=4)

As expected;

Now lets do something, you may come across from time to time, the sort function.

sort($cars);

Dump:


array   
0 => string 'Car1' (length=4)   
1 => string 'Car2' (length=4)   
2 => string 'Car3' (length=4)   
3 => string 'Car4' (length=4)   
4 => string 'Car5' (length=4)   
5 => string 'Car6' (length=4)

Conclusion; not specifying keys or using something that doesn't respect said keys will mean your arrays will start from zero.

Link to comment
Share on other sites

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.   Paste as plain text instead

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