0

I am having a little trouble with an xml feed (atom). I am running a for each loop to return prices using simple xml and converting them too arrays which works fine below :-

foreach ($dc->departures->departure as $price)
{
    $lowest = $price->prices->price[5]->asXML();
    $lowestval = array($lowest);
    print_r($lowestval);
}

Which is returning :-

Array ( [0] => 2289 ) 
Array ( [0] => 2207 ) 
Array ( [0] => 2369 ) 
Array ( [0] => 2229 )

My goal is to return only the lowest price, so I can display a Prices From: area. From what I understand I need to use the min() function, however this only works with one array with several values. I've tried array_merge which doesn't seem to work and just returns the same as above. I am a PHP newbie so there maybe something obvious. A kick in the correct direction would be appreciated.

hakre
  • 193,403
  • 52
  • 435
  • 836

3 Answers3

1

Try this. Its working fine

<?php

foreach ($dc->departures->departure as $price)
{ 
$lowest = $price->prices->price[5]->asXML();
$lowestval[] = $lowest; 
}

$min = min($lowestval);
echo $index = array_search($min, $array);
?>
Padmanathan J
  • 4,614
  • 5
  • 37
  • 75
  • Thanks Nathan this seems to have done the trick almost I couldnt get it to show any results with the bit above but the lowestval[] seemed too have done the trick grouping everything in one array at the end. I just cant get it to display value lower than 4 digits. Please see amended code I did above. Thanks again its already been a great help! – user1749305 Jul 15 '13 at 10:49
0
$data = array();
$data[] =Array (0 => 2289 ) ;
$data[] = Array ( 0 => 2207 ) ;
$data[] = Array ( 0 => 2369 ) ;
$data[] = Array ( 0 => 2229 );

array_multisort($data);
$first = array_shift($data);
var_dump($first);  // 2207
Cups
  • 6,901
  • 3
  • 26
  • 30
0

You can also use 'sort()' function to sort an array value. Here is an example with some extra value as well merge array.

    $arry1 = array(
    array(5),
    array(10000),
    array(2289),
    array(2288),
    array(2207),
    array(2369),
    array(2229),
    array(5421),
    array(541) 
    );


   $arry2 = array(
       array(456789),
       array(54564)
   );
   $arry1 = array_merge($arry1,$arry2);

   sort($val);
   echo '<pre>';
   print_r($val);
   echo '</pre>';

then you can use first element of an array as min value.

    echo $arry1[0][0];
Rakesh
  • 756
  • 8
  • 10