2013-01-20 50 views
0

嗨,我有一個數組,看起來像這樣不能使用字符串數組offset

Array 
(
     [0] => Array 
       (
         [Marque] => AUDI 
         [Famille] => A4 Avant N1 
       ) 
     [1] => Array 
       (
         [Marque] => AUDI 
         [Famille] => A3 
       ) 
     [2] => Array 
       (
         [Marque] => AUDI 
         [Famille] => A5 
       ) 
     [3] => Array 
       (
         [Marque] => AUDI 
         [Famille] => A4 
       ) 
) 

我需要一個數組,看起來像這樣:

報價

Array 
(
    [AUDI] => Array 
    (
     [0] => A4 Avant N1 
     [1] => A3 
     [2] => A5 
     [3] => A4 
    ) 

) 

所以票價我得到了這個去:

if (isset($_GET['debug'])){ $stri=array(); $i=0; foreach($csv_array as $car){ 
    if (!in_array("$car[Marque]", $stri)) { 
    $stri[] = "$car[Marque]"; 
    } 
    if (!in_array("$car[Famille]", $stri)) { 
    $stri[$i][$car[Marque]][] = "$car[Famille]"; 
    }  $i++; } 

echo "<pre>"; print_r($stri); echo "</pre>"; 

bu牛逼,讓我

致命錯誤:無法使用字符串數組

回答

0

偏移嘗試把在你的數組鍵報價:

if (isset($_GET['debug'])){ $stri=array(); $i=0; foreach($csv_array as $car){ 
    if (!in_array("$car['Marque']", $stri)) { 
    $stri[] = "$car['Marque']"; 
    } 
    if (!in_array("$car['Famille']", $stri)) { 
    $stri[$i][$car['Marque']][] = "$car['Famille']"; 
    }  $i++; } 

echo "<pre>"; print_r($stri); echo "</pre>"; 
+0

「$ car ['Famille']」不是valide php在任何情況下,那不是解決致命錯誤 – user1970938

+0

我改變這個並得到相同的問題 如果(isset($ _ GET ['調試' ])){ $ stri = array(); $ i = 0; foreach($ csv_array as $ car){ if(!in_array($ car ['Marque'],$ stri)){ $ stri [$ i] = $ car ['Marque']; } if(!in_array($ car ['Famille'],$ stri)){ $ stri [$ i] [$ car ['Marque']] [] = $ car ['Famille']; } $ i ++; } echo「

"; print_r($stri); echo "
」; [/ code] – user1970938

0

你必須使用單引號正確拉出值:

$car[Famille] => $car['Famille'] 

[編輯]現在我明白了...

$stri = Array(); 

foreach($csv_array as $car) 
{ 
    if (!array_key_exists("$car['Marque']", $stri)) 
    { 
     $stri["$car['Marque']"] = Array(); 
     $stri["$car['Marque']"][] = "$car['Famille']"; 
    else 
     $stri["$car['Marque']"][] = "$car['Famille']"; 
} 

var_dump($stri); 
+0

thx但這並不能解決致命錯誤 – user1970938

+0

錯誤行嗎? –

+0

好吧,我明白了......現在它應該可以按照你的想法工作。 –

相關問題