RE: [PHP] Array

From: Date: Mon, 16 Dec 2002 23:53:50 +0000
Subject: RE: [PHP] Array
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-128490@lists.php.net to get a copy of this message
I'm not sure what is meant in this thread but arrays are pretty simple. PHP allows both numerical and associative arrays (although in all reality they all are associative) but anyway: $arr1 = array('a','b','c', 42 => 'yep'); print $arr1[0]; // a print $arr1[1]; // b print $arr1[2]; // c print $arr1[42]; // yep $arr1[] = 'd'; print $arr1[43]; // d $arr2 = array('a' => 'apple', 'blah'); print $arr2['a']; // apple print $arr2[0]; // blah Anyway the best way to see what values an array actually has is with print_r() or var_dump() print_r($arr2); That code quoted from "PHP Bible" has many issues, it isn't generic at all. Please ignore it or at least keep in mind the context of it. A more generic form of it: function print_keys_and_values($arr) { foreach ($arr as $key => $value) { print "Key: $key - Value: $value<br>\n"; } } Please read the following manual entries on arrays: http://www.php.net/types.array http://www.php.net/foreach Play around with them for awhile and they'll eventually make sense. Regarding the original question of this thread, see print_r() as I blame a typo or something... or maybe it's a variable scope issue. Your example looks fine. Regards, Philip Olson P.s: Essentially numerical == numbered keys associative == worded keys On Mon, 16 Dec 2002, Andy Turegano wrote: > I see. Well, in that case I don't really know what to do. Sorry. > > > On Tue, 17 Dec 2002, Quentin Bennett wrote: > > > Hi, > > > > No I don't think that is right. > > > > $monthschedule["Jun"] is not what is being looked for, but > > $monthschedule[something]="Jun"; > > > > Try doing a loop to see what is in the array. > > > > Example, from PHP Bible, > > > > function print_keys_and_values_each($arr) > > { > > reset($arr); > > while ($cell = each($arr)) > > { > > $c = $cell['value']; > > $k = $cell['key']; > > print ("Key: $k; Value: $c<BR>"); > > } > > } > > > > > > Quentin > > > > -----Original Message----- > > From: Andy Turegano [mailto:aturegano@junker.org] > > Sent: Tuesday, 17 December 2002 10:25 a.m. > > To: Mako Shark > > Cc: php-general@lists.php.net > > Subject: Re: [PHP] Array > > > > > > What you have to do, at least I think, is you have to type: > > $r = $monthschedule["Jun"]; > > > > That is what I think you have to do. The other way you did it was when you > > have a value-only array. > > > > > > On Mon, 16 Dec 2002, Mako Shark wrote: > > > > > I have an array I set up like this: > > > > > > $monthschedule = array(1 => "Jan", 2 => "Feb", 3 => > > > "Mar", 6 => "Jun"); > > > > > > When I try to access them, doing this: > > > $r = $monthschedule[6]; > > > > > > nothing comes up ($r is blank). Any thoughts? There > > > are missing elements (4,5,7-12) in $monthschedule. > > > > > > __________________________________________________ > > > Do you Yahoo!? > > > Yahoo! Mail Plus - Powerful. Affordable. Sign up now. > > > http://mailplus.yahoo.com > > > > > > -- > > > PHP General Mailing List (http://www.php.net/) > > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > The information contained in this email is privileged and confidential and > > intended for the addressee only. If you are not the intended recipient, you > > are asked to respect that confidentiality and not disclose, copy or make use > > of its contents. If received in error you are asked to destroy this email > > and contact the sender immediately. Your assistance is appreciated. > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php >

« previous php.general (#128490) next »