Bruce Gust
asked on
How can I update this array?
Here's my array when I do a var_dump:
array (size=3)
'eventdate' => string '2016-08-27' (length=10)
'eventtype' => string 'transaction' (length=11)
'runningbal' => string '32.50' (length=5)
The name of the array is "$event."
I need to update "eventtype." My plan was to do a "foreach" in order to isolate each of the variables and then change "transaction" to "payment."
When I do this:
foreach($event as $sport)
{
echo $sport['eventtype'];
}
I get this: Warning: Illegal string offset 'eventtype' in C:\wamp\www\patient_focus\ arrays.php on line 74
What am I doing wrong?
array (size=3)
'eventdate' => string '2016-08-27' (length=10)
'eventtype' => string 'transaction' (length=11)
'runningbal' => string '32.50' (length=5)
The name of the array is "$event."
I need to update "eventtype." My plan was to do a "foreach" in order to isolate each of the variables and then change "transaction" to "payment."
When I do this:
foreach($event as $sport)
{
echo $sport['eventtype'];
}
I get this: Warning: Illegal string offset 'eventtype' in C:\wamp\www\patient_focus\
What am I doing wrong?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Please use var_export() to print out your test data in the $event array, and post that output here in the code snippet. Then we can see (and code) with exactly the same information you're using.
@Ray sorry, but I don't understand your point: Bruce is just using a wrong code within the foreach loop... He has just to use $event['eventtype'] to get that element.
@brucegust: Here the working code to update array - nothing esoteric :)
You can check it here: http://test.webintenerife.com/array-change.php
@brucegust: Here the working code to update array - nothing esoteric :)
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
$event = array(
'eventdate' => '2016-08-27',
'eventtype' => 'transaction',
'runningbal' => '32.50',
);
echo "Original array:<br>";
echo "<pre>";
var_dump($event);
echo "</pre>";
echo "Going to change event type to payment (\$event['eventtype'] = 'payment';)<br>The array after the change:<br>";
$event['eventtype'] = 'payment';
echo "<pre>";
var_dump($event);
echo "</pre>";
You can check it here: http://test.webintenerife.com/array-change.php
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Ah, I see. I had got the second part of your explanation, but I didn't consider the possibility that the array $event were part of a larger data set... As usual, good point, Ray :)
ASKER
Ray, I thought I had nailed it by doing a var_dump()! Well, nuts! As it turns out, Marco was able to give me what I needed, so I'm good, however...
https://www.experts-exchange.com/questions/28967806/Why-can-I-not-retrieve-this-info-from-my-array.html
...and I make a point of doing var_export.
Thank you!
https://www.experts-exchange.com/questions/28967806/Why-can-I-not-retrieve-this-info-from-my-array.html
...and I make a point of doing var_export.
Thank you!
Open in new window