2016-04-12 34 views
-5

Merhaba Görmek için karmaşık bir dizi basit yapmak istiyorum.PHP, karmaşık geniş bir dizinin nasıl yapıldığını basit

Ancak, ben bir döngü yapma tür şaşırıp

i

Ben

'collection' => [ 
    (int) 0 => [ 
     'type' => 'col', 
     'name' => '2016 F/W', 
     'url' => 'blablabla' 
    ], 
    (int) 1 => [ 
     'type' => 'col', 
     'name' => '2015 F/W', 
     'url' => 'blablabla' 
    ], 
    (int) 2 => [ 
     'type' => 'project', 
     'name' => '2015 F/W', 
     'url' => 'blablabla' 
    ] 
] 

basit bu diziyi istiyorum ve bu

gibi bu diziyi istediğiniz dizinin ilgili yanlış olduğunu görmek, lütfen
[ 
(int) 0 => [ 
    'type' => 'col', 
    'type2' => [ 
     (int) 0 => [ 
      'name' => '2016 F/W', 
      'image' => [ 
       (int) 0 => [ 
        'url' => null 
       ], 
       (int) 1 => [ 
        'url' => null 
       ] 
      ] 
     ], 
     (int) 1 => [ 
      'name' => '2015 F/W', 
      'image' => [ 
       (int) 0 => [ 
        'url' => null 
       ] 
      ] 
     ] 
    ] 
], 
(int) 1 => [ 
    'type' => 'project', 
    'type2' => [ 
     (int) 0 => [ 
      'name' => '2015 F/W', 
      'image' => [ 
       (int) 0 => [ 
        'url' => null 
       ] 
      ] 
     ], 
    ] 
] 

Lütfen Yardım! Neredeyse çıldırıyorum!

+0

orijin ilk ve sonuç son olarak –

+0

bunun nasıl görüneceğini öğrenmek için döngüyü yapıyorsunuz, bazı kodları vermeniz gerekiyor çünkü insanlar ne yaptığınızı anlayamıyor. –

+0

İkincisi, birden çok resim URL'sini tek bir dizeden nasıl oluşturuyor? – Justinas

cevap

1

Sonuç dizisinin çok kullanışlı görünmediğini söylemeliyim. Gerçekten result dizisinin url kısmı anlamadı

$arr = array(
     'collection' => array(
      array(
       'type' => 'col', 
       'name' => '2016 fw', 
       'url' => 'blabla1' 
      ), 
      array(
       'type' => 'col', 
       'name' => '2015 F/W', 
       'url' => 'blabla2' 
      ), 
      array(
       'type' => 'project', 
       'name' => '2015 F/W', 
       'url' => 'blabla_project' 
      ), 

     ), 
    ); 

    // $types array will hold a list of all types. type in this 
    // example is 'col' and 'project'. 
    // we will use $types to find if type X was already added to the 
    // result array. 
    $types = array(); 

    // Every type X will be in the result array in key K, So $typesMap 
    // will map between the type to the key in the result array. 
    // Example: type X is located in the result array under key K 
    // So I can find the type X data by looking at the 
    // $resultArr[K] 
    $typesMap = array(); 
    $typeIndx = 0; 

    // This is the result array. 
    $resultArr = array(); 

    // run over all collections 
    foreach($arr['collection'] as $collection){ 

     // get the current type. e.g 'col' 
     $type = $collection['type']; 

     // Search the $types array for 'col' to see if it was defined 
     // already. 
     if(!in_array($type, $types)) { 
      // if it wasn't defined then define it now: 

      // add it to the $types array so that we can find it on 
      // the next run. 
      $types[] = $type; 

      // define the key (K) that it will be in the $resultArr 
      $typesMap[$type] = $typeIndx; 

      // Create the 'col' key in the result array. 
      $resultArr[$typeIndx] = array('type' => $type, 'type2' => array()); 
      $typeIndx++; 
     } 

     // get the key for type $type. 
     // remember? I can use the $typesMap to find the type's index 
     // in the result array. 
     $currentIndx = $typesMap[$type]; 

     // add the extra data to the 'type2' (not a very good name for 
     // this) 
     $resultArr[$currentIndx]['type2'][] = array(
      'name' => $collection['name'], 
      'image' => $collection['url'] 
     ); 

    } 

Sadece bu yapılabilir nasıl almak benim vereceğiz Bu görevin amaçları hakkında çok fazla bilgi yok olduğu için .. ..

+0

Teşekkürler, bana çok yardımcı oldu ama çok zayıflamıştım. –

+0

Bu harika çalışıyor ancak kodunuzu okumakta zorlanıyorum. Kodunu hiç anlamıyorum. Benim için gelişmiş bir ilişkisel dizi egzersiz var mı? kodunuzu anlamak için? teşekkürler –

+1

bazı yorumlar güncellendi. Deneyin ve hata ayıklamanız gerekir. xdebug? – carmel

İlgili konular