I have this array

Array
(
    [0] => Array
        (
            [id] => 400
            [url] => Array
                (
                    [scheme] => http
                    [host] => www.domain3.com
                    [path] => /4/
                )

        [user] => user4
        [pass] => pass4 

    )

[1] => Array
    (
        [id] => 500
        [url] => Array
            (
                [scheme] => http
                [host] => www.domain4.com
                [path] => /5/
            )

        [user] => user5
        [pass] => pass5
    )

[2] => Array
    (
        [id] => 300
        [url] => Array
            (
                [scheme] => http
                [host] => www.domain1.com
                [path] => /3/
            )

        [user] => user3
        [pass] => pass3 

    )

[3] => Array
    (
        [id] => 100
        [url] => Array
            (
                [scheme] => http
                [host] => www.domain1.com
                [path] => /1/
            )

        [user] => user1
        [pass] => pass1 

    )

[4] => Array
    (
        [id] => 200
        [url] => Array
            (
                [scheme] => http
                [host] => www.domain1.com
                [path] => /2/
            )

        [user] => user2
        [pass] => pass2

    )

)

How can I filter out duplicate instances of "host" subkey for domain1.com and generate a new array preserving the other keys: id,user,pass?

    i'lld be inclined to create a foreach loop, you can then keep track of host etc and ignore the duplicates

    very basic example that will exclude duplicate host values

    foreach($foo){
    
    if(!in_array($foo['host'],$host){//check if named host already been processed
    $out[]=$foo;
    }
    
    $host[]=$foo['host'];//keep the values of host in an array for checking
    
    
    }
      dagon;10964752 wrote:

      i'lld be inclined to create a foreach loop, you can then keep track of host etc and ignore the duplicates

      very basic example that will exclude duplicate host values

      foreach($foo){
      
      if(!in_array($foo['host'],$host){//check if named host already been processed
      $out[]=$foo;
      }
      
      $host[]=$foo['host'];//keep the values of host in an array for checking
      
      
      }

      What if I want to keep one instance of the duplicate host so from the array above I get 3 results instead of 2?

        in my example you should still get 1 copy, just not duplicates, if your not, post the code your using, and the array (as a variable, so i can run it locally)

          Write a Reply...