Avatar of Bruce Gust
Bruce Gust
Flag 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?
PHP

Avatar of undefined
Last Comment
Bruce Gust

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Marco Gasi

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Marco Gasi

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

Ray Paseur

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.
Marco Gasi

@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
Your help has saved me hundreds of hours of internet surfing.
fblack61
SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Marco Gasi

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 :)
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!