Есть ли другой способ преобразования многомерного массива? - PullRequest
0 голосов
/ 13 января 2020

У меня есть многомерный массив, и я хочу преобразовать только определенные ключи c в многомерный массив.

Вот пример многомерного массива:

array (
    0 => 
    array (
        'id' => 0,
        'payment_method' => 2,
        'amount' => 100,
        'check_no' => '',
        'remarks' => '',
        'check_date' => '2020-01-13',
        'bank_account_id' => NULL,
        'surcharge_id' => NULL,
        'card_number' => '',
        'customer_name' => '',
        'merchant_number' => '',
        'batch_number' => '',
        'approval_number' => '',
        'other_reference_1' => '',
        'other_reference_3' => '',
        'other_reference_2' => '',
        'based_amount' => 0,
        'actual_amount' => 0,
        'total_charge' => 0,
        'transaction_id' => 47,
        'transaction_status' => 0,
    ),
    1 => 
    array (
        'id' => 0,
        'payment_method' => 2,
        'amount' => 150,
        'check_no' => '',
        'remarks' => '',
        'check_date' => '2020-01-13',
        'bank_account_id' => NULL,
        'surcharge_id' => NULL,
        'card_number' => '',
        'customer_name' => '',
        'merchant_number' => '',
        'batch_number' => '',
        'approval_number' => '',
        'other_reference_1' => '',
        'other_reference_3' => '',
        'other_reference_2' => '',
        'based_amount' => 0,
        'actual_amount' => 0,
        'total_charge' => 0,
        'transaction_id' => 47,
        'transaction_status' => 0,
    ),
    2 => 
    array (
        'id' => 0,
        'payment_method' => 2,
        'amount' => 100,
        'check_no' => '',
        'remarks' => '',
        'check_date' => '2020-01-13',
        'bank_account_id' => NULL,
        'surcharge_id' => NULL,
        'card_number' => '',
        'customer_name' => '',
        'merchant_number' => '',
        'batch_number' => '',
        'approval_number' => '',
        'other_reference_1' => '',
        'other_reference_3' => '',
        'other_reference_2' => '',
        'based_amount' => 0,
        'actual_amount' => 0,
        'total_charge' => 0,
        'transaction_id' => 47,
        'transaction_status' => 0,
    )
)  

Я только хочу указать c ключи для преобразования, например, я хочу только транзакции_id, транзакции_статуса, суммы и т. д. c.

Тогда ожидаемый результат будет:

array (
    0 =>
    array (
        'transaction_id' => 47,
        'transaction_status' => 0,
        'amount' => 100
    ),
    1 =>
    array (
        'transaction_id' => 47,
        'transaction_status' => 0,
        'amount' => 100
    ),
    2 =>
    array (
        'transaction_id' => 47,
        'transaction_status' => 0,
        'amount' => 100
    )
)

Спасибо! !

Ответы [ 3 ]

1 голос
/ 13 января 2020

Вы можете использовать карта массива , ключ массива-пересечения и ключи массива-заполнения как:

$keys = ['transaction_id','transaction_status','amount'];
$keys = array_fill_keys($keys,0);
$arr = array_map(function($e) use ($keys) { return array_intersect_key($e, $keys);}, $arr);

Живой пример: 3v4l

1 голос
/ 13 января 2020

Использование forecah l oop возможно, вот так.

<?php 
$a = array (
    0 => 
    array (
        'id' => 0,
        'payment_method' => 2,
        'amount' => 100,
        'check_no' => '',
        'remarks' => '',
        'check_date' => '2020-01-13',
        'bank_account_id' => NULL,
        'surcharge_id' => NULL,
        'card_number' => '',
        'customer_name' => '',
        'merchant_number' => '',
        'batch_number' => '',
        'approval_number' => '',
        'other_reference_1' => '',
        'other_reference_3' => '',
        'other_reference_2' => '',
        'based_amount' => 0,
        'actual_amount' => 0,
        'total_charge' => 0,
        'transaction_id' => 47,
        'transaction_status' => 0,
    ),
    1 => 
    array (
        'id' => 0,
        'payment_method' => 2,
        'amount' => 150,
        'check_no' => '',
        'remarks' => '',
        'check_date' => '2020-01-13',
        'bank_account_id' => NULL,
        'surcharge_id' => NULL,
        'card_number' => '',
        'customer_name' => '',
        'merchant_number' => '',
        'batch_number' => '',
        'approval_number' => '',
        'other_reference_1' => '',
        'other_reference_3' => '',
        'other_reference_2' => '',
        'based_amount' => 0,
        'actual_amount' => 0,
        'total_charge' => 0,
        'transaction_id' => 47,
        'transaction_status' => 0,
    ),
    2 => 
    array (
        'id' => 0,
        'payment_method' => 2,
        'amount' => 100,
        'check_no' => '',
        'remarks' => '',
        'check_date' => '2020-01-13',
        'bank_account_id' => NULL,
        'surcharge_id' => NULL,
        'card_number' => '',
        'customer_name' => '',
        'merchant_number' => '',
        'batch_number' => '',
        'approval_number' => '',
        'other_reference_1' => '',
        'other_reference_3' => '',
        'other_reference_2' => '',
        'based_amount' => 0,
        'actual_amount' => 0,
        'total_charge' => 0,
        'transaction_id' => 47,
        'transaction_status' => 0,
    )
);
$new_array = array();
foreach($a as $value){
    $new_array[] = array(
            'transaction_id' => $value['transaction_id'],
            'transaction_status' => $value['transaction_status'],
            'amount' => $value['amount']
            );
}
print_r($new_array);

Вывод:

Array
(
    [0] => Array
        (
            [transaction_id] => 47
            [transaction_status] => 0
            [amount] => 100
        )

    [1] => Array
        (
            [transaction_id] => 47
            [transaction_status] => 0
            [amount] => 150
        )

    [2] => Array
        (
            [transaction_id] => 47
            [transaction_status] => 0
            [amount] => 100
        )

)

Надеюсь, это поможет вам.

0 голосов
/ 13 января 2020

Использование Laravel Array Helper

use Illuminate\Support\Arr;

$newArray = [];
$keys = ['transaction_id', 'transaction_status', 'amount'];

foreach($array as $item) {
    $newArray[] = Arr::only($item, $keys);
}

return $newArray;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...