ptmuldoon Posted December 20, 2007 Share Posted December 20, 2007 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. } } } Quote Link to comment Share on other sites More sharing options...
Jessica Posted December 20, 2007 Share Posted December 20, 2007 If you search on php.net you'd find: http://php.net/array_search And then by reading the docs you would find: http://www.php.net/manual/en/function.array-keys.php Try working with those. Quote Link to comment Share on other sites More sharing options...
ptmuldoon Posted December 21, 2007 Author Share Posted December 21, 2007 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 Quote Link to comment Share on other sites More sharing options...
rarebit Posted December 21, 2007 Share Posted December 21, 2007 if find, then check i-1 & i+1 if inactive, if so then break else continue Quote Link to comment Share on other sites More sharing options...
Barand Posted December 21, 2007 Share Posted December 21, 2007 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>'; } } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.