Advanced Search

Results 1 to 9 of 9

Thread: Making an array with a php variable

  1. #1
    Join Date
    Jul 2012
    Posts
    197
    Thanks
    55
    Thanked 3 Times in 3 Posts

    Default Making an array with a php variable

    Hi ,

    I have a php variable $ year which contains values as follows

    $year = 1922192319241925

    please note these are not a single value, it is year 1922, 1923, 1924, 1925

    My issue is i need to check each of this value of year with another variable $yearcheck as follows.

    If ($yearcheck = $year) //$yearcheck == "1922" or "1923" or "1924" or "1925"
    {
    echo "Message";
    }

    can anybody find a solution for this problem
    You will know the truth, and the truth will set you free (John 8:32)(The truth is about the only begotten son of GOD.....When you feel you are alone for facing your problems, God will never leave you by your own You are important for God

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    1,882
    Thanks
    16
    Thanked 288 Times in 287 Posts
    Blog Entries
    4

    Default

    Maybe something like this - for php5, although I've included a php4 str_split() function for anyone who might need it;
    PHP Code:
    <?php

    // php4 version of php5 native str_split() function
    // uncomment if required
    /*
    function str_split($string, $length=1) {
      $string_array = explode("\r\n", chunk_split($string, $length));
      return $string_array;
    }
    */

    $year "1922192319241925";
    $yearcheck "1923";

    $array str_split($year4); // split $year at every 4th character

    $yr = array($array[0], $array[1], $array[2], $array[3]);
    if (
    in_array($yearcheck$yr)) {
        echo 
    "Found $yearcheck";
    }

    ?>
    This works on the assumption that $year is always formatted as 4 concatenated years, with each year consisting of 4 numbers (although you could extend the $yr array() with more values as required).
    Focus on Function Web Design | Latest News RSS | Facebook | Twitter |
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps |
    The only limit to creativity is imagination: JemCon.org

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,687
    Thanks
    43
    Thanked 3,129 Times in 3,095 Posts
    Blog Entries
    12

    Default

    You don't have to copy the array. This will do just fine:

    PHP Code:
    <?php

    // php4 version of php5 native str_split() function
    // uncomment if required
    /*
    function str_split($string, $length=1) {
      $string_array = explode("\r\n", chunk_split($string, $length));
      return $string_array;
    }
    */

    $year "1922192319241925";
    $yearcheck "1923";

    $yeararray str_split($year4); // split $year at every 4th character

    if (in_array($yearcheck$yeararray)) {
        echo 
    "Found $yearcheck";
    }

    ?>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    1,882
    Thanks
    16
    Thanked 288 Times in 287 Posts
    Blog Entries
    4

    Default

    Yes I just realised that, except I did it with a few variable name differences;
    PHP Code:
    <?php 

    // php4 version of php5 native str_split() function 
    // uncomment if required 
    /* 
    function str_split($string, $length=1) { 
      $string_array = explode("\r\n", chunk_split($string, $length)); 
      return $string_array; 

    */ 

    $year "1922192319241925"
    $yearcheck "1923"

    $yr str_split($year4); // split $year at every 4th character 

    if (in_array($yearcheck$yr)) { 
        echo 
    "Found $yearcheck"


    ?>
    Its a Sunday - my brain is still in weekend mode

    Still, not bad for a somebody on iPhone
    Focus on Function Web Design | Latest News RSS | Facebook | Twitter |
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps |
    The only limit to creativity is imagination: JemCon.org

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,687
    Thanks
    43
    Thanked 3,129 Times in 3,095 Posts
    Blog Entries
    12

    Default

    Right, that works. and if you really needed to copy the array or, more to the point perhaps make another version of it slightly changed, a foreach or other loop might be a better choice because you wouldn't have to know ahead of time how many elements were in the array.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Jul 2012
    Posts
    197
    Thanks
    55
    Thanked 3 Times in 3 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    Right, that works. and if you really needed to copy the array or, more to the point perhaps make another version of it slightly changed, a foreach or other loop might be a better choice because you wouldn't have to know ahead of time how many elements were in the array.
    Thanks for your advice and the coding seems to be fine.

    But to solve my issue i need to give some more information as follows.

    PHP Code:
    <?php 

    // php4 version of php5 native str_split() function 
    // uncomment if required 
    /* 
    function str_split($string, $length=1) { 
      $string_array = explode("\r\n", chunk_split($string, $length)); 
      return $string_array; 

    */ 

    $year "1922192319241925"
    $yearcheck "1923"

    $yr str_split($year4); // split $year at every 4th character 

    if (in_array($yearcheck$yr)) { 
        echo 
    "Found $yearcheck"


    ?>
    the values of $year is displaying from a loop increment $year++ not in such a way i asked in my first question, so i think it not to be need of splitting ?
    May be foreach is solution for that ?
    Last edited by Beverleyh; 06-17-2013 at 02:19 PM. Reason: formatting
    You will know the truth, and the truth will set you free (John 8:32)(The truth is about the only begotten son of GOD.....When you feel you are alone for facing your problems, God will never leave you by your own You are important for God

  7. #7
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    1,882
    Thanks
    16
    Thanked 288 Times in 287 Posts
    Blog Entries
    4

    Default

    I'm not sure it matters how $year is being generated, or how long it is - whether
    $year = "1922192319241925"; or
    $year = "19221923192419251926192719281929193019311932193319341935";
    the provided script will just take that string, split at everything 4th characters and add it to the array.

    The suggested code might need to be reevaluated if the string is very, very long but we'll need details. I guess we need to see more of your script to make and solid suggestions.

    Also - don't forget your formatting.
    Focus on Function Web Design | Latest News RSS | Facebook | Twitter |
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps |
    The only limit to creativity is imagination: JemCon.org

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,687
    Thanks
    43
    Thanked 3,129 Times in 3,095 Posts
    Blog Entries
    12

    Default

    I should add that foreach requires an array to work with. So, if you want to use foreach, you need an array, not a string, which is what split does for you.

    But you say all you need is to see if a given year ($yearcheck) is in the string $year. If that's all you need you have your answer. If you need something else, what else do you need?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Jul 2012
    Posts
    197
    Thanks
    55
    Thanked 3 Times in 3 Posts

    Default

    Thanks.. I Solved the issue
    Also - don't forget your formatting.
    Noted, But I am not getting WYSWING at that time,
    But you say all you need is to see if a given year ($yearcheck) is in the string $year. If that's all you need you have your answer. If you need something else, what else do you need?
    Yes the solutions provided here are correct, but need to be an expert thinking only in this issue, because the result of $year is from a for loop check the following it will give some what help to other readers

    Code:
    $dec = array();
    for ($a=$tyear; $a<=$fyear; $a++)
    array_push($dec,$a);
    Then make it in

    Code:
    if (in_array($yearcheck, $dec)) { 
        echo "Found $yearcheck"; 
    }
    You will know the truth, and the truth will set you free (John 8:32)(The truth is about the only begotten son of GOD.....When you feel you are alone for facing your problems, God will never leave you by your own You are important for God

Similar Threads

  1. Puzzle help? (making an array?)
    By SemiSuicidal in forum JavaScript
    Replies: 2
    Last Post: 09-20-2011, 03:13 PM
  2. how to to pass array[x] to a variable?
    By ksquared3 in forum JavaScript
    Replies: 15
    Last Post: 02-07-2011, 11:51 AM
  3. Replies: 0
    Last Post: 01-24-2009, 05:46 AM
  4. Replies: 4
    Last Post: 06-17-2007, 08:40 PM
  5. Variable and array looping
    By Ampulator00 in forum JavaScript
    Replies: 0
    Last Post: 12-20-2004, 11:19 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •