1

I have the following array output:

Array ( [0] => stdClass Object ( [name] => asdasd [email] => [email protected] [message] => asdasd ) ) 

Which is given by the following code:

if(isset($_POST['emailContent'])){ $mail = new PHPMailer; $emailContent = $_POST['emailContent']; $emailContent = json_decode($emailContent); print_r($emailContent); } 

I need to access the actual object and get the info from name, email and message.

How can I do this?

I have tried

$name = $emailContent[name]; $name = $emailContent['name']; 

but no luck so far. Any ideas what I might be doing wrong?

2
  • 2
    try $emailContent[0]->name Commented Jan 14, 2016 at 7:11
  • tried $emailContent.name; or $emailContent->name;? Commented Jan 14, 2016 at 7:11

6 Answers 6

2

Try the following solution:

$name = $emailContent[0]->name; $email = $emailContent[0]->email; $message = $emailContent[0]->message; 
Sign up to request clarification or add additional context in comments.

2 Comments

This does it. Not sure how this happened.. I've never worked with arrays like this before. Is it because I used JSON_DECODE?
@codeninja: if you need array from json_decode, use second parameter. like json_decode($emailContent, true).
1

Your Object array has a value on 0th index so you need to access it by specifying index as , $emailContent[0]->name; to get name value here 0 is index . Similar for email and message as , $emailContent[0]->email; $emailContent[0]->message;

Comments

0

if the array you have var_dumped is $emailContent you can retrieve the data as below :-

$emailContent[0]->name $emailContent[0]->email $emailContent[0]->message 

Comments

0

Try this:

foreach( $emailContent as $index => $content ) $name = $content[$index]->name; $email = $content[$index]->email; echo 'Name: ' . $name .' | email: ' . $email.'<br>'; } 

1 Comment

It'd be -> not [] for proceeding properties of the object. ($emailContent[0]->name...etc)
0

You have to get the object from the array first and then use -> to access the name.

Try this $name = $emailContent[0]->name;

Comments

0

Try this instead:

$name = $emailContent[0]->name; $email = $emailContent[0]->email; $message = $emailContent[0]->message; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.