Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need some help trying to implode my multidimensional array twice. I'm using Joomla 2.5 and it's for a backend component..

Here's what the array is:

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

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

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

                )

        )

)

I've tried using the following code:

$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);

But I'm getting these kind of results:

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

When I'm after:

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

Any help would be greatly appreciated!! :)

share|improve this question
 
Is the number of options always the same ? Just those 3 options ? –  Niloct Oct 17 '12 at 0:08
 
yep, it's always 3 options.. –  Leanne Seawright Oct 17 '12 at 0:15
add comment

1 Answer

up vote 2 down vote accepted

Assuming the main array is $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);
share|improve this answer
 
thanks so much, that worked perfectly :) –  Leanne Seawright Oct 17 '12 at 0:24
 
Look ma, without testing :) Thank you. –  Niloct Oct 17 '12 at 0:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.