SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    66 Post(s)
    Tagged
    2 Thread(s)

    array_diff() and with named arrays

    Code PHP:
    $a1 = array("1", "2");
    $a2 = array(array("numbers" => "1", "something else" => "foo"), array("numbers" => "2", "something else" => "bar"));

    I need to compare $a1 to $a2 numbers.

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    I dont... understand?

    What output do you desire?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    Assumption: You mean you want any values of $a2 whose number element does not exist in $a1;

    Something like...
    PHP Code:
    $a3 array_filter($a2,function ($item) { return !in_array($item['number'],$a1); }); 
    , perhaps?
    (Note: Untested)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  4. #4
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    66 Post(s)
    Tagged
    2 Thread(s)
    No way to use array_diff? I want anything in $a1 that does not exist in $a2['numbers']

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by K. Wolfe View Post
    No way to use array_diff? I want anything in $a1 that does not exist in $a2['numbers']
    Well, sort of i suppose.
    PHP Code:
    array_diff($a1,array_map(function($item) { return $item['numbers']; },$a2)); 
    Essentially reduce $a2 to the array of it's number elements, and then diff it.

    EDIT: Helps if i put the parameters in the correct order.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  6. #6
    SitePoint Zealot 2ndmouse's Avatar
    Join Date
    Jan 2007
    Location
    West London
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but values 1 and 2 DO exist in $a2
    do you mean you want to return any values in $a2 that don't exist in $a1

    if so,

    this might help:

    PHP Code:
    $a1 = array("1""2");
    $a2 = array(array("numbers" => "1""something else" => "foo"), array("numbers" => "2""something else" => "bar"));
    $a3 array_merge_recursive(array_diff($a2[0],$a1),array_diff($a2[1],$a1));

    echo 
    '<pre>';
    print_r($a3);
    echo 
    '</pre>'
    returns:

    Code:
    Array
    (
        [something else] => Array
            (
                [0] => foo
                [1] => bar
            )
    
    )
    Detect file changes remotely. SimpleSiteAudit is an early
    warning anti-hacker system which sends an alert on detection.

    PHP Find Orphan Files - Finds all the unreferenced files on your site.


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
  •