Jump to content

[SOLVED] Arrays


Ken2k7

Recommended Posts

Almost all forum scripts have some sort of lang.php which holds an array with output lines.

 

$arr = array();

$arr = array(
'SOMETHING'  =>   "SOMETHING ELSE",
'SOMETHING 1'    =>   "SOMETHING ELSE 1",
.
.
.
);

?>

 

My question is how does => work and if I plan to use something like this too, how would I go about getting the values?

 

Thanks in advance,

Ken

Link to comment
https://forums.phpfreaks.com/topic/83162-solved-arrays/
Share on other sites

It's a way of specifying the key in order to create an associative array instead of the default enumerated array.

 

<?php
$array = array('key' => 'value');
echo $array['key']; // output: value

$array = array('value');
echo $array[0]; //output: value
?>

 

See: http://www.php.net/manual/en/language.types.array.php

Link to comment
https://forums.phpfreaks.com/topic/83162-solved-arrays/#findComment-423002
Share on other sites

Thanks Daniel.

 

Final question on this matter: can I use a loop for this?

 

Edit: okay read the link you placed up Daniel. Question now is: can you explain what the => does in this? And what does $array as $i mean?

<?php

foreach ($array as $i => $value) {
    unset($array[$i]);
}

?>

Link to comment
https://forums.phpfreaks.com/topic/83162-solved-arrays/#findComment-423003
Share on other sites

It signifies the same. The thing to the right of the => operator is the value and to the left is the key. In other words: You are looping through the array $array and at each iteration you can access the value as $value and the key as $i inside the loop.

 

You can read more about foreach here: http://php.net/foreach

Link to comment
https://forums.phpfreaks.com/topic/83162-solved-arrays/#findComment-423005
Share on other sites

Oh okay.

 

Just making sure on one thing here.

 

So rather than having a key, can I use the foreach loop as such:

<?php 
foreach ($array as $i) {
    unset($array[$i]);
}
?>

 

Unless the $value plays a special role in that foreach loop :S Which I have no idea what it does. :(

Link to comment
https://forums.phpfreaks.com/topic/83162-solved-arrays/#findComment-423010
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.