Solved

How can I update this array?

Posted on 2016-09-04
7
53 Views
Last Modified: 2016-09-05
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?
0
Comment
Question by:brucegust
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 450 total points
ID: 41783647
You don't need to use foreach: just use
echo $event['eventtype'];

Open in new window

0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41783650
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

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41783651
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41783660
@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
1
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 50 total points
ID: 41783729
Hi, Marco.  There have been various iterations of this question.  Some of them have used multidimensional arrays.  I'm just wondering if this $event is really an excerpt from a larger data set, or if it has been copied from a larger data set, or otherwise exists in a different form than we see here.  I have a feeling that there may be more to it than is present inside the four corners of this question.

And in any case, when we can get the author of a question to understand the value of showing us the test data (instead of showing us non-working code, or a partial description of the test data, or any of a number of other non-helpful things) then we have made progress for everyone.  And if the author uses var_export() PHP will give us a piece of working code that we can copy/paste and test with immediately.  It's a "help us help you" request.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41783734
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 :)
0
 

Author Comment

by:brucegust
ID: 41785082
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!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question