#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    390
    Rep Power
    47

    Correct format for Array


    I would like to create an array to store information for time periods on multiple days.

    If
    the days of the week are 1, 2, 3, 4, 5, 6, 7
    and
    there are 5 periods every day: 1, 2, 3, 4, 5

    Then, logically the third period of the fourth day is (4, 3)

    How can I store a value for each day/period combination? (not every combination will have a value)

    I have tried:
    PHP Code:
    $value[4,3] = "somevalue"
    But that doesn't work.

    What am I missing?
    Last edited by SeanF; July 16th, 2013 at 10:46 PM.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    London
    Posts
    1
    Rep Power
    0

    FIll up array values


    Have you tried filling out the values in rows, actually the days are columns and the periods are rows. Fill up each row with values like 1,2,3,4,5.
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    PHP Code:
    $value[4][3] = "somevalue"
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,016
    Rep Power
    376
    if you want to store it as an array then you should know the first "index" is 0 and not 1. Keep that in mind for doing looping of the array..
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    390
    Rep Power
    47
    Originally Posted by Jacques1
    PHP Code:
    $value[4][3] = "somevalue"
    Thanks!

    I've tried the following, very simple example (where $dow is an integer, day_of_week, and $b is an integer, time_block):

    PHP Code:
    echo "ID: $data_id DOW: $dow B: $b </br>";
    $last_data[$dow][$b] =  $data_id;
    echo 
    " Data: $last_data[$dow][$b]. <br>"
    and I get:
    Code:
    ID: 4 DOW: 2 B: 3
    Block Count = 1 ..Data: Array[3]
    How do I output the actual value (which should be Data: 4")?
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Appearently, PHP cannot parse the multi-index syntax within strings. Use concatenation instead:

    PHP Code:
    echo 'Data: ' htmlspecialchars($last_data[$dow][$b], ENT_QUOTES ENT_HTML401'UTF-8') . '<br>'
    And don't forget the escaping.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    390
    Rep Power
    47
    Originally Posted by Jacques1
    Appearently, PHP cannot parse the multi-index syntax within strings. Use concatenation instead:

    PHP Code:
    echo 'Data: ' htmlspecialchars($last_data[$dow][$b], ENT_QUOTES ENT_HTML401'UTF-8') . '<br>'
    And don't forget the escaping.
    Thanks... that's a syntax I have not seen before... what exactly is it doing? It doesn't seem to work in my code.
  14. #8
  15. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    What syntax? What does "not work" mean?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  16. #9
  17. Why so angry?
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jan 2004
    Posts
    1,945
    Rep Power
    898
    Originally Posted by SeanF
    Thanks!

    I've tried the following, very simple example (where $dow is an integer, day_of_week, and $b is an integer, time_block):

    PHP Code:
    echo "ID: $data_id DOW: $dow B: $b </br>";
    $last_data[$dow][$b] =  $data_id;
    echo 
    " Data: $last_data[$dow][$b]. <br>"
    and I get:
    Code:
    ID: 4 DOW: 2 B: 3
    Block Count = 1 ..Data: Array[3]
    How do I output the actual value (which should be Data: 4")?
    In double quoted strings, wrap what needs to be parsed in curly braces

    PHP Code:
    echo " Data: {$last_data[$dow][$b]}. <br>"
    or use periods to concatenate without quotations

    PHP Code:
    echo " Data: " $last_data[$dow][$b] . ". <br>"
    Verify and sanitize ALL USER DATA.

    And, to steal a quote from jeremy, "Explain your problem instead of asking how to do what you decided was the solution." Chances are someone on the forums will know a better or more efficient way to do what you're trying to accomplish.

    Avatar: Stolen by me, shown to me by patrick.

  18. #10
  19. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by SeanF
    Thanks... that's a syntax I have not seen before... what exactly is it doing? It doesn't seem to work in my code.
    This should be all you really need in this case:
    PHP Code:
    echo 'Data: '.htmlspecialchars($last_data[$dow][$b]).'<br>'
    htmlspecialchars converts certain characters to their HTML "entity" equivalent to avoid collision with those characters' use in the HTML structure. This is especially important to do if what you're printing out is content that originated from user entry--without this conversion it's possible for them to inject Javascript code into your site, which is a type of security exploit.

    In most display capacities what I provided is perfectly sufficient. If you're putting a value within a tag--such as a form input value field (ie, value=' -Your variable- ') then Jacque's more lengthy form becomes more prudent.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    390
    Rep Power
    47
    Thanks all for your replies.

    I discovered that PHP can use array vales in formulas more directly than it can output them to the screen.

    For example,

    I can simply assign another variable such as:
    PHP Code:
    $check $last_data[$dow][$b]; 
    and then use that value or echo it to the screen quite simply.

    This seems more direct than using
    PHP Code:
    echo 'Data: ' htmlspecialchars($last_data[$dow][$b], ENT_QUOTES ENT_HTML401'UTF-8') . '<br>'
    although, the solution proposed by AngryPanda (double quoted strings in curly braces) seems pretty straightforward as well.

    Thanks again
  22. #12
  23. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    No, no, no. I didn't add the htmlspecialchars() just for fun or to make your code look ugly. It's there to protect your code against cross-site scripting. You need it.

    Removing it to make your code prettier is like removing the brakes from your car to make it lighter.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  24. #13
  25. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by Jacques1
    Removing it to make your code prettier is like removing the brakes from your car to make it lighter.
    More like leaving it unlocked to make it quicker to get into and out of, I'd say. The lack of brakes is going to screw you over no matter what. Leaving the door unlocked for short periods probably won't get it stolen, but it's really not a risk worth taking.
    LinkedIn: Dave Mittner
  26. #14
  27. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4124
    Originally Posted by SeanF
    I can simply assign another variable such as:
    PHP Code:
    $check $last_data[$dow][$b]; 
    and then use that value or echo it to the screen quite simply.
    yes and no

    The yes part:

    you access the value in that manor.

    For example, to make your code look prettier

    PHP Code:
    //
    $val $arr[$k1][$k2];

    $new_val $val 2;

    //instead of

    $new_val $arr[$k1][$k2] * 2
    with a high level of nesting and numerous arrays and keys coming from other array values, etc etc etc then making your code more maintainable is a good thing

    The no part

    PHP Code:
    // This is bad if the value is user submitted or untrusted data
    $val $arr[$k1][$k2];

    echo 
    $val;

    //This is better

    echo htmlspecialchars($valENT_QUOTES ENT_HTML401'UTF-8'); 
    passing a value from one variable to another does not cleanse it in any way.

    As pointed out above when injecting data from one technology (eg php) to another (eg html, mysql) you must escape (cleanse, make safe) the data for use in the target technology
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]

IMN logo majestic logo threadwatch logo seochat tools logo