How to change the structure of an array in PHP

I have an array in the following form,

Array
(
    [0] => Array
        (
            [emailAddress] => abc@gmail.com
            [subject] => Hi
            [content] => How are you?
            [attachment] => QmFzZTY0IGlzIGFuIGVuY29kaW5nIHNjaGVtZSB1c2VkIHRvIHJlcHJlc2VudCBiaW5hcnkgZGF0YSBpbiBhbiBBU0NJSSBmb3JtYXQuIA==
            [fileName] => Base 64 Encoder
        )
)

      

How can I get it in the following form:

Array
(
    [emailAddress] => abc@gmail.com
    [subject] => Hi
    [content] => How are you?
    [attachments] => Array
        (
            [attachment] => QmFzZTY0IGlzIGFuIGVuY29kaW5nIHNjaGVtZSB1c2VkIHRvIHJlcHJlc2VudCBiaW5hcnkgZGF0YSBpbiBhbiBBU0NJSSBmb3JtYXQu
            [fileName] => Base 64 Encoder
        )

)

      

And is there a way to convert the type [attachments] => Array

to [attachments] => Object

? as below:

Array
(
    [emailAddress] => abc@gmail.com
    [subject] => Hi
    [content] => How are you?
    [attachments] => Object
        (
            [attachment] => QmFzZTY0IGlzIGFuIGVuY29kaW5nIHNjaGVtZSB1c2VkIHRvIHJlcHJlc2VudCBiaW5hcnkgZGF0YSBpbiBhbiBBU0NJSSBmb3JtYXQu
            [fileName] => Base 64 Encoder
        )

)

      

I tried to look at the number of methods (like array_push, array_splice, etc.) but still can't get it. Hope someone can help me. Thank.

+3


source to share


1 answer


try it



$result = array();
$result = $arr[0];
$result['attachments'] = new stdClass();
$result['attachments']->attachment = $result['attachment'];
$result['attachments']->fileName = $result['fileName'];
unset($result['attachment']);
print_r($result);

      

+6


source







All Articles