|
Posted by Justin Koivisto on 02/16/06 20:26
bobmct wrote:
> Fellow PHP'ers;
>
> I'm digging myself a hole on this one so I thought it has come to the
> time when I must ask those who know.
>
> I have what should be a simple question for loading, accessing and
> retrieving values stored in a simple one-dimensional array.
>
> I am simply trying to determine the occurrence of numbers as they
> appear in a file. For example lets assume the lowest allowed value is
> (00) and the highest allowed value is (35) and the numbers could occur
> from zero to n times over the course of a period of time.
>
> The problem I am having is referencing the array elements as strings
> but it appears in reality its being references as a vector/index.
>
> I am creating the array with:
>
> $array_name=array();
>
> then I am incrementing the count with:
>
> $X=the number I've parsed from the file;
> $array_name[$X]++;
>
> then at the end I am using a for loop to print it out:
>
> for($X=0;$X<=highest_allowed_value;$X++) {
> print $X, $array_name[$X];
> }
>
> Of course, my field names are different and I think that the
> references to the $X in the brackets are being interpreted as numeric
> vectors but when I try to convert them to a string and use them it
> also fails.
>
> Can someone PLEASE set me straight on the proper technique to
> accomplish this? As much as I've used PHP over the years I usually
> have no issues with arrays except when I need strict numeric indexing.
> Go figure.
>
> Corrections? Suggestions? Advise? Anyone??
>
> Thank you so much.
>
> Signed: Frustrated PHP'er...
Seems fine to me:
<?php
$read=array(
'00', '20', '14', '00', '29',
'14', '35', '20', '03', '06'
);
$new=array();
foreach($read as $x) $new[$x]++;
print_r($new);
?>
Result:
Array
(
[00] => 2
[20] => 2
[14] => 2
[29] => 1
[35] => 1
[03] => 1
[06] => 1
)
The key here is to be sure that the values in the $read array are
strings when pulled from the file. (Use var_dump() to check it out.)
--
Justin Koivisto, ZCE - justin@koivi.com
http://koivi.com
Navigation:
[Reply to this message]
|