Page 1 of 1

Problem with array_search()

Posted: Sun Nov 28, 2010 4:32 pm
by doughemi
The following snippet does not work properly:

Code: Select all

foreach($classSailedArray as $classSailed){
        $classid=array_search($classSailed, $classarray);
        echo $classSailed . '  '. $classid . '<br>';}
$classSailedArray is an array of the name of classes sailed by a club. $classarray is an array of all possible classes, with the index of each array item being the class ID number.
The first iteration of the loop returns the correct value of the index of $classarray[], but the following iterations return a null.

Is this expected behavior? If so, can anyone suggest an alternative approach to retrieve the id number?

Re: Problem with array_search()

Posted: Sun Nov 28, 2010 7:49 pm
by requinix
What's the output of

Code: Select all

print_r($classSailedArray);
print_r($classarray);

Re: Problem with array_search()

Posted: Sun Nov 28, 2010 9:33 pm
by doughemi
tasairis wrote:What's the output of

Code: Select all

print_r($classSailedArray);
print_r($classarray);
They show the proper contents of each array, with the proper keys.

Re: Problem with array_search()

Posted: Sun Nov 28, 2010 10:30 pm
by Weirdan
doughemi wrote:They show the proper contents of each array, with the proper keys.
"Proper" doesn't say much. Post the output of those statements.

Re: Problem with array_search()

Posted: Mon Nov 29, 2010 5:53 am
by doughemi

Code: Select all

<?php
echo '<pre>';
print_r($classSailedArray);
echo '</pre><pre>';
print_r($classarray);
echo '</pre>;
?>
Array
(
    [0] => AC Class
    [1] => Nirvana
    [2] => US1M
)
Array
(
    [1] => AC Class
    [2] => EC12
    [3] => INF54
    [5] => J
    [6] => M
    [7] => N12
    [8] => Open
    [9] => S/B
    [10] => Soling 50
    [11] => Star45
    [12] => US1M
    [13] => 10 Rater
    [14] => 36/600
    [15] => Soling 1M
    [16] => CR914
    [18] => ODOM
    [20] => Victoria
    [21] => Wheeler
    [22] => RC Laser
    [23] => IOM
    [25] => V32
    [26] => Fairwind 900
    [28] => Seawind
    [29] => US12
    [30] => Footy
    [33] => Vintage
    [34] => Nirvana
    [35] => PeaPod
    [36] => T37
    [40] => MicroMagic
    [42] => RG65
    [43] => Canterbury J
    [99] => Other
)

Re: Problem with array_search()

Posted: Mon Nov 29, 2010 6:43 am
by doughemi
Disregard. The problem isn't there; it's further in the foreach loop in the original script. Sorry for wasting your time.

Re: Problem with array_search()

Posted: Mon Nov 29, 2010 6:52 am
by Weirdan

Code: Select all


   <?php
    $classSailedArray = array (
        0 => 'AC Class',
        1 => 'Nirvana',
        2 => 'US1M',
    );
    $classarray = array
    (
        1 =>'AC Class',
        2 =>'EC12',
        3 =>'INF54',
        5 => 'J',
        6 => 'M',
        7 => 'N12',
        8 => 'Open',
        9 => 'S/B',
        10 => 'Soling 50',
        11 => 'Star45',
        12 => 'US1M',
        13 => '10 Rater',
        14 => '36/600',
        15 => 'Soling 1M',
        16 => 'CR914',
        18 => 'ODOM',
        20 => 'Victoria',
        21 => 'Wheeler',
        22 => 'RC Laser',
        23 => 'IOM',
        25 => 'V32',
        26 => 'Fairwind 900',
        28 => 'Seawind',
        29 => 'US12',
        30 => 'Footy',
        33 => 'Vintage',
        34 => 'Nirvana',
        35 => 'PeaPod',
        36 => 'T37',
        40 => 'MicroMagic',
        42 => 'RG65',
        43 => 'Canterbury J',
        99 => 'Other',
    );
    foreach($classSailedArray as $classSailed){
            $classid=array_search($classSailed, $classarray);
            echo $classSailed . '  '. $classid . '<br>';}

- the above works for me.

Try trimming values of both arrays - just in case there's some trailing space you don't see:

Code: Select all

    function trimmer(&$value) { $value = trim($value); }
    array_walk($classSailedArray, 'trimmer');
    array_walk($classarray, 'trimmer');
    foreach($classSailedArray as $classSailed){
            $classid=array_search($classSailed, $classarray);
            echo $classSailed . '  '. $classid . '<br>';}