2012-09-02 9 views
0

Utilisez PHPExel pour analyser le fichier XLS. Il fait la structure du tableau suivant:Transformation d'un tableau plat en tableau multidimensionnel

Array(

    [0] => Array(
      [A] => City 1 
      [B] => NULL 
      [C] => NULL 
      [D] => NULL 
      [E] => NULL 
      [F] => NULL 
      [G] => NULL 
     ) 

    [1] => Array(
      [A] => City 1 - Sell Department Phone 
      [B] => NULL 
      [C] => 123-123-123 
      [D] => NULL 
      [E] => NULL 
      [F] => NULL 
      [G] => NULL 
     ) 

    [2] => Array(
      [A] => Stock 1 
      [B] => Stock 1 Address 
      [C] => Stock 1 Phone 
      [D] => NULL 
      [E] => NULL 
      [F] => NULL 
      [G] => NULL 
     ) 

    [3] => Array(
      [A] => Stock 2 
      [B] => Stock 2 Address 
      [C] => Stock 2 Phone 
      [D] => NULL 
      [E] => NULL 
      [F] => NULL 
      [G] => NULL 
     ) 

    // City Name 2 begins 
    [4] => Array(
      [A] => City 2 
      [B] => NULL 
      [C] => NULL 
      [D] => NULL 
      [E] => NULL 
      [F] => NULL 
      [G] => NULL 
     ) 
    [5] => Array(
      [A] => City 2 - Sell Department Phone 
      [B] => NULL 
      [C] => 123-123-124 
      [D] => NULL 
      [E] => NULL 
      [F] => NULL 
      [G] => NULL 
     ) 
    // and so on...  
) 

La structure ci-dessus ne sont pas à portée de main pour le traitement, alors je dois le convertir en la forme suivante:

Array(

    [0] => Array(
    [City 1] => Array(
     [sell] => City 1 - Sell Department Phone 
     [stocks] => Array(
     [Stock 1] => Array(
      [address] => Stock 1 Address 
      [phone] => Stock 1 Phone 
     ) 

     [Stock 2] => Array(
      [address] => Stock 2 Address 
      [phone] => Stock 2 Phone 
     ) 
    ) 
    ) 
) 

    [1] => Array(
    [City 2] => Array(
     [sell] => City 2 - Sell Department Phone 
     [stocks] => Array(
     [Stock 1] => Array(
      [address] => Stock 1 Address 
      [phone] => Stock 1 Phone 
     ) 
     [Stock 2] => Array(
      [address] => Stock 2 Address 
      [phone] => Stock 2 Phone 
     ) 
    ) 
    ) 
) 
) 

Mon code à ce jour:

$output = array(); 

foreach ($sheetData as $key => $data) { 

    // Filter out empty array items 
    $data = array_filter($data); 

    if (count($data) == 1 && $data['A']) { 
    // Get "City" name 
    $output[$key]['city'] = $data['A']; 
    } 

    // Stuck here 
} 

S'il vous plaît aidez-moi cette question

Répondre

1

Vous pouvez essayer d'utiliser array_chunk pour obtenir tous les index es correspondant à une ville dans un tableau. Ensuite, pour ce tableau, construisez manuellement le nouveau tableau.

$final_array = array(); 
$arr = array_chunk($old_array, 4); 
foreach($arr as $key => $city) { 
    $final_array[$key]['sell'] = $city[1]['A']; 
    //and so on... 
} 
Questions connexes