3

I have an array in the following form,

Array
(
    [0] => Array
        (
            [emailAddress] => [email protected]
            [subject] => Hi
            [content] => How are you?
            [attachment] => QmFzZTY0IGlzIGFuIGVuY29kaW5nIHNjaGVtZSB1c2VkIHRvIHJlcHJlc2VudCBiaW5hcnkgZGF0YSBpbiBhbiBBU0NJSSBmb3JtYXQuIA==
            [fileName] => Base 64 Encoder
        )
)

How could I have it on the following form:

Array
(
    [emailAddress] => [email protected]
    [subject] => Hi
    [content] => How are you?
    [attachments] => Array
        (
            [attachment] => QmFzZTY0IGlzIGFuIGVuY29kaW5nIHNjaGVtZSB1c2VkIHRvIHJlcHJlc2VudCBiaW5hcnkgZGF0YSBpbiBhbiBBU0NJSSBmb3JtYXQu
            [fileName] => Base 64 Encoder
        )

)

And is there any way to convert the type of [attachments] => Array to [attachments] => Object? like below:

Array
(
    [emailAddress] => [email protected]
    [subject] => Hi
    [content] => How are you?
    [attachments] => Object
        (
            [attachment] => QmFzZTY0IGlzIGFuIGVuY29kaW5nIHNjaGVtZSB1c2VkIHRvIHJlcHJlc2VudCBiaW5hcnkgZGF0YSBpbiBhbiBBU0NJSSBmb3JtYXQu
            [fileName] => Base 64 Encoder
        )

)

I've tried to look number of methods (like array_push, array_splice and etc.) but still can't get it. Hope someone can help me with that. Thanks.

6
  • Do you want to change the array AFTER it's build or change the way it's build the first time? Commented Aug 5, 2015 at 6:49
  • Regarding your 2nd question (array => object), please check this answer: stackoverflow.com/a/1869147/3647441 Commented Aug 5, 2015 at 6:50
  • I want to change it after it's build. Because the original array is retrieved from the excel file. Commented Aug 5, 2015 at 6:51
  • $array[0] ? If you want to generate it that way, then paste how this was generated. Commented Aug 5, 2015 at 6:51
  • @Jigar, I have retrieved the original array from the excel file, that is why it looks like that. Commented Aug 5, 2015 at 6:56

1 Answer 1

6

Try this

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