2014-08-30 98 views
-1

我需要一些幫助從數組中提取數據。我有一個數組$prices,其構造像(返回從MySQL):有沒有辦法檢查mysql返回數組中的鍵?

Array 
(
    [0] => Array 
     (
      [pricegroup] => doppia 
      [date] => 2014-09-01 
      [price] => 60 
      [id] => 974 
     ) 

    [1] => Array 
     (
      [pricegroup] => doppia 
      [date] => 2014-09-02 
      [price] => 60 
      [id] => 975 
     ), 
    ... 
) 

,所以我需要得到的價格出給定的日期是數組,目前我在做一個循環,但我認爲這是slow..and我不喜歡循環becouse我必須做同樣的環路每個請求約30倍(對於檢查每天價格)d:

<? 

    $prices = json_decode('[{"pricegroup":"doppia","date":"2014-09-01","price":"60","id":"974"},{"pricegroup":"doppia","date":"2014-09-02","price":"60","id":"975"},{"pricegroup":"doppia","date":"2014-09-03","price":"60","id":"976"},{"pricegroup":"doppia","date":"2014-09-04","price":"70","id":"977"},{"pricegroup":"doppia","date":"2014-09-05","price":"60","id":"978"},{"pricegroup":"doppia","date":"2014-09-06","price":"60","id":"979"},{"pricegroup":"doppia","date":"2014-09-07","price":"60","id":"980"},{"pricegroup":"doppia","date":"2014-09-08","price":"60","id":"981"},{"pricegroup":"doppia","date":"2014-09-09","price":"60","id":"982"},{"pricegroup":"doppia","date":"2014-09-10","price":"60","id":"983"},{"pricegroup":"doppia","date":"2014-09-11","price":"60","id":"984"},{"pricegroup":"doppia","date":"2014-09-12","price":"60","id":"985"},{"pricegroup":"doppia","date":"2014-09-13","price":"60","id":"986"},{"pricegroup":"doppia","date":"2014-09-14","price":"60","id":"987"},{"pricegroup":"doppia","date":"2014-09-15","price":"60","id":"988"},{"pricegroup":"doppia","date":"2014-09-16","price":"60","id":"989"},{"pricegroup":"doppia","date":"2014-09-17","price":"60","id":"990"},{"pricegroup":"doppia","date":"2014-09-18","price":"60","id":"991"},{"pricegroup":"doppia","date":"2014-09-19","price":"60","id":"992"},{"pricegroup":"doppia","date":"2014-09-20","price":"60","id":"993"},{"pricegroup":"doppia","date":"2014-09-21","price":"60","id":"994"},{"pricegroup":"doppia","date":"2014-09-22","price":"60","id":"995"},{"pricegroup":"doppia","date":"2014-09-23","price":"60","id":"996"},{"pricegroup":"doppia","date":"2014-09-24","price":"60","id":"997"},{"pricegroup":"doppia","date":"2014-09-25","price":"60","id":"998"},{"pricegroup":"doppia","date":"2014-09-26","price":"60","id":"999"},{"pricegroup":"doppia","date":"2014-09-27","price":"60","id":"1000"},{"pricegroup":"doppia","date":"2014-09-28","price":"60","id":"1001"},{"pricegroup":"doppia","date":"2014-09-29","price":"60","id":"1002"},{"pricegroup":"doppia","date":"2014-09-30","price":"60","id":"1003"}]',true); 



function getPriceOfDay($day,$from) { 
    $res; $loops = count($from)-1; 

    for ($i = 0; $i <= $loops; $i++) { 

    if($day === $from[$i][date]) { 
     $res .= '<p>Found: '.$from[$i][date].' for '.$from[$i][price].'</p>'; 
    } 
    } 

    return $res; 
} 

echo getPriceOfDay('2014-09-01',$prices); 
echo getPriceOfDay('2014-09-02',$prices); 
echo getPriceOfDay('2014-09-03',$prices); 
echo getPriceOfDay('2014-09-04',$prices); 
echo getPriceOfDay('2014-09-05',$prices); 
echo getPriceOfDay('2014-09-06',$prices); 
echo getPriceOfDay('2014-09-07',$prices); 
echo getPriceOfDay('2014-09-08',$prices); 
echo getPriceOfDay('2014-09-09',$prices); 
echo getPriceOfDay('2014-09-10',$prices); 
echo getPriceOfDay('2014-09-11',$prices); 
echo getPriceOfDay('2014-09-12',$prices); 
echo getPriceOfDay('2014-09-13',$prices); 
echo getPriceOfDay('2014-09-14',$prices); 
echo getPriceOfDay('2014-09-15',$prices); 
echo getPriceOfDay('2014-09-16',$prices); 
echo getPriceOfDay('2014-09-17',$prices); 
echo getPriceOfDay('2014-09-18',$prices); 
echo getPriceOfDay('2014-09-19',$prices); 
echo getPriceOfDay('2014-09-20',$prices); 
echo getPriceOfDay('2014-09-21',$prices); 
echo getPriceOfDay('2014-09-22',$prices); 
echo getPriceOfDay('2014-09-23',$prices); 
echo getPriceOfDay('2014-09-24',$prices); 
echo getPriceOfDay('2014-09-25',$prices); 
echo getPriceOfDay('2014-09-26',$prices); 
echo getPriceOfDay('2014-09-27',$prices); 
echo getPriceOfDay('2014-09-28',$prices); 
echo getPriceOfDay('2014-09-29',$prices); 
echo getPriceOfDay('2014-09-30',$prices); 

echo '<pre>'; 
print_r($prices); 

回答

0

你不需要重複這樣的陣列上,添加所有的日期在一個陣列中並使用in_array

$my_dates = array("2014-09-08", "2014-09-09", ...); 

function getPriceOfDay($my_dates, $values) { 

    $res = ""; 
    foreach($values as $value) { 
    if(in_array($value['date'], $my_dates)) { 
     $res .= "<p>Found: " .$value['date'] . ' for '. $value['price'] . "</p>"; 
    } 
    } 

    return $res; 
} 
getPriceOfDay($my_dates, $prices); 

此時您只需要傳遞數組一次,所有結果將連接到$res

相關問題