2012-10-16 28 views
2

我需要一些帮助试图两次内爆我的多维数组。我使用Joomla 2.5,它是一个后端组件..php多维数组,两次内爆?

这里的数组是什么:

Array 
(
    [jform] => Array 
     (
      [options] => Array 
       (
        [colour] => Array 
         (
          [0] => a 
          [1] => d 
         ) 

        [size] => Array 
         (
          [0] => b 
          [1] => e 
         ) 

        [qty] => Array 
         (
          [0] => c 
          [1] => f 
         ) 

       ) 

     ) 

) 

我用下面的代码尝试:

$i=0; 
$optchildArr = array(); 
$optchildArrX = array(); 
foreach ($this->options as $optArr) : 

    $j=0; 
    foreach ($optArr as $arr) : 
     $optchildArrX[] = $arr; 
     $j++; 
    endforeach; 

    $optchildArr[$i] = implode(',',$optchildArrX); 
    $i++; 
endforeach; 

$this->options = implode(';',$optchildArr); 

但我m如果这些样的结果:

[options] => Array 
     (
      [0] => a,d 
      [1] => a,d,b,e 
      [2] => a,d,b,e,c,f 
     ) 

当我后:

[options] => Array 
     (
      [0] => a,b,c 
      [1] => d,e,f 
     ) 

任何帮助将不胜感激! :)

+0

是选项的数量始终是相同的?只有这3个选项? – Niloct

+0

是的,它总是3个选项.. –

回答

2

假设主阵列是$A

function mergeArrays($colour, $size, $qty) { 
    $result = array(); 
    for ($i=0; $i<count($colour); $i++) { 
     //Assuming three arrays have the same length; 
     $result[] = implode(',',array($colour[$i], $size[$i], $qty[$i])); 
    } 
    return $result; 
} 

$result_array = array_map(
    'mergeArrays', 
    $A['jform']['options']['colour'], 
    $A['jform']['options']['size'], 
    $A['jform']['options']['qty'] 
); 

//Check the output from this, should be the output you described. 
var_dump($result_array); 
+0

非常感谢,这工作完美:) –

+0

看看马,没有测试:)谢谢。 – Niloct