1

I have these type four separate arrays

Array
(
    [0] => cmbt
    [1] => cmbt
)
Array
(
    [0] => airport
    [1] => guindy
)
Array
(
    [0] => 1000
    [1] => 500
)
Array
(
    [0] => 2500
    [1] => 1000
)

and I need to combine as a result like this

array(
    0 => array(
        'cmbt',
        'airport',
        1000,
        2500
    ),
    1 => array(
        'cmbt',
        'guindy',
        500,
        1000,
    )

Please help me...

1
  • Did you tried any thing.At least you could do it using loop. Commented Jul 10, 2015 at 12:34

3 Answers 3

1

Lets assume your main array is $all_array:

$wanted_array = array(0 => array(), 1 => array());
foreach($all_array as $element)
{
    $wanted_array[0][] = $element[0];
    $wanted_array[1][] = $element[1];
}

To say this doesn't contain any error handling if any malformed $all_array is happening. But it should do what you want

Sign up to request clarification or add additional context in comments.

Comments

1
$final_array = [];

$final_array[] = array_column($your_array, 0); 

$final_array[] = array_column($your_array, 1);

$your_array in this context assumes that there's a big array containing all 4 of your smaller arrays. That should be a problem overall.

It should get you started on a good track.

Comments

1

SPL's MultipleIterators are very useful for this type of task:

$array1 = [
    'cmbt', 'cmbt',
];
$array2 = [
    'airport', 'guindy',
];
$array3 = [
    1000, 500,
];
$array4 = [
    2500, 1000,
];


$mi = new MultipleIterator();
$mi->attachIterator(new ArrayIterator($array1));
$mi->attachIterator(new ArrayIterator($array2));
$mi->attachIterator(new ArrayIterator($array3));
$mi->attachIterator(new ArrayIterator($array4));
$result = [];
foreach($mi as $values) {
    $result[] = $values;
}

var_dump($result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.