Cannot use string offset as an array in - php

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 }

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 [].

Access the array value by key.
$iReturnMedia = $aMethods[0]['return_media'];
echo $iReturnMedia;

Your accessing it as if it was an object in an array, you do it like:
$iReturnMedia = $aMethods[0]['return_media'];
echo $iReturnMedia;

Try this,
$iReturnMedia = $aMethodList[$sMethodGroup][0]['return_media'];
echo $iReturnMedia;
Try to var_dump($aMethods) . It will be give exactly idea of that array...

find below the code to access the array values -
foreach ($aMethodList as $sMethodGroup => $aMethods) {
echo $aMethods[0]['return_media'];
}

Related

How to get PHP session variable deeply nested inside array?

Have tried a number of permutations to get a value from a PHP session. The session is an array of objects I think with key value pairs. This is the structure as outputted by a key/value foreach loop
Array
(
[laboratory_roster] => Array
(
[employee_entrance] => stdClass Object
(
[step] => employee_entrance
[employee_first_name] => asdfasd
[employee_last_name] => fasdfasdfv
[employee_access_code] => valid
[employee_email] => blah#blah.com
[employee_state_origin] => NY
[employee_kit_for_whom] => employee_kit_for_employee
)
)
)
This is the foreach loop that I wrote to display the output above:
foreach($_SESSION['laboratory_roster']['employee_entrance'] as $key=>$value)
{
// and print out the values
echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
}
What I wish to do is simply assign the value of the innermost value to a variable. Nothing works. Have tried:
$first_name = $_SESSION['employee_entrance']['employee_first_name'];
and this...
$first_name = $_SESSION['employee_entrance'][1];
and this...
$first_name = $_SESSION['laboratory_roster']['employee_entrance']['employee_first_name'];
and this...
$first_name = $_SESSION['laboratory_roster']['employee_entrance']['employee_first_name'][0];
Nothing works! It's probably so simple how to get the innermost value into a PHP variable, but I am not getting it. Thanks for help!
Two ways:
1 Convert to stdClass: $stdClass = json_decode(json_encode($_SESSION)); Then access with [].
2 Convert to array: $array = json_decode(json_encode($_SESSION), true); Then access with ->.
Try:
$first_name = $_SESSION['laboratory_roster']['employee_entrance']->employee_first_name;
As employee_entrance is an object (instance of stdClass) and not an array.

Php array_rand() printing variable name

I have an array that is filled with different sayings and am trying to output a random one of the sayings. My program prints out the random saying, but sometimes it prints out the variable name that is assigned to the saying instead of the actual saying and I am not sure why.
$foo=Array('saying1', 'saying2', 'saying3');
$foo['saying1'] = "Hello.";
$foo['saying2'] = "World.";
$foo['saying3'] = "Goodbye.";
echo $foo[array_rand($foo)];
So for example it will print World as it should, but other times it will print saying2. Not sure what I am doing wrong.
Drop the values at the start. Change the first line to just:
$foo = array();
What you did was put values 'saying1' and such in the array. You don't want those values in there. You can also drop the index values with:
$foo[] = 'Hello.';
$foo[] = 'World.';
That simplifies your work.
You declared your array in the wrong way on the first line.
If you want to use your array as an associative Array:
$foo=Array('saying1' => array (), 'saying2' => array(), 'saying3' => array());
Or you can go for the not associative style given by Kainaw.
Edit: Calling this on the not associative array:
echo("<pre>"); print_r($foo); echo("</pre>");
Has as output:
Array
(
[0] => saying1
[1] => saying2
[2] => saying3
[saying1] => Hello.
[saying2] => World.
[saying3] => Goodbye.
)
Building on what #Answers_Seeker has said, to get your code to work the way you expect it, you'd have to re-declare and initialise your array using one of the methods below:
$foo=array('saying1'=>'Hello.', 'saying2'=>'World.', 'saying3'=>'Goodbye.');
OR this:
$foo=array();
$foo['saying1'] = "Hello.";
$foo['saying2'] = "World.";
$foo['saying3'] = "Goodbye.";
Then, to print the contents randomly:
echo $foo[array_rand($foo)];

how to get a value from a returned JSON array

If i have:
print_r($request->getRequestVars());
that prints out:
Array ( [n] => Coors [s] => 3 )
how would i print out coors?
echo $request->getRequestVars()->n;
is not working, and i've tried several other things. I know this is super basic but it's frustrating me
Try:
<?php
$var = $request->getRequestVars();
echo $var['n'];
?>
I assume that the method $request->getRequestVars(); acutally returns an array.
So, you would have to do something along the lines of:
$foo = $request->getRequestVars();
echo $foo['n'];
Please note that $request->getRequestVars() returns array not object. PHP 5.4 has Function Array Dereferencing if that is what you are running then you can have:
echo $request->getRequestVars()['n'];
else
$v = $request->getRequestVars();
echo $v['n'];

how to get the value from an array

$a=Array ([storage] => [submitted] => 1 [values] => Array ( [q] => googl [op]))
How can I get the value of q from this array $a->q doesn't give me the value. Why?
You use the -> on objects. For an array you need to index the variable like this:
echo $a['values']['q'];
echo $a['values']['q'];
This is 2-d array you can get value like above.
you can also use the foreach to get retrieve values of arrays.
You should use $a['values']['q'].
to get it do this:
echo $a['values']['q'];
You use the -> on objects. For an array you need to index the variable like this:
echo $a['values']['q'];
Check this foreach tutorial for u can get more information

How do I access a string-indexed element of a PHP array?

I have the following array (in php after executing print_r on the array object):
Array (
[#weight] => 0
[#value] => Some value.
)
Assuming the array object is $arr, how do I print out "value". The following does NOT work:
print $arr->value;
print $val ['value'] ;
print $val [value] ;
So... how do you do it? Any insight into WHY would be greatly appreciated! Thanks!
echo $arr['#value'];
The print_r() appears to be telling you that the array key is the string #value.
After quickly checking the docs, it looks like my comment was correct.
Try this code:
print $arr['#value'];
The reason is that the key to the array is not value, but #value.
You said your array contains this :
Array (
[#weight] => 0
[#value] => Some value.
)
So, what about using the keys given in print_r's output, like this :
echo $arr['#value'];
What print_r gives is the couples of keys/values your array contains ; and to access a value in an array, you use $your_array['the_key']
You might want to take a look at the PHP manual ; here's the page about arrays.
Going through the chapters about the basics of PHP might help you in the future :-)

Categories