Jump to content

PHP Array Help


munozca

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/220576-php-array-help/
Share on other sites

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

)

Link to comment
https://forums.phpfreaks.com/topic/220576-php-array-help/#findComment-1142723
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.