changing a value of an element in a twodimensionla array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aqazi@inbox.com

    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

  • tim

    #2
    Re: changing a value of an element in a twodimensionla array


    aqazi@inbox.com wrote:[color=blue]
    > 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[/color]


    Hi aqazi

    These line aren't doing what you expect they were[color=blue]
    > $data[$value][1] = "1";*
    > echo $data[$value][0].NL;[/color]

    $value is always an array so its trying to access the $data array with
    an invalid index.

    Earlier on in the script you use[color=blue]
    > array_push($dat a,$record);[/color]

    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

    Comment

    • aqazi@inbox.com

      #3
      Re: changing a value of an element in a twodimensionla array

      Thanks a Lot.
      Greatly appreciated.
      aqazi

      Comment

      • Jerry Stuckle

        #4
        Re: changing a value of an element in a twodimensionla array

        aqazi@inbox.com wrote:[color=blue]
        > 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
        >[/color]

        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.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        • Rik

          #5
          Re: changing a value of an element in a twodimensionla array

          aqazi@inbox.com wrote:[color=blue]
          > 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[/color]

          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


          Comment

          Working...