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

    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Unknown array size


    hello everyone!!
    I ' m really really new to php and I was wondering if it's possible to search an array of unknown size.
    I have a string and I use explode in order to split it into other strings using as a delimeter the space character.
    Then as a result,I have an array of strings.
    Now,what I want to do is call a function for every string in the array but I dont know how many strings-how many array elements- exist.
    How can I make this loop?
    Thanks in advance!

    here's what I have so far..
    PHP Code:
    $wholeString $_GET['str'];
    $array explode(" "$wholeString ); 
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,622
    Rep Power
    595
    You always can know the size of an array using count.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Your question has generic answers which can use foreach, array_walk, count, or any number of other things. Foreach is probably what you want.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0
    thank you guys so much for your answers.
    Also I'd like to ask if "count" is the best way to check
    when I have reached the last element of the array,as in my code
    the last element of the array has to deal with a different function from the other ones
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    use array_pop to get the last element off the array beforehand.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by ManiacDan
    use array_pop to get the last element off the array beforehand.
    If I have understand properly the use of array-pop it deletes the last element of the array.All I want to do is to save the last element in a variable in order to use it for a check in the code
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,622
    Rep Power
    595
    It does both. It returns the last element into a variable and removes it from the array. Alternatively you can use 'count' (as previously suggested) as the index of the last element and keep the array in tact.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  14. #8
  15. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    PHP Code:
    $array = array(,1,2,3,4,5,"hello");
    $last array_pop($array);
    foreach ( 
    $array as $value ) {
      
    someFunction($value);
    }
    otherFunction($last); 
    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo