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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.