User Contributed Notes: Array functions
david@preform.dk
31-Jan-1999 06:30
use the max function to get the highest value of an array e.g:
$maxval = max($array);
dmarsh@spscc.ctc.edu
30-Mar-1999 04:18
Also see the 'Variable' section (http://www.php3.org/manual/ref.var.php3) for useful functions (like unset to destroy a variable or element in an array).
r.eckart@upw.de
09-Sep-1999 09:52
It seems to be impossible to write into multidimensional arrays using HTML posts, like:
<PRE>
<select name="thisarray[0][]" size="1" >
<option value="1" >Foo
<option value="2" >Bar
<option value="3" >Blub
</select>
</PRE>
It is possible though to access a specific array position e.g. thisarray[1].
I think it might be useful if multidimensional access would be added.
shevek@anarres.org
11-Feb-2000 06:45
The two essential array functions are
filter and map.
We have map (array_walk).
Can we have filter (see 'grep' in Perl) please?
Is there any other nice way to walk an array and remove items based on some sort of 'validity'?
S.
javier@eos.com.pe
24-Apr-2000 12:41
is there any way to get the _whole_ array using <PRE>next()</PRE> ?
this code would skip the first item:
<PRE>
reset ($arr);
while ($item = next ($arr)) {
//.... do something with $item
}
</PRE>
the problem is that next() preincrements the counter (linke ++p, instead of p++)
i think reset() should go to 'one before' the first item.
sheppard_norfleet@mail.crc.com
28-Apr-2000 05:47
It would be very helpful if there were two additionaly array functions (ar_import,ar_export) that would take any array and encode it(ar_export) into a string so it could be passed via a form, or some other array insensitive transport.
Then, when the encoded string was received it could be decoded back to an array.(ar_import).
jbarrett@iplay,net
30-Apr-2000 03:02
Check out Serialize and Unserialize
bjorn@kulturkonsult.no
04-May-2000 07:57
I want a function to return a two-dimensional array, containing the parsed contents of a text file. The format of the text file is:
<pre>username,f34d76asd6542hfg54387634
username2,d8937f98f73987fkjs3498
</pre>
I want the array to be something like
$userdb[0][user] = username
$userdb[0][pass_hash] = f34d76as...
The number of lines may change. I guess that file() and explode() is the clue. And, yes, it must work in PHP 3. Any ideas would be greatly appreciated.
bjorn@kulturkonsult.no
04-May-2000 12:42
Well, my earlier post regarding two-dimensional arrays weren't supposed to be here in the first place - I realized that later. Anyway, I were able to figure it all out myself. Here's the solution, if anyone's interested:
<pre>
function Users2array() {
$f = fopen("./userdb", "r");
$i = -1;
while (!feof($f)) {
$i++;
$user = explode(",", fgets($f, 4096));
$db[$i]["user"] = $user[0];
$db[$i]["hash"] = trim($user[1]);
}
return $db;
}
</pre>
terry@terrym.com
08-May-2000 11:53
<pre>
There is no mention of "is_array()" on this page.
This is the logical place where some one would look for such a thing, so it should be here.
There is mention of it via the "count()" page in the "See also" section, but it should be here also.
While it is great to have the "veriable functions" section, one who does not know their way around
the documentation is likely to bang head for hours before finding some of this stuff that is in logical,
but non-intuative places.
I guess that what I'm trying to say here is that when the "logical" placement of something is
"non-intuative", there should be a cross link in the place that one would intuatively look in.
Terry
</pre>
jimo@crusade.org
24-May-2000 07:19
How to get array values from a form without using []?
Professional PHP Programming, page 163, suggests that when you have text boxes with the same name, put [] after them, eg:
<INPUT TYPE="text" NAME="oranges[]" VALUE=2>
<INPUT TYPE="text" NAME="oranges[]" VALUE=3>
When you submit the form, you can loop through your oranges to get each value.
However, this mucks up your javascript if you are accessing the value of each orange text box, because when you put [] beside the NAME, javascript no longer recognizes those oranges as an array.
In perl, those oranges without the []would still be received as an array (12), which you can then break apart and access element by element. Is this possible in PHP?