How to create an object out of array of strings in php

438

I'm new tophp. I just want to create an object from array of strings.

I want these array of strings:

$arrEmails = [
   '[email protected]',
   '[email protected]',
   '[email protected]'
];

to be put in an object to be accessed like,$emails = $objEmails->email

How is it possible?

556

Answer

Solution:

Just create your empty object and just simply add that property:

$objEmails = new stdClass; // initialize an empty object
$objEmails->email = $arrEmails; // declare the property
37

Answer

Solution:

You should have a class like this:

class ObjEmails
{
    $emails = array();
    public function __construct()
    {
        $this->emails = array(
          '[email protected]',
          '[email protected]',
          '[email protected]'
        );
    }

}

$objEmails = new ObjEmails;
$emails = $objEmails->email;
615

Answer

Solution:

You can also try with -

$arrEmails = [
   '[email protected]',
   '[email protected]',
   '[email protected]'
];
$objEmails = (object) $arrEmail;

People are also looking for solutions to the problem: php - Execute link without clicking

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.