2017-03-14 1 views
0

Je veux prendre toutes les cordes avant : et les affecter comme clé correspondantesupprimer chaîne spécifique du tableau et assignent comme clé

Ceci est mon tableau:

Array 
(
    [0] => Array 
     (
      [0] => FileName:index.php 
      [1] => Description:Display the home page 
      [2] => Version:1.1 
      [3] => Author: Developer 
      [4] => Author URI: https://developer.blogspot.com 
     ) 

) 

Je veux dans cette format:

Array 
    (
     [0] => Array 
      (
       ['FileName'] => index.php 
       ['Description'] => Display the home page 
       ['Version'] => 1.1 
       ['Author'] => Developer 
       ['Author URI'] => https://developer.blogspot.com 
      ) 

    ) 

Merci à tous pour votre aide.

+0

Qu'avez-vous déjà essayé? –

+0

Je joue avec foreach et array_combine. Rien ne fonctionne. –

+0

Pouvez-vous s'il vous plaît coller votre code source? –

Répondre

0

Vous pouvez essayer cette fonction:

<?php 

$array = Array 
(
    Array 
     (
      0 => 'FileName:index.php', 
      1 => 'Description:Display the home page', 
      2 => 'Version:1.1', 
      3 => 'Author: Developer', 
      4 => 'Author URI: https://developer.blogspot.com' 
     ) 
); 

$array = process_array($array); 
var_dump($array); 

function process_array(array $array) { 
    $datas = array(); 
    foreach($array as $key => $value) { 
     foreach($value as $text) { 
      $parts = explode(':', $text); 
      $newKey = array_shift($parts); 
      $datas[$key][$newKey] = implode(':', $parts); 
     } 
    } 

    return $datas; 
} 
+0

merci Guillaume, votre code fonctionne comme un charme. –

+0

Heureux de vous aider ;-) –

0

Essayez celui-ci.

<?php 
$arr = array("FileName:index.php", 
      "Description:Display the home page",  
      "Version:1.1", 
       "Author: Developer", 
      "Author URI: https://developer.blogspot.com" 
     ); 
$finalArr = []; 
for($i=0;$i<count($arr);$i++) { 
    $newKey = explode(':',$arr[$i]); 
    $finalArr[$newKey[0]] = $newKey[1]; 
} 

echo '<pre>'; print_r($finalArr); 


Array 
(
    [FileName] => index.php 
    [Description] => Display the home page 
    [Version] => 1.1 
    [Author] => Developer 
    [Author URI] => https 
)