0

I have an array $aMethods whose print_r output is this:

 Array
 (
     [0] => Array
         (
             [pattern] => 
             [return_media] => 1
             [return_name] => 
         )

 )

I'm trying to access 'return_media' with this code:

 $iReturnMedia = $aMethods[0]->return_media;
 echo $iReturnMedia;

Also, when I tried this:

 $iReturnMedia = $aMethods[0]['return_media'];

I get an error stating: Cannot use string offset as an array in...

But it's not working, $iReturnMedia comes back as blank. Could someone tell me what I'm doing wrong here?

EDIT: $aMethods is set in a foreach loop as such:

 foreach ($aMethodList as $sMethodGroup => $aMethods) { //insert code from above }
1
  • 1
    You should turn on warnings; they would have told you exactly what was going wrong. Commented Jun 10, 2012 at 4:28

5 Answers 5

3

You need to use:

$iReturnMedia = $aMethods[0]['return_media'];

The operation -> is for accessing object properties. Since you're just dealing with nested arrays, you need to index them with [].

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

7 Comments

When I do that I get this error: Fatal error: Cannot use string offset as an array in /home/user/public_html/test.php on line 89
@ElliotB., what is line 89 ?
Line 89 is this: $iReturnMedia = $aMethods[0]['return_media'];
Why all the thumbs up? The suggested solution is not working.
People vote silly on SO. @ElliotB. Provide the code where $aMethods is set.
|
0

Access the array value by key.

$iReturnMedia = $aMethods[0]['return_media'];
echo $iReturnMedia;

Comments

0

Your accessing it as if it was an object in an array, you do it like:

$iReturnMedia = $aMethods[0]['return_media'];
 echo $iReturnMedia;

1 Comment

Oops, sorry about that, I edited your comment instead of mine. Feel free to ignore that.
0

Try this,

$iReturnMedia = $aMethodList[$sMethodGroup][0]['return_media'];
echo $iReturnMedia;

Try to var_dump($aMethods) . It will be give exactly idea of that array...

Comments

0

find below the code to access the array values -

foreach ($aMethodList as $sMethodGroup => $aMethods) { 
 echo $aMethods[0]['return_media'];
}

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.