Comparison of programming languages (associative array): Difference between revisions

Content deleted Content added
Update code and link.
Line 774:
 
<source lang=PHP>
$phonebook = array ();
$phonebook['Sally Smart'] = '555-9999';
$phonebook['John Doe'] = '555-1212';
$phonebook['J. Random Hacker'] = '555-1337';
 
// or
 
$phonebook = array (
'Sally Smart' => '555-9999',
'John Doe' => '555-1212',
'J. Random Hacker' => '555-1337',
);
 
// or
 
$phonebook['contacts']['Sally Smart']['number'] = '555-9999';
$phonebook['contacts']['JohnSally DoeSmart']['number'] = '555-12129999';
$phonebook['contacts']['SallyJohn SmartDoe']['number'] = '555-99991212';
$phonebook['contacts']['J. Random Hacker']['number'] = '555-1337';
</source>
Line 796 ⟶ 797:
 
<source lang=PHP>
foreach ($phonebook as $name => $number) {
echo "'Number for ', $name, ': ', $number, "\n";
}
 
// For the last array example it is used like this
foreach($phonebook['contacts'] as $name => $num) {
echo 'Name: ', $name, ', number: ', $num['number'], "\n";
{
echo "<div>Name:{$name}</div>";
echo "<div>Number:{$num['number']}</div>";
}
</source>
Line 810 ⟶ 809:
PHP has an [http://php.net/array extensive set of functions] to operate on arrays.
 
If you want an associative array that can use objects as keys instead of strings and integers, you can use the [http://www.php.net/manual/en/class.splobjectstorage.phpSplObjectStorage SplObjectStorage] class from the Standard PHP Library (SPL).
 
===Pike===