2012-03-28 55 views
0

我有兩個數組。數組操作的想法

$Array1 = array("Maza", "Nissan","Tacoma","Cobalt","Explorer"); 

和第二陣列是

array(
    (int) 0 => array(
     (int) 0 => 'In Stock', 
     (int) 1 => 'Cars' 
    ), 
    (int) 1 => array(
     (int) 0 => 'In stock', 
     (int) 1 => 'Cars/Toyota/Tacoma' 
    ), 
    (int) 2 => array(
     (int) 0 => 'Out of Stock', 
     (int) 1 => 'Cars/Toyota/Celica' 
    ), 
    (int) 3 => array(
     (int) 0 => 'In Stock', 
     (int) 1 => 'Cars/Ford/Fusion' 
    ), 
    (int) 4 => array(
     (int) 0 => 'Out of Stock', 
     (int) 1 => 'Cars/Ford/Explorer' 
    ), 
    (int) 5 => array(
     (int) 0 => 'In Stock', 
     (int) 1 => 'Cars/Chevy/Cobalt' 
    ), 
    (int) 6 => array(
     (int) 0 => 'In Stock', 
     (int) 1 => 'Cars/Nissan' 
    ) 

) 

現在我想看到基於第一和第二陣列上什麼車庫存與否。所以對於Cobalt來說,它會讓我回到In Stock,就像Explorer一樣,它會讓我斷貨。對於馬自達來說,它可以重新調整「不要攜帶」。在那裏,我很困惑的是不管用斜線「/」來爆炸數組[1],然後查看它。有沒有更簡單/更快的方法來做到這一點。感謝

+0

你嘗試過什麼?另外,如果資源管理器有兩個實例表示「缺貨」和「庫存」,應如何返回「缺貨」? – Josh 2012-03-28 19:55:31

+0

你*有*擁有第二個數組的方式嗎?對於汽車可用性而言,是否有一種不同的數據模型可供選擇?您多久查看一次汽車的可用性,以及您多久一次「重新裝載」第二陣列的信息? – Irfy 2012-03-28 19:59:21

+0

固定資源管理器問題並編輯它 – Autolycus 2012-03-28 20:01:03

回答

2

試試這個:

$cars = array("Maza", "Nissan","Tacoma","Cobalt","Explorer"); 
$stocks=array(
    (int) 0 => array(
     (int) 0 => 'In Stock', 
     (int) 1 => 'Cars' 
    ), 
    (int) 1 => array(
     (int) 0 => 'In stock', 
     (int) 1 => 'Cars/Toyota/Tacoma' 
    ), 
    (int) 2 => array(
     (int) 0 => 'Out of Stock', 
     (int) 1 => 'Cars/Toyota/Celica' 
    ), 
    (int) 3 => array(
     (int) 0 => 'In Stock', 
     (int) 1 => 'Cars/Ford/Fusion' 
    ), 
    (int) 4 => array(
     (int) 0 => 'Out of Stock', 
     (int) 1 => 'Cars/Ford/Explorer' 
    ), 
    (int) 5 => array(
     (int) 0 => 'In Stock', 
     (int) 1 => 'Cars/Chevy/Cobalt' 
    ), 
    (int) 6 => array(
     (int) 0 => 'In Stock', 
     (int) 1 => 'Cars/Nissan' 
    ) 

); 
$output=array(); 
foreach ($cars as $car) { 
    foreach ($stocks as $stock) { 
    if(in_array($car, explode('/', $stock[1]))){ 
     $output[$car]=$stock[0]; 
     break; 
     }else{$output[$car]='Do not care';} 
} 
} 
echo '<pre>'; 
print_r($output); 

這將打印:

Array 
(
    [Maza] => Do not care 
    [Nissan] => In Stock 
    [Tacoma] => In stock 
    [Cobalt] => In Stock 
    [Explorer] => Out of Stock 
) 
2

也許

$Array1 = array("Maza", "Nissan","Tacoma","Cobalt","Explorer"); 
$secondarray = array(
    array('In Stock', 'Cars'), 
    array('In stock', 'Cars/Toyota/Tacoma'), 
    array('Out of Stock', 'Cars/Toyota/Celica'), 
    array('In Stock', 'Cars/Ford/Explorer'), 
    array('Out of Stock', 'Cars/Ford/Explorer'), 
    array('In Stock', 'Cars/Chevy/Cobalt'), 
    array('In Stock', 'Cars/Nissan')); 
function findcar($car, $secondarray) { 
    for($c = 0; $c <= count($secondarray); $c++) { 
     if(strpos(strtolower($secondarray[$c][1]), strtolower($car)) > 0) { 
       return $secondarray[$c][0]; 
     } 
    } 
} 

echo findcar($Array1[1], $secondarray); 

這將打印出In Stock,如果汽車沒有被發現它只是沒有返回。

+0

是和添加數組:D – Dion 2012-03-28 19:57:51

+0

什麼是$汽車?數組? – Autolycus 2012-03-28 20:01:48

+0

什麼是次要的? – Autolycus 2012-03-28 20:01:56