0

i'm having below multidimensional array,

Array
(
    [address] => Array
        (
            [1] => skdbabsdkbakbskjabsd 
            [2] => skbdskbdkjbskjdbkjsb ksbdk bsdkb
        )

    [contact] => Array
        (
            [1] => 1236547890
            [2] => 654654654646464
        )

)

How to convert / merge into something like this,

Array
(
    [0] => Array
        (
            [1] => skdbabsdkbakbskjabsd 
            [2] => 1236547890
        )

    [1] => Array
        (
            [1] => skbdskbdkjbskjdbkjsb ksbdk bsdkb
            [2] => 654654654646464
        )

)

As mentioned below i'm trying to combine first address 1 with contact 1 similarly address 2 to contact 2 and so on..

Any solution to solve this ?

0

3 Answers 3

0

Here is link for the code

$address= Array
(
    "address" => Array
        (
            "skdbabsdkbakbskjabsd",
            "skbdskbdkjbskjdbkjsb ksbdk bsdkb"
        ),

    "contact" => Array
        (
            1236547890,
            6546546
        )

);


$result= array();
$i = 0;
foreach($address['address'] as $key => $value){
    $result[$i][] = $value;
    $result[$i][] = $address['contact'][$key];
    $i++;
}
echo '<pre>';
  print_r($result);
echo '</pre>';
0
0

If you can rely on the numerical keys, I would do this:

$result = array();
foreach ($address as $key => $value) {
    $result[] = array($address[$key], $contact[$key]);
}
0
$data = array('adress' => array('1' => 'skdbabsdkbakbskjabsd', 
                                '2' => 'skbdskbdkjbskjdbkjsb ksbdk bsdkb'),
              'contact' => array('1' => '1236547890', 
                                 '2' => '654654654646464'));
echo '<pre>';    

print_r($data);

$mergedArray = array();
foreach($data['adress'] as $key => $adress){
    $mergedArray[] = array(1 => $adress, 2 => $data['contact'][$key]);
}


print_r($mergedArray);

echo '</pre>';

Array
(
    [adress] => Array
        (
            [1] => skdbabsdkbakbskjabsd
            [2] => skbdskbdkjbskjdbkjsb ksbdk bsdkb
        )

    [contact] => Array
        (
            [1] => 1236547890
            [2] => 654654654646464
        )

)
Array
(
    [0] => Array
        (
            [1] => skdbabsdkbakbskjabsd
            [2] => 1236547890
        )

    [1] => Array
        (
            [1] => skbdskbdkjbskjdbkjsb ksbdk bsdkb
            [2] => 654654654646464
        )

)

Your Answer

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

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