SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get before comma in array?

    I'm trying to figure this out...

    I have a string like this:
    PHP Code:
    $string '1,john,43,2,peter,51,3,jeff,50,5,nick,46'
    The string is like this: placement,name,age

    So to get this out in an array I do this:
    PHP Code:
    $array explode(",",$string);
    $h '';
    $k 0;

    foreach(
    $array as $value){
        
    $h .= ($k%== 2)?$value.';':$value.',';
        
    $k++;
    }

    $newarray explode(";",substr($h,0,-1)); 
    Now I have an array like this
    Code:
    Array
    (
        [0] => 1,john,43
        [1] => 2,peter,51
        [2] => 3,jeff,50
        [3] => 5,nick,46
    )
    Now what I want now is to be able to get the placement out of the array and find the missing placement (4) and make that into an variable... Just dont know where to start?

    Please help and thanks in advance:-)

  2. #2
    SitePoint Enthusiast NuttySkunk's Avatar
    Join Date
    Jan 2012
    Location
    United Kingdom
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you check that each array starts with the array item number + 1?

    i.e. if "$newarray[3] == 4" then it is correct however if "$newarray[3] !== 4" then it is missing?

    PHP Code:

    for($i 0,$i <= count($newarray),$i++){
         
    $missing = (substr($newarray[$i], 01) == ($i 1)) ? 'Value exists':'Value dosnt exist';

    I am by no means a PHP expert and I will be surprised if this even works (cant test it right now), but it might spark an idea?

  3. #3
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by NuttySkunk View Post
    Could you check that each array starts with the array item number + 1?

    i.e. if "$newarray[3] == 4" then it is correct however if "$newarray[3] !== 4" then it is missing?

    PHP Code:

    for($i 0,$i <= count($newarray),$i++){
         
    $missing = (substr($newarray[$i], 01) == ($i 1)) ? 'Value exists':'Value dosnt exist';

    I am by no means a PHP expert and I will be surprised if this even works (cant test it right now), but it might spark an idea?
    Actually... It kind a works... When doing the script above I get this:
    Value existsValue existsValue existsValue dosnt existValue dosnt exist

    Now, how do I extract the number that is missing so I only get that and as a number e.g. number 4 in this case?

  4. #4
    SitePoint Enthusiast NuttySkunk's Avatar
    Join Date
    Jan 2012
    Location
    United Kingdom
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh awesome.

    To get the number back:

    PHP Code:
    for($i 0,$i <= count($newarray),$i++){
         
    $missing = (substr($newarray[$i], 01) == ($i 1)) ? 'Value for $newarray[' $i '] exists':'Value for $newarray[' $i '] dosnt exist';

    And then you need to run some if statement or several to incriment $i because the numbers are out of sync, instead of the numbers being different by one they are now differrent by two and if another value fails they will become different by three etc etc I will be back later, ill try to think of a the next bit.

  5. #5
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now, mayby we are allmost there but not yet...

    When doing this:
    PHP Code:
    for ($i 0$i <= count($newarray); $i++) {
        
    $missing = (substr($newarray[$i], 01) == ($i 1)) ? 'Value for '.substr($newarray[$i], 01).' exists<br>':'Value for '.substr($newarray[$i], 01).' dosnt exist<br>';
        echo 
    $missing;

    I get this:
    Code:
    Value for 1 exists
    Value for 2 exists
    Value for 3 exists
    Value for 5 dosnt exist
    Value for dosnt exist
    It jumps number 4 and actually... Thats the only number I want? Where do I/we go wrong?

  6. #6
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well... Getting close...
    I have done this:
    PHP Code:
    $string '1,john,43,2,peter,51,3,jeff,50,5,nick,46,7,Rick,46';

    $array explode(",",$string);
    $h '';
    $k 0;

    foreach(
    $array as $value){
        
    $h .= ($k%== 2)?$value.';':$value.',';
        
    $k++;
    }

    $newarray explode(";",substr($h,0,-1));

    for (
    $i 0$i <= count($newarray); $i++) {
        
    $arr1[] = substr($newarray[$i], 01);
    }

    $arr2 range(1,max($arr1));                                                    

    $missing array_diff($arr2,$arr1);

    echo 
    '<pre>';
    print_r($missing);
    echo 
    '</pre>'
    This gives me the 2 missing numbers as I wanted... But I only want to get the first missing number into an phh variable $firstmissing...

    Hop do I do so?

  7. #7
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And the winner is...

    PHP Code:
    $first reset($missing); 
    Thanks for all the help ;-)

  8. #8
    SitePoint Enthusiast NuttySkunk's Avatar
    Join Date
    Jan 2012
    Location
    United Kingdom
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Glad its sorted.

  9. #9
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,081
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    If you don't care about the "inbetween array" * you can also do this:

    PHP Code:
    $string '1,john,43,2,peter,51,3,jeff,50,5,nick,46,7,Rick,46'
    $arr explode(','$string);
    $count count($arr);
    $current $arr[0];

    for (
    $i=3$i<$count$i+=3) {
      
    $expected $current+1;
      if (
    $arr[$i] != $expected) {
        echo 
    $expected' is missing!';
      }
      
    $current $arr[$i];

    *) I mean this one

    Code:
    Array
    (
        [0] => 1,john,43
        [1] => 2,peter,51
        [2] => 3,jeff,50
        [3] => 5,nick,46
    )
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  10. #10
    SitePoint Enthusiast NuttySkunk's Avatar
    Join Date
    Jan 2012
    Location
    United Kingdom
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    haha That seemed so hard and yet you can just think of that solution! Looks good.


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
  •