SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast laresistance2's Avatar
    Join Date
    May 2011
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How to use yield with array_unique???

    Hi all!

    I would like to know how I could use yield with array_unique

    Here is a simple example (which does not work)

    PHP Code:
    function get_datas() {
       
    $a = ['jjf''jjfj''kfdjkf''aa''aa'];
       foreach(
    $a as $b)
       {
         
    $array = (yield $b);
         
    array_unique($array);
       }


  2. #2
    SitePoint Addict skunkbad's Avatar
    Join Date
    Apr 2008
    Location
    Temecula, CA
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by laresistance2 View Post
    Hi all!

    I would like to know how I could use yield with array_unique

    Here is a simple example (which does not work)

    PHP Code:
    function get_datas() {
       
    $a = ['jjf''jjfj''kfdjkf''aa''aa'];
       foreach(
    $a as $b)
       {
         
    $array = (yield $b);
         
    array_unique($array);
       }

    Yield is just like return, only the function can be called multiple times in a loop because function execution does not stop. If all you are looking for is a unique array, it doesn't seem like you would necessarily want to use the function shown in your question, but it would seem to me that you would use array_unique before the foreach loop.

    PHP Code:
    function get_datas() {
       
    $a = ['jjf''jjfj''kfdjkf''aa''aa'];
       
    array_unique($a);
       foreach(
    $a as $b)
       {
         
    yield $b;
       }


  3. #3
    SitePoint Enthusiast laresistance2's Avatar
    Join Date
    May 2011
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK and can you tell me how I can convert this code with yield

    PHP Code:
    /**
     * @return array
     */
    function get_datas() {
        
    // Begin code ...
        
    $arrayLines = array();
        while (
    false !== $line fgets($fileHandle)) {
            if  (!empty(
    $line))
                
    $arrayLines[] = $line;
            else
                return array();
        }

        
    fclose($fileHandle);

        return 
    $arrayLines;


    Thanks!

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,154
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Actually, I think your initial example should look more like this:

    PHP Code:
    function get_datas() {
       
    $a = ['jjf''jjfj''kfdjkf''aa''aa'];
       foreach(
    $a as $b)
       {
         
    yield $b;
       }
    }  

    $getDatas get_datas();
    $array = array();
    foreach (
    $getDatas as $value)
    {
      if (!
    in_array($value$array))
        
    $array[] = $value;

    Which really is pointless, because you could just use array_unique($a) and get the same result. Can you tell me what you are trying to accomplish with using yield? I'm having a hard time trying to figure out what you want your method to do differently.


Tags for this Thread

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
  •