Problem with array_search()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
doughemi
Forum Newbie
Posts: 11
Joined: Wed Dec 02, 2009 1:40 pm

Problem with array_search()

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problem with array_search()

Post by requinix »

What's the output of

Code: Select all

print_r($classSailedArray);
print_r($classarray);
User avatar
doughemi
Forum Newbie
Posts: 11
Joined: Wed Dec 02, 2009 1:40 pm

Re: Problem with array_search()

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Problem with array_search()

Post 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.
User avatar
doughemi
Forum Newbie
Posts: 11
Joined: Wed Dec 02, 2009 1:40 pm

Re: Problem with array_search()

Post 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
)
User avatar
doughemi
Forum Newbie
Posts: 11
Joined: Wed Dec 02, 2009 1:40 pm

Re: Problem with array_search()

Post by doughemi »

Disregard. The problem isn't there; it's further in the foreach loop in the original script. Sorry for wasting your time.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Problem with array_search()

Post 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>';}
Post Reply