Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11

Thread: array

  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts

    array

    Hi;
    I got two arrays which is first_array and second_array,
    i want to keep the elements which in second_array and the first latter do not start with a,k,c,m,d,e,f,g.
    so i just want to keep 'pppa','yyyyk','zzjjj' for the second_array,could anyone tell me how do that, please, thanks


    $first_Array=('a','k','b','c','m','d','e','f','g')

    $second_Array=('ahhh','kggg','bhjj','ctert','msd','dgx','efd','fffd','gdf','pppa','yyyyk','zzjjj')

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Depends on if you want to do it the lazy way or not
    This way would require a global call:
    PHP Code:

    function removeMatchFirst($item)
    {
        global 
    $first_Array;
        
    $bResult false;
        if(
    is_string($item))
        {
            
    $firstChar substr($item01);
            if (!
    in_array($firstChar$first_Array))
            {
                
    $bResult true;
            }
        }
        return 
    $bResult;
    }

    $second_array array_filter($scondArray'removeMatchFirst'); 
    Another (probably better I guess) solution would be to use an array_walk. It takes some work to force an additional parameter in as a reference, but it can be done.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts
    sorry, where you call to the fuction removeMatchFirst with parrmata with item ??

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Code:
    $second_array = array_filter($scondArray, 'removeMatchFirst');
                                                      ^
                                                      Called Here
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts
    there are nothing i can print out ??? bit strange

    PHP Code:
    $first_Array=array('a','k','b','c','m','d','e','f','g');
    $secondArray=array('ahhh','kggg','bhjj','ctert','msd','dgx','efd','fffd','gdf','pppa','yyyyk','zzjjj');

    function 
    removeMatchFirst($item

        global 
    $first_Array;
        
    $bResult false
        if(
    is_string($item)) 
        { 
            
    $firstChar substr($item01); 
            if (!
    in_array($firstChar$first_Array)) 
            { 
                
    $bResult true
            } 
        } 
        return 
    $bResult


    $second_array array_filter($secondArray'removeMatchFirst');  
    print_r($second_Array); 

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by kamkam View Post
    there are nothing i can print out ??? bit strange

    PHP Code:
    $first_Array=array('a','k','b','c','m','d','e','f','g');
    $secondArray=array('ahhh','kggg','bhjj','ctert','msd','dgx','efd','fffd','gdf','pppa','yyyyk','zzjjj');

    function 
    removeMatchFirst($item

        global 
    $first_Array;
        
    $bResult false
        if(
    is_string($item)) 
        { 
            
    $firstChar substr($item01); 
            if (!
    in_array($firstChar$first_Array)) 
            { 
                
    $bResult true
            } 
        } 
        return 
    $bResult


    $second_array array_filter($secondArray'removeMatchFirst');  
    print_r($second_Array); 
    Do you mean that its not printing anything?
    PHP Code:
    $second_array array_filter($secondArray'removeMatchFirst');   
    print_r($second_Array); 
    $second_array should be either $second_array or $second_Array. There is a case difference in the 'a'.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Thanks, but why it it does not work for non english letter, it give me not correct result.

    Array ( [0] => 万 [1] => 与 [2] => 丑 [3] => 专 [4] => 业 [5] => 是万 [6] => 的与 [7] => 飞丑 [8] => 人专 [9] => 任业 )


    PHP Code:
    $first_Array=array('万','与','丑','专','业');
    $secondArray=array('万','与','丑','专','业','是万','的与','飞丑','人专','任业');


    function 
    removeMatchFirst($item)  
    {  
        global 
    $first_Array
        
    $bResult false;  
        if(
    is_string($item))  
        {  
            
    $firstChar substr($item01);  
            if (!
    in_array($firstChar$first_Array))  
            {  
                
    $bResult true;  
            }  
        }  
        return 
    $bResult;  
    }  

    $second_Array array_filter($secondArray'removeMatchFirst');   
    print_r($second_Array); 

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I'll fix it when I get home and can write some code.
    The problem is likely that its the unicode charset thats killing it. I'm thinking substr fetches 0 and 1 on byte counts and not character counts. I find out how to fix it later.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #9
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts
    ok, thanks for your time.

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Ok, try this:
    PHP Code:
    function removeMatchFirst($item)
    {
        global 
    $first_Array;
        
    $bResult true;
        if (
    is_string($item))
        {
            
    $iInArr count($first_Array);
            for (
    $i 0$i $iInArr && $bResult; ++$i)
            {
                if (
    mb_strpos($item$first_Array[$i]) === 0)
                {
                    
    $bResult false;
                }
            }
        }
        
        return 
    $bResult;

    Hopefully someone else has more experience with the mbstring library to find an easier way to do this.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #11
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Thanks a lot, it does work now, thanks for your time again.


  •  

    Posting Permissions

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