Jump to content


Photo

*SOLVED* undestand arrays properly help lol


  • Please log in to reply
3 replies to this topic

#1 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 14 March 2006 - 04:57 PM

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--]
$myarray=array("array1"=>"a","array2"=>"b","array3"=>"c","array4"=>"d","array5"=>"e",);

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

echo $arraying ."<br>";
echo $arrayingshard."<br>";
}

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#2 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 14 March 2006 - 05:27 PM

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.

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

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

while(list($arraing,(arrayingshard)=each($myarray)){
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:
// 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);


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:
$myarray=array("array1"=>"a","array2"=>"b","array3"=>"c","array4"=>"d","array5"=>"e");

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

hope that helps
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#3 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 14 March 2006 - 05:49 PM

[!--quoteo(post=354954:date=Mar 14 2006, 05:27 PM:name=redbullmarky)--][div class=\'quotetop\']QUOTE(redbullmarky @ Mar 14 2006, 05:27 PM) View Post[/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.

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

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

while(list($arraing,(arrayingshard)=each($myarray)){
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:
// 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);
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:
$myarray=array("array1"=>"a","array2"=>"b","array3"=>"c","array4"=>"d","array5"=>"e");

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

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.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#4 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 14 March 2006 - 06:44 PM

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
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users