Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I know this is a silly question, but i'm stucked! I have the following array:

Array ( [type] => 8 [message] => Use of undefined constant hola - assumed 'hola' 
 [file] => C:\wamp\www\WeCode\code.php(29) : eval()'d code [line] => 3 ) 

I want a variable $var to have the string of [message] element. I'm trying to access the array via indexes, but it throws me offset errors! So what can I do? I think is pretty simple, but I'm stuck with that.

share|improve this question

3 Answers

up vote 0 down vote accepted

You can use:

$var = $array['message'];

In this case, message is your array index.

share|improve this answer
This is what i needed. Gosh why I was so blind to see that? Thank you bro. – Carlos Cuellar García Jun 1 at 19:10
Not a problem :) Accept or upvote? Thanks :) – Phil Cross Jun 1 at 19:11
1  
Done, Mr. Cross. Thank You again. – Carlos Cuellar García Jun 1 at 19:13

It's an associative array and you need to access elements by key. In your case:

$var = $array['message'];
share|improve this answer
That is what i needed. Thank you Arturo =) – Carlos Cuellar García Jun 1 at 19:11
You're welcome ! – ArturoO Jun 1 at 19:14

I believe your array syntax is correct, but I like to create associative arrays this way:

$array1 = array( 'type' => 8, 'message' => "Use of undefined constant hola - assumed 'hola'", 'file' => "C:\wamp\www\WeCode\code.php(29) : eval()'d code", 'line' => 3 );

Then since this is an associative array, you can access it like this:

$var2 = $array1['message'];
share|improve this answer
Please do not recommend things like $arr[key]. This is not future-proof and bad style. – str Jun 1 at 19:05
Ok, the last code is what I needed, nothing else. Thanks, Revent. – Carlos Cuellar García Jun 1 at 19:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.