Solved

How can I update this array?

Posted on 2016-09-04
7
35 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 108

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Append letter to end of string 4 36
phpmailer in WHILE loop - weird results 10 28
How can I upload multiple images from a HTML form? 2 18
Checking if varaible is empty 6 31
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now