array help

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

    #1

    array help

    i'm still new to php and programming and was hoping to get some help on
    a few questions i haven't had success in answering myself. I
    successfully created my first very simple script to accomplish a task I
    wanted, but not completely. see my code below:

    <?php

    //my first ever php program simply designed to automatically adjust new
    prices of my properties

    //current property prices
    $prices = array('250000', '250000', '350000');

    //percentage of how much to add to current prices
    $increase_rate = '0.015'; // 1.15 %

    //add new percentage to each price
    foreach ($prices as &$newprice) {
    $newprice = $newprice + ($newprice * $increase_rate) ;
    }

    //print out all new prices
    for ($key = 0; $key < 3; $key++) {
    print "$prices[$key]<br />";
    }

    ?>

    (forgive me if these questions seem completely common sense or stated
    clearly in the php manual. I have done reading into these all day and
    have gave up and so here I am)

    HELP QUESTIONS:
    ---------------


    1.) How would I make a copy of the old prices array that
    was manipulated?

    e.g. -echo the following structure:
    set an array of values, then do a function to
    manipulate the values, then show the old
    values in comparison of the new values
    (basically, show that $253,750 used to be
    $250,000)


    2.) a) What if i have commas in my prices by default?

    e.g. = array('349,502' '234,995');

    b) How would I go about processing comma values
    received via a form that a user entered or from .CSV
    file that looked like (132,500, 123,340, 345,959,
    244,459)?
    c) How would I add a comma into my printed outputed values?

    3.) There must be a better way to print out all the values
    from an array but constructing a loop came to mind and
    beats doing it one by one. Is there better way?

  • .:[ ikciu ]:.

    #2
    Re: array help

    Hmm Uzytkownik <kurrent@gmail. comwrote:
    1.) How would I make a copy of the old prices array that
    was manipulated?
    $copy = $prices;
    2.) a) What if i have commas in my prices by default?
    str_replace(',' ,'.',$newprice) + 9....
    >
    b) How would I go about processing comma values
    received via a form that a user entered or from .CSV
    file that looked like (132,500, 123,340, 345,959,
    244,459)?
    str_replace(',' ,'.',$newprice) + 9....

    c) How would I add a comma into my printed outputed values?
    str_replace('.' ,',',$newprice) + 9....

    3.) There must be a better way to print out all the values
    from an array but constructing a loop came to mind and
    beats doing it one by one. Is there better way?
    you can do it this way what you do


    --
    ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~
    Ikciu | gg: 718845 | yahoo: ikciu_irsa | www: www.e-irsa.pl

    2be || !2be $this =mysql_query();


    Comment

    • Rik

      #3
      Re: array help

      kurrent@gmail.c om wrote:
      i'm still new to php and programming and was hoping to get some help
      on a few questions i haven't had success in answering myself. I
      successfully created my first very simple script to accomplish a task
      I wanted, but not completely. see my code below:
      >
      <?php
      >
      //my first ever php program simply designed to automatically adjust
      new prices of my properties
      >
      //current property prices
      $prices = array('250000', '250000', '350000');
      >
      //percentage of how much to add to current prices
      $increase_rate = '0.015'; // 1.15 %
      >
      //add new percentage to each price
      foreach ($prices as &$newprice) {
      $newprice = $newprice + ($newprice * $increase_rate) ;
      }
      >
      //print out all new prices
      for ($key = 0; $key < 3; $key++) {
      print "$prices[$key]<br />";
      }
      >
      >>
      <pre>
      <?php
      $prices = array('250000', '250000', '350000');
      $increase_rate = '0.015';

      function increase_price( &$item,$key,$ra te){
      $item = number_format(f loatval(str_rep lace(',','',$it em)) * (1 +
      $rate),0,'',',' );
      }
      $oldprices = $prices;
      array_walk($pri ces,'increase_p rice',$increase _rate);
      $changes = array_map(null, $oldprices,$pri ces);
      print_r($change s);
      ?>
      </pre>
      e.g. -echo the following structure:
      set an array of values, then do a function to
      manipulate the values, then show the old
      values in comparison of the new values
      (basically, show that $253,750 used to be
      $250,000)
      print_r() will show you the arrays, if it's just to check for the
      programmer, for a form/UI offcourse it will be better to give it some sort
      of layout, vprintf() is very handy for this.
      2.) a) What if i have commas in my prices by default?
      >
      e.g. = array('349,502' '234,995');
      Removed by str_replace();
      b) How would I go about processing comma values
      received via a form that a user entered or from .CSV
      file that looked like (132,500, 123,340, 345,959,
      244,459)?
      str_replace could be used for this, but if you're not absolutely sure about
      your data, I'd use $price = preg_replace('/[^0-9]/','',$price); (on the
      condition you never use decimals, otherwise you might want to use
      '/[^0-9\.]/' instead).
      c) How would I add a comma into my printed outputed values?
      number_format() , or maybe in this case money_format();
      3.) There must be a better way to print out all the values
      from an array but constructing a loop came to mind and
      beats doing it one by one. Is there better way?
      Why not a foreach() loop there?
      Anyway, print_r() and var_dump() are your friends while building, for a
      form/html-page for other users you almost certainly will have to use a loop
      or a str_repeat()/vprintf() combo.

      Grtz,
      --
      Rik Wasmus


      Comment

      • kurrent@gmail.com

        #4
        Re: array help


        Rik wrote:
        kurrent@gmail.c om wrote:
        i'm still new to php and programming and was hoping to get some help
        on a few questions i haven't had success in answering myself. I
        successfully created my first very simple script to accomplish a task
        I wanted, but not completely. see my code below:

        <?php

        //my first ever php program simply designed to automatically adjust
        new prices of my properties

        //current property prices
        $prices = array('250000', '250000', '350000');

        //percentage of how much to add to current prices
        $increase_rate = '0.015'; // 1.15 %

        //add new percentage to each price
        foreach ($prices as &$newprice) {
        $newprice = $newprice + ($newprice * $increase_rate) ;
        }

        //print out all new prices
        for ($key = 0; $key < 3; $key++) {
        print "$prices[$key]<br />";
        }
        >
        <pre>
        <?php
        $prices = array('250000', '250000', '350000');
        $increase_rate = '0.015';
        >
        function increase_price( &$item,$key,$ra te){
        $item = number_format(f loatval(str_rep lace(',','',$it em)) * (1 +
        $rate),0,'',',' );
        }
        $oldprices = $prices;
        array_walk($pri ces,'increase_p rice',$increase _rate);
        $changes = array_map(null, $oldprices,$pri ces);
        print_r($change s);
        ?>
        </pre>
        >
        e.g. -echo the following structure:
        set an array of values, then do a function to
        manipulate the values, then show the old
        values in comparison of the new values
        (basically, show that $253,750 used to be
        $250,000)
        >
        print_r() will show you the arrays, if it's just to check for the
        programmer, for a form/UI offcourse it will be better to give it some sort
        of layout, vprintf() is very handy for this.
        >
        2.) a) What if i have commas in my prices by default?

        e.g. = array('349,502' '234,995');
        >
        Removed by str_replace();
        >
        b) How would I go about processing comma values
        received via a form that a user entered or from .CSV
        file that looked like (132,500, 123,340, 345,959,
        244,459)?
        >
        str_replace could be used for this, but if you're not absolutely sure about
        your data, I'd use $price = preg_replace('/[^0-9]/','',$price); (on the
        condition you never use decimals, otherwise you might want to use
        '/[^0-9\.]/' instead).
        >
        c) How would I add a comma into my printed outputed values?
        >
        number_format() , or maybe in this case money_format();
        >
        3.) There must be a better way to print out all the values
        from an array but constructing a loop came to mind and
        beats doing it one by one. Is there better way?
        >
        Why not a foreach() loop there?
        Anyway, print_r() and var_dump() are your friends while building, for a
        form/html-page for other users you almost certainly will have to use a loop
        or a str_repeat()/vprintf() combo.
        >
        Grtz,
        --
        Rik Wasmus

        Thanks you very much for your detailed response Rik. You're
        explanations are going to keep me busy tonight learning them throughly
        and help me out a great deal in the process. Much appreciated for you
        to take the time to answer these questions!

        Comment

        Working...