Solved

How can I update this array?

Posted on 2016-09-04
7
47 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
  • 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 109

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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 109

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Doing something wrong with this PDO Select Statement... 3 18
How is this connection happening? 3 20
Why is my wamp get_include_path() wrong? 2 21
parse url to form? 7 25
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

809 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