我有一个多维数组,我只想转换多维数组中的特定键。
这是示例多维数组:
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,
)
)
我只想要特定的键进行转换,例如,我只想要transaction_id,transaction_status,金额等。
那么预期的输出将是:
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
)
)
谢谢!
答案 0 :(得分:1)
使用forecah
这样循环是可能的。
<?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
)
)
希望这对您有所帮助。
答案 1 :(得分:1)
您可以将array-map,array-intersect-key和array-fill-keys用作:
$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
答案 2 :(得分:0)
使用Laravel阵列帮助器
use Illuminate\Support\Arr;
$newArray = [];
$keys = ['transaction_id', 'transaction_status', 'amount'];
foreach($array as $item) {
$newArray[] = Arr::only($item, $keys);
}
return $newArray;