2015-09-21 23 views
-1

對不起,描述錯了我就是這樣。將多維數組隱藏到鍵值中

Array 
(
    [0] => hello[30815][183] 
    [1] => hello[30815][291][28280] 
    [2] => hello[30815][291][28280][custom_math] 
    [3] => hello[30815][472][28273][comment] 
) 

增加對循環

for($i=0; $i<=count($cfn);$i++) 
     { 
      echo $cfn[$i] ; 
      echo '<br>'; 
      echo $cfv[$i]; 
      echo '<br>'; 
     } 

後,我得到了這樣的類型的數組

hello[30815][183] => 28275 
hello[30815][291][28280] => 
hello[30815][291][28280][custom_math] => Noce 
hello[30815][472][28273][comment] => 

我想轉換成

[hello] => Array 
     (
      [183] => 28275 
      [291] => Array 
       (
        [28280] => 
       ) 
      [291] => Array 
       (
        [28280] => Array 
         (
          [custom_math] => Noce 
         ) 
       ) 

      [472] => Array 
       (
        [28273] => Array 
         (
          [comment] => 
         ) 

       ) 
     ) 

我如何在PHP 。我的數組長度不固定.30815 id在所有中都很常見。 我如何解決這個問題。

+1

如果你進入下一個深度的陣列幾乎是一樣的。你有嘗試過什麼嗎? –

+0

@ b0s3請立即檢查。我之前描述過錯誤。 –

+0

$ cfn從哪裏來? – Amarnasan

回答

1
$data //array for processing data 
$productValue // array for values 

$final = array(); 
$i=0; 
foreach ($data as $value) { 
    $firstStrips = explode("[", $value); 
    $process = array(); 
    foreach($firstStrips as $key => $firstStrip){ 
     if($key==0 || $key==1){ 
      continue; 
     } 
     $firstStrip = str_replace("]", "", $firstStrip); 
     $process[] =(string)$firstStrip; 
    } 
    $testArray = convert($process, $productValue[$i]); 
    $final= array_merge_recursive($testArray,$final); 
    $i++; 
} 

function convert($final='', $dataValue) 
{ 
    $out = array(); 
    $cur = &$out; 
    foreach ($final as $value) { 
     $cur[$value." "] = array(); 
     (string)$cur = &$cur[$value." "]; 
     $i++; 
    } 
    $cur = $dataValue; 
    return $out; 
} 

print_r($final); 
+1

謝謝Ankit。現在大部分問題都解決了。 –

1

只需將條目30815分配給變量。

$hello = $hello[30815]; 
+0

請立即檢查。 –

1
$hello = array(); 
$hello[30815][183] = 28275; 
$hello[30815][291][28280] = 4234; 
$hello[30815][291][28280][custom_math] = Noce; 
$hello[30815][472][28273][comment] = 234234; 
$hello = $hello[30815]; 
echo "<pre>"; 
print_r($hello) ; 
echo "</pre>"; 
+0

請再次檢查我的問題 –