2

My array is below i need to arrange like array2 (without use $aa['caption1'] like names directly) arrray1 is

Array
(
    [0] => Array
        (
            [caption1] => Array
                (
                    [0] => gfdhgfjhg
                    [1] => dfhfgjghk
                )

            [caption2] => Array
                (
                    [0] => shgjgh
                    [1] => dhfgkgjl
                )

            [banner_image] => Array
                (
                    [0] => assets/images/page_content/img_namT7.jpg
                    [1] => assets/images/page_content/img_R8mzP.jpg
                )

        )

    [1] => Array
        (
            [heading] => Array
                (
                    [0] => 
                )

            [pragraph] => Array
                (
                    [0] => 
                )

        )
)

arrray2 is(Required format )

Array
(
    [0] => Array
        (
          array('caption1'=>'caption1','caption2'=>'shgjgh','banner_image'=>'assets/images/page_content/img_namT7.jpg'),
          array('caption1'=>'dfhfgjghk','caption2'=>'dhfgkgjl','banner_image'=>'page_content/img_R8mzP.jpg')           
        )

    [1] => Array
        (
           array('heading'=>'','pragraph'=>''),
           array('heading'=>'fgh','pragraph'=>'ghgh'),
        )
)

please any one help me.

1
  • note, there's no such record 'heading'=>'fgh','pragraph'=>'ghgh' within intial array Commented Jun 29, 2017 at 12:00

1 Answer 1

1

The solution using array_keys, array_map and array_combine functions:

// $arr is your initial array
$result = [];
foreach($arr as $v){
    $keys = array_keys($v);
    $data = call_user_func_array('array_map', [null] + $v);
    $result[] = array_map(function($item) use($keys){ 
        return array_combine($keys, $item); 
    }, $data);
}

print_r($result);

The output:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [caption1] => gfdhgfjhg
                    [caption2] => shgjgh
                    [banner_image] => assets/images/page_content/img_namT7.jpg
                )

            [1] => Array
                (
                    [caption1] => dfhfgjghk
                    [caption2] => dhfgkgjl
                    [banner_image] => assets/images/page_content/img_R8mzP.jpg
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [heading] => 
                    [pragraph] => 
                )
        )
)
Sign up to request clarification or add additional context in comments.

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.