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,quantity,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($userFile, 1000, ",")) !== FALSE)
{
array_push($data,$record);
}
fclose($userFile);
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 4 1458 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,quantity,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($userFile, 1000, ",")) !== FALSE) { array_push($data,$record); } fclose($userFile);
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($data,$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
Thanks a Lot.
Greatly appreciated.
aqazi 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,quantity,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($userFile, 1000, ",")) !== FALSE) { array_push($data,$record); } fclose($userFile);
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*******@attglobal.net
================== aq***@inbox.com wrote: while (($record = fgetcsv($userFile, 1000, ",")) !== FALSE) { array_push($data,$record); } fclose($userFile);
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($userFile, 1000, ",")) !== FALSE)
{
$data[$record[0]]= array('color' => $record[1], 'quantity' =>
$record[2], 'price' => $record[3]);
}
fclose($userFile);
(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_all or array_search_all_recursive function
Grtz,
--
Rik Wasmus This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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];
...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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...
| |