Jump to content

Recommended Posts

Can someone brake down this array method with your own example please, And exsplain in detail what going on.
My course does so but relly hard.

please include the list and each statements thanks

[!--sizeo:5--][span style=\"font-size:18pt;line-height:100%\"][!--/sizeo--]code corrected sorry[!--sizec--][/span][!--/sizec--]
[code]
$myarray=array("array1"=>"a","array2"=>"b","array3"=>"c","array4"=>"d","array5"=>"e",);

while(list($arraying,$arrayingshard)=each($myarray)){

echo $arraying ."<br>";
echo $arrayingshard."<br>";
}
[/code]
Link to comment
https://forums.phpfreaks.com/topic/4950-solved-undestand-arrays-properly-help-lol/
Share on other sites

when arrays were first explained to me, i was told to imagine a load of boxes lined up either in a line or a grid. each one had a name, a number, or both. each box could be referred to by its name OR number.

this bit sets up 5 boxes, labelled 'array1' through to 'array5'. each one is given start contents, which in this case is a letter from A-E.

[code]
$myarray=array("array1"=>"a","array2"=>"b","array3"=>"c","array4"=>"d","array5"=>"e");
[/code]

the 'while' statement simply kicks off a loop that will keep repeating while the condition remains true.
as for this:

[code]
while(list($arraing,(arrayingshard)=each($myarray)){
[/code]
i really don't know what youre trying to achieve. i even tried it myself out of curiosity but it didnt work, so i'm guessing it was a bit of guesswork on your behalf, or some code that your course teacher gave you. either way, it seems really horrible (although if there's a specific purpose to doing it this way, i'd love to know - anyone?)

but in a nutshell, all an array is is loads of variables grouped by the same name. here's 3 examples:
[code]
// set up a variable (1 box) called 'a', stick the number 5 in it
$a = 5;

// set up 5 boxes. by default, these will be labelled 0-4 (labels start at 0) as i have not specifically named them otherwise, and put some even numbers in it.
// these boxes can be accessed by $a[0] - $a[4]. eg $a[2] contains '6'
$a = array(2, 4, 6, 8, 10);

// do the same as above, but give each box a better name
// these boxes can be accessed with $a['first'] - $a['fifth']
$a = array("first"=>2, "second"=>4, "third"=>6, "fourth"=>8, "fifth"=>10);
[/code]


so if i'm getting this right, and your example needs you to show the box name and the box's value, just use 'foreach', which basically is a nice way of automatically going through each box of an array one by one:
[code]
$myarray=array("array1"=>"a","array2"=>"b","array3"=>"c","array4"=>"d","array5"=>"e");

foreach($myarray as $boxname=>$boxvalue)
{
   echo $boxname.' is '.$boxvalue.'<br>';
}
[/code]

hope that helps
[!--quoteo(post=354954:date=Mar 14 2006, 05:27 PM:name=redbullmarky)--][div class=\'quotetop\']QUOTE(redbullmarky @ Mar 14 2006, 05:27 PM) [snapback]354954[/snapback][/div][div class=\'quotemain\'][!--quotec--]
when arrays were first explained to me, i was told to imagine a load of boxes lined up either in a line or a grid. each one had a name, a number, or both. each box could be referred to by its name OR number.

this bit sets up 5 boxes, labelled 'array1' through to 'array5'. each one is given start contents, which in this case is a letter from A-E.

[code]
$myarray=array("array1"=>"a","array2"=>"b","array3"=>"c","array4"=>"d","array5"=>"e");
[/code]

the 'while' statement simply kicks off a loop that will keep repeating while the condition remains true.
as for this:

[code]
while(list($arraing,(arrayingshard)=each($myarray)){
[/code]
i really don't know what youre trying to achieve. i even tried it myself out of curiosity but it didnt work, so i'm guessing it was a bit of guesswork on your behalf, or some code that your course teacher gave you. either way, it seems really horrible (although if there's a specific purpose to doing it this way, i'd love to know - anyone?)

but in a nutshell, all an array is is loads of variables grouped by the same name. here's 3 examples:
[code]
// set up a variable (1 box) called 'a', stick the number 5 in it
$a = 5;

// set up 5 boxes. by default, these will be labelled 0-4 (labels start at 0) as i have not specifically named them otherwise, and put some even numbers in it.
// these boxes can be accessed by $a[0] - $a[4]. eg $a[2] contains '6'
$a = array(2, 4, 6, 8, 10);

// do the same as above, but give each box a better name
// these boxes can be accessed with $a['first'] - $a['fifth']
$a = array("first"=>2, "second"=>4, "third"=>6, "fourth"=>8, "fifth"=>10);
[/code]
so if i'm getting this right, and your example needs you to show the box name and the box's value, just use 'foreach', which basically is a nice way of automatically going through each box of an array one by one:
[code]
$myarray=array("array1"=>"a","array2"=>"b","array3"=>"c","array4"=>"d","array5"=>"e");

foreach($myarray as $boxname=>$boxvalue)
{
   echo $boxname.' is '.$boxvalue.'<br>';
}
[/code]

hope that helps
[/quote]


Thank you for the example its grate and better then the video training i am using and a lot easer.

I was told to use list and each stsement but i think your code is better, is the statement list and each a statement i realy need to study in a array or is your exmple just as good or better thank you so much.
if you were told to use list and you HAVE to use list, then i'd look at the list syntax and work it out. i've never personally used it as i've never found a practical reason/need to. take a look anyway, you may find you'll need it one day: [a href=\"http://www.php.net/list\" target=\"_blank\"]www.php.net/list[/a]

glad i could help!
cheers
Mark
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.