Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

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?
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
But if you need to iterate throught the array you can do it this way:
foreach ($event as $key => $value) {
	if ($key == 'eventtype') {
		echo $value;
	}
}

Open in new window

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 :)
<?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>";

Open in new window


You can check it here: http://test.webintenerife.com/array-change.php
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 :)
Avatar of Bruce Gust

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!