你可以簡單地做,使用內
array_walk()
foreach
。這可能會給你你想要的結果。第1部分:算法 - 沒有樣本數組。 (
Quick Test it Here.)
<?php
$arrResult = array(); //$arr IS THE MAIN ARRAY TO CLEAN UP
$arrFin = array_walk($arr, function($data, $index) use (&$arrResult){
$clone = $data;
$sifts = array('stax', 'stax_value', 'ed_tax', 'ed_tax_value', 'w_ed_tax', 'w_ed_tax_value', 'amount', 'total_discount');
$tempObj = new stdClass();
foreach($clone as $k=>$v){
if(in_array($k, $sifts)){
unset($clone[$k]);
$tempObj->$k = $v;
}
}
$clone['data'] = $tempObj;
$arrResult[] = $clone;
});
var_dump($arrResult);
第2部分:算法 - 與試樣陣列(
Quick Test it Here.)
<?php
$arr = array(
array(
'order_num' =>1400236903,
'status' => 'unpaid',
'payment_rec_date' => '2014-05-16',
'description' => null,
'actual_amount' => 200,
'stax' => 20.00,
'stax_value' => 30.00,
'ed_tax' => 31.21,
'ed_tax_value' => 30.00,
'w_ed_tax' => 25.00,
'w_ed_tax_value' => 55.56,
'amount' => 12000,
'total_discount' => 152,
),
array(
'order_num' =>3098786903,
'status' => 'paid',
'payment_rec_date' => '2014-05-16',
'description' => "Lorem Ipsum",
'actual_amount' => 200,
'stax' => 20.00,
'stax_value' => 30.00,
'ed_tax' => 31.21,
'ed_tax_value' => 30.00,
'w_ed_tax' => 25.00,
'w_ed_tax_value' => 55.56,
'amount' => 12000,
'total_discount' => 152,
),
);
$arrResult = array();
$arrFin = array_walk($arr, function($data, $index) use (&$arrResult){
$clone = $data;
$sifts = array('stax', 'stax_value', 'ed_tax', 'ed_tax_value', 'w_ed_tax', 'w_ed_tax_value', 'amount', 'total_discount');
$tempObj = new stdClass();
foreach($clone as $k=>$v){
if(in_array($k, $sifts)){
unset($clone[$k]);
$tempObj->$k = $v;
}
}
$clone['data'] = $tempObj;
$arrResult[] = $clone;
});
var_dump($arrResult);
的
的var_dump($ arrResult)以上產生下面下面的輸出:(
Quick Test it Here.)
array (size=2)
0 =>
array (size=6)
'order_num' => int 1400236903
'status' => string 'unpaid' (length=6)
'payment_rec_date' => string '2014-05-16' (length=10)
'description' => null
'actual_amount' => int 200
'data' =>
object(stdClass)[2]
public 'stax' => float 20
public 'stax_value' => float 30
public 'ed_tax' => float 31.21
public 'ed_tax_value' => float 30
public 'w_ed_tax' => float 25
public 'w_ed_tax_value' => float 55.56
public 'amount' => int 12000
public 'total_discount' => int 152
1 =>
array (size=6)
'order_num' => int 3098786903
'status' => string 'paid' (length=4)
'payment_rec_date' => string '2014-05-16' (length=10)
'description' => string 'Lorem Ipsum' (length=11)
'actual_amount' => int 200
'data' =>
object(stdClass)[3]
public 'stax' => float 20
public 'stax_value' => float 30
public 'ed_tax' => float 31.21
public 'ed_tax_value' => float 30
public 'w_ed_tax' => float 25
public 'w_ed_tax_value' => float 55.56
public 'amount' => int 12000
public 'total_discount' => int 152
你的問題不是很清楚。請解釋你想達到的目標。你想要一個特定類型的數組,告訴我們它與另一個不同。你有沒有檢查php array_keys,array_search等支持的數組函數? – adi
歡迎來到stackoverflow。要爲您的問題獲得解決方案,請按照http://stackoverflow.com/help/how-to-ask –
https://eval.in/612031 – splash58