munozca Posted December 3, 2010 Share Posted December 3, 2010 I need assistance in creating a new array from the following array: Array ( [Diesel] => Diesel [691.400000572205] => 691.400000572205 [Off-Road Diesel] => Off-Road Diesel [1104.20000505447] => 1104.20000505447 ) I need to new array to look like this: Array ( [Diesel] => 691.400000572205 [Off-Road Diesel] => 1104.20000505447 ) What's the best way to go about this? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/220576-php-array-help/ Share on other sites More sharing options...
taquitosensei Posted December 3, 2010 Share Posted December 3, 2010 nm I was wrong Quote Link to comment https://forums.phpfreaks.com/topic/220576-php-array-help/#findComment-1142640 Share on other sites More sharing options...
Pikachu2000 Posted December 3, 2010 Share Posted December 3, 2010 The best thing to do would be to change the way the array is constructed to begin with, but I'm guessing that isn't possible. As long as the array is always the same structure, with an element containing the name followed by an element containing the value, this certainly ain't pretty, but it works . . . <?php $data = array ( 'Diesel' => 'Diesel', '691.400000572205' => 691.400000572205, 'Off-Road Diesel' => 'Off-Road Diesel', '1104.20000505447' => 1104.20000505447 ); $num_array = array_values($data); foreach( $num_array as $k => $v ) { if( $k % 2 == 0 ) { $reordered[$v] = $num_array[$k+1]; } } echo '<pre>'; print_r($reordered); echo '</pre>'; ?> Returns: Array ( [Diesel] => 691.400000572 [Off-Road Diesel] => 1104.20000505 ) Quote Link to comment https://forums.phpfreaks.com/topic/220576-php-array-help/#findComment-1142723 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.