I have a multi-dimensional array and php seems to be returning an array instead of a value when I attempt to access the values directly. What am I doing to cause this?
The array looks like (via print_r):
Array (
[12] => Array ( [2016] => 93083.00 [2015] => 85367.00 [2014] => 69726.00 )
[11] => Array ( [2016] => 66730.00 [2015] => 65548.00 [2014] => 77936.00 )
[10] => Array ( [2016] => 84602.00 [2015] => 112070.00 [2014] => 102104.00 )
)
I'm trying to access values using $arrayname[12][2016] but it is returning Array[2016] instead of 93083.
Is this a simple syntax mistake? Or am I missing part of the concept here? I've been trying to work this problem for hours so maybe I'm missing a simple explanation.
EDIT: the syntax above is actually correct, the issue was in the data entry: I was trying to access a key that didn't exist. I tried to delete the post, but can't since it has been answered.
$arrayname[12] = [2016=>93083.00, 2015=> 85367.00 ]
...
...
echo $arrayname[12][2015] ; // prints 85367
i think your array has one more level. try $arrayname[12][2016][2016] .
Related
I have the following array in my Moodle plugin:
Array (
[0] => stdClass Object (
[id] => 32
[sessionid] => 5
[sessiontimezone] => CET
[timestart] => 1464071400
[timefinish] => 1464102000 )
[1] => stdClass Object (
[id] => 33
[sessionid] => 5
[sessiontimezone] => CET
[timestart] => 1465281000
[timefinish] => 1465311600 )
)
How to get the data. Right now, when I make:
$pluginsessiondates = $DB->get_record('plugin_sessions', array('id'=>$sessionid));
I get only data from the frist array [0]
How to get the data from every array key and then the single values? Thanks in advance.
The Moodle DB functions are for getting data out of the database, rather than from an array somewhere inside your plugin.
If you have an array somewhere, then you can get fields from it by writing:
echo $myarray[0]->id;
echo $myarray[1]->id;
etc.
If you are not trying to get data out of an existing array and want, instead, to get it out of the database, then $DB->get_record() will, as its name implies, get you only a single record, whereas $DB->get_records() will get you all the matching records:
$sessions = $DB->get_records('plugin_sessions', array('sessionid' => $sessionid));
foreach ($sessions as $session) {
echo $session->id;
}
I have an array $presettings
print_r($presettings); outputs:
Array (
[0] => stdClass Object (
[uuid] => xxx-1ef8-aac6-xxx-xxx
[name] => etime
[owner] => eder112T Resident
[online] => 1
[channel] => 63b525ae-xxx-3555-1c74-xxx
[owner_uuid] => a371751c-eb77-xxx-899c-xxx
[simname] => Plainfield
[slurl] => xxx://xxx/xx/xx/243/24/xx/?title=xx
[design] => 2
[msg_oftheday] => two
[machine_name] => one
[autopay] =>
[autolog_leave] =>
[autolog_offline] =>
[allow_activation] =>
)
)
and now i want to get a special key:
echo "test output : "$presettings['machine_name']." testend";
outputs "" (nothing).
my method look like this
function preloadSettingsFromMYSQL($ownername,$prim_uuid)
{
$result = $this->instance->get_rows("SELECT * FROM etime_rims where owner='".$ownername."' AND uuid='".$prim_uuid."'");
return $result;
}
$result is an object array, also tried it with $presettings->machine_name, did not work too.
where is the error?
thank you.
If you look closely in the print_r result, you can see that there is a 0 there, meaning that those values aren't directly in $presettings, but actually in the first element of $presettings.
Just try:
$presettings[0]->machine_name
You object is multi dimensional so just add a level
$presettings[0]->machine_name;
As side note you have an object here and not array do don't try to access alues with scopes.
In your array, you have stdClass Object at index 0. You're looking to access the object's variables, which is a slightly different syntax than arrays:
echo $presettings[0]->machine_name;
I'm having trouble handling a nested array I get as result from an API. Print_r($result, true); returns an array looking like this (only much longer):
Array
(
[success] => 1
[return] => Array
(
[sellorders] => Array
(
[0] => Array
(
[sellprice] => 0.00000059
[quantity] => 1076.00000000
[total] => 0.00063484
)
[1] => Array
(
[sellprice] => 0.00000060
[quantity] => 927.41519000
[total] => 0.00055645
)
)
[buyorders] => Array
(
[0] => Array
(
[buyprice] => 0.00000058
[quantity] => 6535.77328102
[total] => 0.00379075
)
[1] => Array
(
[buyprice] => 0.00000057
[quantity] => 118539.39620414
[total] => 0.06756746
)
)
)
)
I need to grab the 3 values (sellprice/buyprice, quantity, total) from the first index of both arrays (sellorders and buyorders) and store them in variables ($sellprice, $sellquantity, $selltotal).
The full example php script I'm using can be found on the bottom of this page. Could anyone help me figure this out?
In php, arrays can more or less have infinite dimensions. You can go deeper within an array's dimensions by adding another set of square brackets. For example,
$array['deep']['deeper']['deepest'][0];
Assuming the indexes in the sellorders and buyorders are the same in your array, you could do
$sellprice = $result['return']['sellorders'][0]['sellprice'];
$sellquantity = $result['return']['sellorders'][0]['quantity'];
$selltotal = $result['return']['sellorders'][0]['total'];
The value should look something like this:
$sellprice = $array['return']['sellorders'][0]['sellprice']
You might want to think about how you iterate over these nested arrays in order to pick out all the values. Furthermore, if you have control over the output I might be better to use a different data structure to enable easier processing.
You can access the values of the nested arrays by adding another pair of square brackets with the appropriate index at the end:
$array['outer']['inner'];
It's up to you to transfer this knowledge to your specific array.
If you want to go thru all of those arrays... try this:
for($i=0; $i<count($array['return']['sellorders']); $i++) {
$this_array = $array['return']['sellorders'][$i];
var_dump($this_array); // it includes sellprice, quantity and total for each entity now.
}
use the same method as above for buyorders as well.
When I do:
print_r($_COOKIE[#PATH]);
It returns a nice array:
Array ( ['threads'] => Array ( [12] => Array ( [50] => 1317830223 [1]
=> 1317785487 [14] => 1317497737 [7] => 1317488004 [9] => 1317485889
[6] => 1317294825 [5] => 1317289974 [4] => 1317288063 ) ) )
But when I do:
print_r($_COOKIE[#PATH]['threads']);
It doesn't print anything... var_dump is also returning NULL.
Whats wrong with that? First print is saying that there is an array like this, but when I try to catch it, script returning null.
Judging by the looks of your array output, your cookie array contains a key named 'threads', not threads. Those quotes are part of the key name, so somewhere you are adding extraneous quotes to the key.
Try print_r($_COOKIE[#PATH]["'threads'"]); to see what I mean.
I'm not even entirely sure how to ask the question, but assuming I have an array in memory like this:
Array (
[0] => Array
(
[0] => Array
(
[0] => 18451
[1] => MDX
)
[1] => Array
(
[0] => 18450
[1] => NSC
)
[2] => Array
(
[0] => 18446
[1] => RL
)
)
)
Is there some existing functionality to turn it into the code version of that array? I have a # of arrays I need to do this for, nested to various degrees. So I imagine I want output something like
$arrayname[] = array(array('18451','MDX'),array('18450','NSC'),array('18446','RL'));
I can write something to do it, but I'd rather not recreate the wheel if there's an existing way to do it.
This may be all you need:
http://us.php.net/var_export
manually loop through the array recursively and create a string like how you want.
var_export could help you there i think. The other option would be looping through it manually :(