PHP比较元素值并推送到数组

时间:2017-03-29 08:48:38

标签: php foreach

我需要比较具有相同元素值ex的数组中的元素。 $array[0]['change'] == $array[1]['change']如果它是真的,那就是make array push。

我有这样的数组

[
    (int) 0 => [
        'id' => (int) 2,
        'uuid' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2',
        'change' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2',
        'company_id' => (int) 18
    ],
    (int) 1 => [
        'id' => (int) 3,
        'uuid' => 'c3f388bc-9efb-4c72-b50e-3b6a9075d919',
        'change' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2',
        'company_id' => (int) 11
    ],
    (int) 2 => [
        'id' => (int) 4,
        'uuid' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e',
        'change' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e',
        'company_id' => (int) 17
    ],
    (int) 3 => [
        'id' => (int) 5,
        'uuid' => '52732822-11a9-4c00-8685-2493b88983f8',
        'change' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e',
        'company_id' => (int) 19
    ],
    (int) 4 => [
        'id' => (int) 6,
        'uuid' => '9a356688-d08b-42ee-b26d-19d76bf6543b',
        'change' => '9a356688-d08b-42ee-b26d-19d76bf6543b',
        'company_id' => (int) 10
    ],
    (int) 5 => [
        'id' => (int) 7,
        'uuid' => '8dbe1a81-d722-4261-9b13-ef0b68cbb759',
        'change' => '8a356688-d08b-42ee-b26d-19d76bf6543b',
        'company_id' => (int) 18
    ],
    (int) 6 => [
        'id' => (int) 8,
        'uuid' => '701a896a-7d19-4cdd-80a4-f9ca7042945b',
        'change' => '701a896a-7d19-4cdd-80a4-f9ca7042945b',
        'company_id' => (int) 19
    ],
    (int) 7 => [
        'id' => (int) 9,
        'uuid' => '64e146bf-5d76-483f-992c-274cde1202ce',
        'change' => '601a896a-7d19-4cdd-80a4-f9ca7042945b',
        'company_id' => (int) 20
    ]
]

我需要得到这样的结果

[
    (int) 0 => [
        (int) 0 => [
            'id' => (int) 2,
            'uuid' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2',
            'change' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2',
            'company_id' => (int) 18
        ],
        (int) 1 => [
            'id' => (int) 3,
            'uuid' => 'c3f388bc-9efb-4c72-b50e-3b6a9075d919',
            'change' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2',
            'company_id' => (int) 18
        ],
    ],
    (int) 1 => [
        (int) 0 => [
            'id' => (int) 4,
            'uuid' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e',
            'change' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e',
            'company_id' => (int) 18
        ],
        (int) 1 => [
            'id' => (int) 5,
            'uuid' => '52732822-11a9-4c00-8685-2493b88983f8',
            'change' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e',
            'company_id' => (int) 18
        ],
    ],
    (int) 2 => [
        'id' => (int) 6,
        'uuid' => '9a356688-d08b-42ee-b26d-19d76bf6543b',
        'change' => '9a356688-d08b-42ee-b26d-19d76bf6543b',
        'company_id' => (int) 18
    ],
    (int) 3 => [
        'id' => (int) 7,
        'uuid' => '8dbe1a81-d722-4261-9b13-ef0b68cbb759',
        'change' => '8a356688-d08b-42ee-b26d-19d76bf6543b',
        'company_id' => (int) 18
    ],
    .
    .
    .   
]

我试图使2个数组具有相同的值而另一个具有不同的值,而不是make merge但结果不是我想要的......我希望有“更好”的解决方案(可能更具可读性) )。

    $exchange_array = [];
    $item_array = [];
    foreach ($order->items as $item) {
      foreach ($order->items as $exchange) {
        if (
          isset($item->change) && isset($exchange->change) &&
          $item->change == $exchange->change &&
          $item->id != $exchange->id
        ) {
          $exchange_array[] = $exchange;
        }
        if (
          !isset($item->change) && !isset($exchange->change) &&
          $item->id != $exchange->id
        ) {
          $item_array[] = $exchange;
        }

      }
    }

    $item = array_merge($exchange_array,$item_array);

谢谢!

2 个答案:

答案 0 :(得分:2)

您可以使用foreach或array_map,如下所示:

$arrays = [
    ['change' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2'],
    ['change' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2'],
    ['change' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e'],
    ['change' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e'],
    ['change' => '9a356688-d08b-42ee-b26d-19d76bf6543b'],
    ['change' => '8a356688-d08b-42ee-b26d-19d76bf6543b'],
    ['change' => '701a896a-7d19-4cdd-80a4-f9ca7042945b'],
    ['change' => '601a896a-7d19-4cdd-80a4-f9ca7042945b'],
];

$list = [];
foreach ($arrays as $array) {
    $list[$array['change']][] = $array;
}

print_r(array_values($list));

// if you would like to use array_map instead

$list = [];
array_map(function ($array) use (&$list) {
    $list[$array['change']][] = $array;
}, $arrays);

print_r(array_values($list));

现场演示:https://3v4l.org/1JO58

答案 1 :(得分:0)

使用以下方法:

$result = [];
foreach ($order->items as $item) {
    if (isset($result[$item['change']])) {
        // check if nested array is a single item(not multidimensional) 
        if (isset($result[$item['change']]['change'])) {
            $result[$item['change']] = [$result[$item['change']], $item];
        } else {
            $result[$item['change']][] = $item;
        }
    } else {
        $result[$item['change']] = $item;
    }
}

$result = array_values($result);