2012-03-21 7 views
1

J'ai un problème avec ce petit problème.php array division/explode

Disons que j'ai un tableau, quelque chose comme ceci:

Array 
    (
     [0] => Size: tiny 
     [1] => Size: small 
     [2] => Size: big 
     [3] => Colour: yellow 
     [4] => Colour: black 
     [5] => Colour: blue 
     [6] => Length: short 
     [7] => Length: long 
    ) 

Ce que je voudrais faire est, passer par chaque champ, Fich je l'ai fait avec foreach, utilisé exploser pour diviser chaque tableau, donc je avoir maintenant le premier attribut dans un tableau (taille, couleur, longueur, etc.) et l'autre valeur dans un autre champ. Résultat Im en espérant est réellement trouver, si tous les attributs (taille, couleur ...) sont les mêmes, ou ils sont différents. Si elles sont différentes, je voudrais thme mettre en tableaux ... sans doute cet exemple retourneraient tableau comme ceci:

Array 
(
    [0] => Array 
     (
      [0] => Size: tiny 
      [1] => Size: small 
      [2] => Size: big 
     ) 

    [1] => Array 
     (
      [0] => Colour: yellow 
      [1] => Colour: black 
      [2] => Colour: blue 
     ) 

    [2] => Array 
     (
      [0] => Length: short 
      [1] => Length: long 
     ) 

) 

Merci pour votre aide et suggestions! Mart

Répondre

1
<?php 
$array = Array 
    (
     '0' => 'Size: tiny', 
     '1' => 'Size: small', 
     '2' => 'Size: big', 
     '3' => 'Colour: yellow', 
     '4' => 'Colour: black', 
     '5' => 'Colour: blue', 
     '6' => 'Length: short', 
     '7' => 'Length: long' 
    ); 

    $map = array(); 
    foreach($array as &$value) 
    { 
     $keyV = explode(': ',$value); 
      $map[$keyV[0]][] = $value; 

    } 

    $final = array_values($map); // throw away the keys 

    var_dump($final); 

donne exactement ce que vous voulez:

array(3) { 
    [0]=> 
    array(3) { 
    [0]=> 
    string(10) "Size: tiny" 
    [1]=> 
    string(11) "Size: small" 
    [2]=> 
    string(9) "Size: big" 
    } 
    [1]=> 
    array(3) { 
    [0]=> 
    string(14) "Colour: yellow" 
    [1]=> 
    string(13) "Colour: black" 
    [2]=> 
    string(12) "Colour: blue" 
    } 
    [2]=> 
    array(2) { 
    [0]=> 
    string(13) "Length: short" 
    [1]=> 
    string(12) "Length: long" 
    } 
} 
+0

merci :) chiborg –

+0

Je vous remercie beaucoup! C'est exactement ce que j'espérais, tu es génial! – Martin

0
<?php 
$array = Array 
    (
     '0' => 'Size: tiny', 
     '1' => 'Size: small', 
     '2' => 'Size: big', 
     '3' => 'Colour: yellow', 
     '4' => 'Colour: black', 
     '5' => 'Colour: blue', 
     '6' => 'Length: short', 
     '7' => 'Length: long' 
    ); 
    foreach($array as &$value) 
    { 
     $value = explode(': ',$value); 
    } 
    $new_array = array(); 
    foreach($array as $value) 
    { 
     $new_array[$value[0]][] = $value[1]; 
    } 
     var_dump($new_array); 

?> 

donnera:

array 
    'Size' => 
    array 
     0 => string 'tiny' (length=4) 
     1 => string 'small' (length=5) 
     2 => string 'big' (length=3) 
    'Colour' => 
    array 
     0 => string 'yellow' (length=6) 
     1 => string 'black' (length=5) 
     2 => string 'blue' (length=4) 
    'Length' => 
    array 
     0 => string 'short' (length=5) 
     1 => string 'short' (length=5)