Jump to content

Searching Arrays


ptmuldoon

Recommended Posts

I'm still green at php, but I have an array that outputs the following data:

 

Array
(
    [0] => Array
        (
            [id] => 2
            [player] => player_2
            [state] => inactive
        )

    [1] => Array
        (
            [id] => 3
            [player] => player_3
            [state] => inactive
        )

    [2] => Array
        (
            [id] => 4
            [player] => player_4
            [state] => trading
        )

    [3] => Array
        (
            [id] => 5
            [player] => player_5
            [state] => inactive
        )

    [4] => Array
        (
            [id] => 6
            [player] => player_6
            [state] => inactive
        )
)

 

If you look above you'll see that player_4 has a state = 'trading'

 

Now, I want to be able search the array, and have it tell me which player has a state of 'inactive' immediately before and immediately after the 'trading' player.  This what I have so far, but i'm sure on how to proceed.

 

$player_data = array();
while ($row = mysql_fetch_assoc($result)) {
    $player_data[] = $row;
}  

print "<pre>";
print_r($player_data);  //helpful to review the array.
print "</pre>";  

for ($row = 0; $row < mysql_num_rows($result); $row++)
{
    foreach($player_data[$row] as $key => $value)
    {    
    // Get the Current Player Info
    if($player_data[$row]['state'] == 'trading'){
	    $current_id = $player_data[$row]['id'];
	    $current_player = $player_data[$row]['player'];
 		echo "<li>".$value."</li>";
		}

		// Get the Next Player Info. Unsure What do use here.
   		

		// Get the Last Player Info. Unsure What do use here.
      	} 
    }	
}

Link to comment
https://forums.phpfreaks.com/topic/82581-searching-arrays/
Share on other sites

Thanks for the help.  I'm going to try and work on it this weekend hopefully.

 

But in quickly looking at both functions, can you identify a specific row of the array with either array_search or array-keys?  I was thinking/believing that to get the 'inactive' player immediately above and beneath the 'trading' player, that I would somehow have to compare to the 'trading' player?  Since there is more than 1 'inactive' player, won't array_search return a result of all the other players, and not just the specific one immediately above and below?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/82581-searching-arrays/#findComment-420887
Share on other sites

try

<?php
$data = array
(
    '0' => array
        (
            'id' => 2,
            'player' => 'player_2',
            'state' => 'inactive'
        ),

    '1' => array
        (
            'id' => 3,
            'player' => 'player_3',
            'state' => 'inactive'
        ),

    '2' => array
        (
            'id' => 4,
            'player' => 'player_4',
            'state' => 'trading'
        ),

    '3' => array
        (
            'id' => 5,
            'player' => 'player_5',
            'state' => 'inactive'
        ),

    '4' => array
        (
            'id' => 6,
            'player' => 'player_6',
            'state' => 'inactive'
        )
);
$k = count($data);
for ($i=0; $i<$k; $i++)
{
    if ($data[$i]['state']=='trading')
    {
        echo $i > 0 ? $data[$i-1]['player'] : 'none', '<br>';
        echo $data[$i]['player'], ' trading', '<br>';
        echo  $i < $k-1 ? $data[$i+1]['player'] : 'none', '<br>'; 
    }
}
?>

Link to comment
https://forums.phpfreaks.com/topic/82581-searching-arrays/#findComment-420912
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.