Quick question about an array

Hi all,

I’m a newbie at PHP, I’ve just began learning it through a series of online tutorials :slight_smile: I’ve just learnt about arrays, and decided to look back at some of the scripts I have been working with, when I came across this one and there’s one part of it I don’t understand. (The script does work though, as far as I know). Here’s the script:
[php]

<?PHP $start = 1; $times = 2; $answer = array(); for ($start; $start < 11; $start++) { $answer[$start] = $start * $times; } print $answer[1] . " "; print $answer[4] . " "; print $answer[8] . " "; print $answer[10]; ?>[/php]

It’s probably a really easy answer or something I missed in the tutorial, but the bit i’m stuck on is this part:
[php]$answer[$start] = $start * $times;[/php]

Why is the [$start] bit in there? I thought it would just be $answer = $start * $times; ?

If someone could just clarify why that [$start] bit of coding is there, that would be great :slight_smile:

that is creating a key in the array, with whatever the value of $start is at the time.

say:

[php]$start = ‘5’; // set start
$arr = array(); // empty array
$arr[$start] = ‘BOO!’; // set key and value in array $arr[/php]

that will create an array with the key 5 and the value BOO!

[php]Array(‘5’ => ‘BOO!’)[/php]

let’s say you took $start out of the brackets, what would it do?
[php]$start = ‘5’; // set start
$arr = array(); // empty array
$arr[] = ‘BOO!’; // set value ONLY in array $arr[/php]
would create this array with key 0 and value BOO!:
[php]Array(‘0’ => ‘BOO!’)[/php]
it would do this because you didn’t specify a value for the key, and so it defaults to a number, that increments as the array grows, starting at 0.

so:
[php]
$arr = array(); // empty array
$arr[] = ‘BOO!’; // set value ONLY in array $arr
$arr[] = ‘YEAH!’; // set value ONLY in array $arr
$arr[] = ‘COOL!’; // set value ONLY in array $arr
[/php]

would create this array:
[php]Array(‘0’ => ‘BOO!’, ‘1’ => ‘YEAH!’, ‘2’ => ‘COOL!’)[/php]

when you’ve got them down, check out multidimensional arrays. their just array(s) inside of an array, but you can do some cool stuff with data using them.

I would suggest trying to make some arrays, and then printing them with

[php]echo ‘

’;
var_dump($array);[/php]

It really shows how they are structured. Then it’s just to add a key for each level you want to go down, like $array[‘level1’][‘level2’][‘level3’] = a value at the third level in the array.

I would go to http://us3.php.net/manual/en/function.uasort.php and fool around with the different array functions there are.

Here’s some old code that I had laying around when I was figuring out php arrays (which I modified from a php book).

[php]<?php

 $fruits = array(
    0 => array('fruit' => 'apple', 'weight' => 3),
    1 => array('fruit' => 'pineapple', 'weight' => 1),
    2 => array('fruit' => 'orange', 'weight' => 2),
    3 => array('fruit' => 'pear', 'weight' => 4),
    4 => array('fruit' => 'banana', 'weight' => 4.5)
 );
 
 // Name sorting function:
 function name_sort($x, $y) {
    return strcasecmp($x['fruit'], $y['fruit']);
 }
 
 // Weight sorting function:
 function sort_by_weight($x, $y) {
	return ($x['weight'] > $y['weight']);	 
 }
 
 // Print the array as is:
 echo '<h2>Array As Is</h2><pre>' . print_r($fruits, 1) . '</pre>';
 
 // Sort by name:    
 uasort($fruits, 'name_sort');
 echo '<h2> Array Sorted by Name</h2><pre>' . print_r($fruits, 1) . '</pre>';

 // Sort by weight:    
 uasort($fruits, 'sort_by_weight');
 echo '<h2> Array Sorted by Weight</h2><pre>' . print_r($fruits, 1) . '</pre>';[/php]

There are a lot of neat things you can do with arrays and things that become useful when you get deeper into PHP.

Thanks a lot for the help guys :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service