1

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.

4 Answers 4

3

You can use:

$var = $array['message'];

In this case, message is your array index.

Sign up to request clarification or add additional context in comments.

1 Comment

In case it is a scalar array you can do $var_scalar = $array['0'];
0

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

$var = $array['message'];

Comments

0

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'];

2 Comments

Please do not recommend things like $arr[key]. This is not future-proof and bad style.
Ok, the last code is what I needed, nothing else. Thanks, Revent.
0

list($var1,$var2)=$array

Like array(), this is not really a function, but a language construct. list() is used to assign a list of variables in one operation. Strings can not be unpacked and list() expressions can not be completely empty.

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.