Help with php array loading/retrieval ???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bobmct

    Help with php array loading/retrieval ???

    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=arr ay();

    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<=hi ghest_allowed_v alue;$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...
  • Justin Koivisto

    #2
    Re: Help with php array loading/retrieval ???

    bobmct wrote:[color=blue]
    > 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=arr ay();
    >
    > 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<=hi ghest_allowed_v alue;$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...[/color]

    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.co m

    Comment

    Working...