473,608 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

changing a value of an element in a twodimensionla array

Hi guys I am having a problem with arrayu manipulation.

in my php script i am reading from a csv file.
the content of file is like this:

name,color,quan tity,price;
apple,red,10,$2 ;
mango,green,12, $2.5;
orange,orange,8 ,$1.5;

I am reading the file like this:

$userFile = fopen("data/user.csv", "r");
$data = array();
$record = array();

while (($record = fgetcsv($userFi le, 1000, ",")) !== FALSE)
{
array_push($dat a,$record);
}
fclose($userFil e);

Now if I want to change the quantity of mango what I have to do. I am
trying to do something, like this but it's not entirly correct.

foreach($data as $value)
{

if(((strcasecmp ("mango", $value[0]) == 0))
{
echo $value[1].NL;
$data[$value][1] = "1";*
echo $data[$value][0].NL;
}
}

Can anyone give me a hint how can I change the value here.

Any help will be grealty appreciated.

Thanks
aqazi

May 20 '06 #1
4 1468
tim

aq***@inbox.com wrote:
Hi guys I am having a problem with arrayu manipulation.

in my php script i am reading from a csv file.
the content of file is like this:

name,color,quan tity,price;
apple,red,10,$2 ;
mango,green,12, $2.5;
orange,orange,8 ,$1.5;

I am reading the file like this:

$userFile = fopen("data/user.csv", "r");
$data = array();
$record = array();

while (($record = fgetcsv($userFi le, 1000, ",")) !== FALSE)
{
array_push($dat a,$record);
}
fclose($userFil e);

Now if I want to change the quantity of mango what I have to do. I am
trying to do something, like this but it's not entirly correct.

foreach($data as $value)
{

if(((strcasecmp ("mango", $value[0]) == 0))
{
echo $value[1].NL;
$data[$value][1] = "1";
echo $data[$value][0].NL;
}
}

Can anyone give me a hint how can I change the value here.

Any help will be grealty appreciated.

Thanks
aqazi

Hi aqazi

These line aren't doing what you expect they were $data[$value][1] = "1";*
echo $data[$value][0].NL;
$value is always an array so its trying to access the $data array with
an invalid index.

Earlier on in the script you use array_push($dat a,$record);


The first $record you push onto $data will have an index of 0, the next
record will have an index of 1 etc

So if you change the foreach loop later on to

// v v
foreach($data as $index => $value)
{

// you may be able to use.... if ( $value[0] == 'mango') {
// if not then use '===' not '==' for checking the return value of
strcasecmp

if(((strcasecmp ("mango", $value[0]) === 0))
{
echo $value[2]."\n";
$data[$index][2] = "1";
echo $data[$index][0]."\n";
}
}

If you use strcasecmp then you must use '===' not '==', strcasecmp may
return false. Using the '== 0' will give you the wrong result when
strcasecmp return false because false has a numeric value of 0. It
will give the impression 'mango' was found whether it was in the string
or not.

Tim

May 20 '06 #2
Thanks a Lot.
Greatly appreciated.
aqazi

May 20 '06 #3
aq***@inbox.com wrote:
Hi guys I am having a problem with arrayu manipulation.

in my php script i am reading from a csv file.
the content of file is like this:

name,color,quan tity,price;
apple,red,10,$2 ;
mango,green,12, $2.5;
orange,orange,8 ,$1.5;

I am reading the file like this:

$userFile = fopen("data/user.csv", "r");
$data = array();
$record = array();

while (($record = fgetcsv($userFi le, 1000, ",")) !== FALSE)
{
array_push($dat a,$record);
}
fclose($userFil e);

Now if I want to change the quantity of mango what I have to do. I am
trying to do something, like this but it's not entirly correct.

foreach($data as $value)
{

if(((strcasecmp ("mango", $value[0]) == 0))
{
echo $value[1].NL;
$data[$value][1] = "1";*
echo $data[$value][0].NL;
}
}

Can anyone give me a hint how can I change the value here.

Any help will be grealty appreciated.

Thanks
aqazi


Two things:

The quantity is in $value[2], not $value[1].
$data[$value][1] is incorrect. $value is a row in $data, not an index. So
just use $value.

$value[2] = "1";

should work (but will set the count to a string instead of an integer).
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
May 20 '06 #4
Rik
aq***@inbox.com wrote:
while (($record = fgetcsv($userFi le, 1000, ",")) !== FALSE)
{
array_push($dat a,$record);
}
fclose($userFil e);

Now if I want to change the quantity of mango what I have to do. I am
trying to do something, like this but it's not entirly correct.

foreach($data as $value)
{

if(((strcasecmp ("mango", $value[0]) == 0))
{
echo $value[1].NL;
$data[$value][1] = "1";*
echo $data[$value][0].NL;
}
}

Can anyone give me a hint how can I change the value here.

Any help will be grealty appreciated.

Thanks
aqazi


It would be easier if you defined keys in your $data array, like:
while (($record = fgetcsv($userFi le, 1000, ",")) !== FALSE)
{
$data[$record[0]]= array('color' => $record[1], 'quantity' =>
$record[2], 'price' => $record[3]);
}
fclose($userFil e);

(Assuming name is unique)

And then:

$data['mango']['quantity'] = 3;
or
$data['mango']['quantity']++;
whatever you want.

Without changing the array like I said, your code would be like:
foreach($data as $key => $value)
{
if($value[0]=='mango')
{
$data[$key][3] = 'whatever you want quantity to be';
}
}
or write your won array_search_al l or array_search_al l_recursive function

Grtz,
--
Rik Wasmus
May 21 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2278
by: Hal Vaughan | last post by:
I have a sample script from a book ("Beginning JavaScript" by Paul Wilton) that removes or adds a choice to a <SELECT> element. The <FORM> is form1 and the <SELECT> is theDay. The example uses these lines (full text is below): if (document.form1.theDay.options.text != "Wednesday) { var days = document.form1.theDay; days.options.text = days.options.text; <snip> var option - new Option("Wednesday", 2);
8
10211
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when it is assigned a text literal?? HOW can I change the array value to upper case then? What other method exists for arrays? Ex: var GridArrayName1 = new Array(); GridArrayName1 = new Array ('test-value'); GridArrayName1 = GridArrayName1...
9
1913
by: F. Da Costa | last post by:
Hi, Does anybody know why IE5+ does *not* honour array objects (like a table) across a session? Example: Frame A contains a var tableVar which is set via form Frame B (on init) using top.A.tableVar = document.getElementById("someTable"); As long as Frame B is *not* 'refreshed/ reloaded' witk another page the
5
2928
by: Andrew Poulos | last post by:
If I'm searching for an occurance of a value in a multi-dimensional array how can I get it's index returned as an array, if found? For example, if: foo = new Array(); foo = , 5, , 9, 10]; Array.prototype.findValue = function(val) { // blah }
2
1718
by: Urs Vogel | last post by:
Hi When using XmlDocument, I can create nodes and attributes as I like. What I didn't achieve is changing the Value of a node (created with createElement), it claims that it's the wrong node type. When changing the InnerText, I loose all child nodes. When cloning the node and reassigning the child nodes of the original node to the cloned node, the children are not visilble in the InnerXml. To make it simple. How do I change the value...
26
7060
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of the structure inside the array. When I try this, an error about late assignment appears. Is it possible to assign a value to a structure field that is in an array? I'm currently getting around the problem by creating a new structure, assign...
14
20384
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is passed also. I understand that this way of passing the array is by value and if the prototype is declared as foo(int *), it is by reference in which case the value if modified in the function will get reflected in the main function as well. I dont...
4
3312
by: gubbachchi | last post by:
Hi all, Please anybody help me solve this problem. I am stuck up with this from past 2 weeks. I am developing an application where, when the user selects date from javascript datepicker and enters the comments and clicks the save button then the date and the date will be stored in the mysql database. This is working fine. But my problem is when, after the user had made an entry the date in the calendar for which an entry has made should be...
0
8059
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8470
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8145
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6815
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4023
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1328
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.