A
A
Andrew2015-08-28 03:00:32
PHP
Andrew, 2015-08-28 03:00:32

How to compare two arrays by the first character of the values ​​and if they match, remove those values ​​from the first array?

Hello, there are two such arrays:

Array
(
    [0] => 0-1439802876-55d1a5fc8879e.jpg
    [1] => 1-1439802876-55d1a5fc8ed30.jpg
    [2] => 2-1439802876-55d1a5fc95a91.jpg
)

Array
(
    [0] => 0-1440719577-55dfa2d9195ec.jpg
    [1] => 2-1440719577-55dfa2d93572b.jpg
    [2] => 3-1440719577-55dfa2d951c51.jpg
)

We need to compare these two arrays by the first digit, and if it matches, then remove the matching values ​​from the first array, and then merge both arrays. Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
magazovski, 2015-08-28
@ntzch

<?php

$a = array(
    "0-1439802876-55d1a5fc8879e.jpg",
    "1-1439802876-55d1a5fc8ed30.jpg",
    "2-1439802876-55d1a5fc95a91.jpg",
);

$b = array(
    "0-1440719577-55dfa2d9195ec.jpg",
    "2-1440719577-55dfa2d93572b.jpg",
    "3-1440719577-55dfa2d951c51.jpg",
);

$a = array_combine(array_map(function ($v) {
    return substr($v, 0, strpos($v, '-'));
}, $a), $a);

$b = array_combine(array_map(function ($v) {
    return substr($v, 0, strpos($v, '-'));
}, $b), $b);

print_r($a+$b);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question