0

First time poster, long-time visitor.

I've tried locating something on SO that could help me, but so far I've been unsuccessful. If anyone knows of a duplicate of this question I apologize in advance, I was not able to find it.

Anyway, I want to know if there is any best practice or great solution for my problem. It's not that I can't write a functioning code, I just prefer to not rewriting the wheel EVERY time, and hopefully there is an elegant solution.

Consider the following array:

Array
(
    [0] => Array
        (
            [filename] => 'test1.jpg'
            [comment] => 'This is a test'
            [status] => 2
            [author] => 'John Smith'
            [uniquekey1] => 3
        )

    [1] => Array
        (
            [filename] => 'test2.jpg'
            [comment] => 'This is a test'
            [status] => 2
            [author] => 'Unknown'
            [uniquekey2] => 3
        )

    [2] => Array
        (
            [filename] => 'test3.jpg'
            [comment] => 'This is a test'
            [status] => 2
            [author] => 'Unknown'
            [uniquekey3] => 3
        )
)

After processing this, I would like an array returned that contains the keys from the array of arrays above, but only the keys and values from the keys that are identical in all the subarrays. In effect, the above would produce an array looking like this:

Array
(
    [comment] => 'This is a test'
    [status] => 2
)

As clearly visible, ONLY the key:value pairs that are identical in all three (in this example) array items are returned.

A good use example is editing multiple items in iTunes, where the equal values are displayed in the editing fields, and the rest shows a ghosted "Multiple values" text. I'm aiming for something similar.

Thanks for any and all help and pointers.

Edit: Added solution here as well. There was a bit of a mixup since the accepted solution missed that the 'uniqueKey's weren't identical, and array_intersect() returned those as well when the values matched, which was unwanted behaviour. The solution was to use array_intersect_assoc() instead it seems.

$a = array(
    array(
        'filename' => 'test1.jpg',
        'comment' => 'This is a test',
        'status' => 2,
        'author' => 'John Smith',
        'uniquekey1' => 3
    ),
    array(
        'filename' => 'test2.jpg',
        'comment' => 'This is a test',
        'status' => 2,
        'author' => 'Unknown',
        'uniquekey2' => 3
    ),
    array(
        'filename' => 'test3.jpg',
        'comment' => 'This is a test',
        'status' => 2,
        'author' => 'Unknown',
        'uniquekey3' => 3
    ),
);

$b = call_user_func_array('array_intersect_assoc',$a);

...which looks to return the 'comment' and 'status' fields, and nothing else.

1 Answer 1

0

Array_intersect to the rescue:

$array = array();
$array[] = array('filename' => 'test1.jpg', 'comment' => 'This is a test', 'uniqueKey' => 3);
$array[] = array('filename' => 'test2.jpg', 'comment' => 'This is a test', 'uniqueKey' => 3);
$array[] = array('filename' => 'test3.jpg', 'comment' => 'This is a test', 'uniqueKey' => 3);

$intersection = call_user_func_array('array_intersect', $array);

$intersection is then:

Array
(
    [comment] => This is a test
    [uniqueKey] => 3
)
Sign up to request clarification or add additional context in comments.

2 Comments

Ahh, but there is the problem, because I was looking here: stackoverflow.com/questions/9438057/… ...and found the same solution basically. But if you look carefully, the 'uniqueKey's are different, and shouldn't match and be returned. I was thinking something along the lines of array_intersect_ukey first, but haven't tried it yet (working on it now).
Alright, solution is to use array_intersect_assoc instead, at least it looks like it works :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.