Frage

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.

War es hilfreich?

Lösung

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
)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top