以下数组来自$ output变量
Array
(
[title] => Subtotal
[data] => $1,104.05
)
Array
(
[title] => Total Shipping
[data] => $56.45
)
Array
(
[title] => Order total
[data] => $1,160.50
)
Array
(
[border] => top
[title] => Paying by
[data] => PayPal
)
我想将以下数组与上面的数组分开,并希望将其存储在新变量中。 注意:阵列变得动态。
Array
(
[title] => Order total
[data] => $1,160.50
)
请帮助!!
答案 0 :(得分:1)
试试这个,
$array = array(
Array
(
[title] => Subtotal
[data] => $1,104.05
)
Array
(
[title] => Total Shipping
[data] => $56.45
)
Array
(
[title] => Order total
[data] => $1,160.50
)
Array
(
[border] => top
[title] => Paying by
[data] => PayPal
)
);
$second_array = array();
foreach($array as $key => $arr){
if(isset($arr['title']) && $arr['title'] === 'Order total'){
$second_array = $arr;
unset($array[$key]);
}
}
此代码将检查title
密钥是否设置及其值
答案 1 :(得分:1)
$array = [
[
'title' => 'Subtotal',
'data' => '$1,104.05'
],
[
'title' => 'Total Shipping',
'data' => '$56.45'
],
[
'title' => 'Order total',
'data' => '$1,160.50'
],
[
'border' => 'top',
'title' => 'Paying by',
'data' => 'PayPal'
]
];
for($i = 0; $i<=count($array); $i){
if(isset($array[$i]['title']) && $arr[$i]['title'] === 'Order total'){
$newarr = $arr;
unset($array[$key]);
}
}
答案 2 :(得分:0)
您应该使用array_filter()。
$array = [
[
'title' => 'Subtotal',
'data' => '$1,104.05'
],
[
'title' => 'Total Shipping',
'data' => '$56.45'
],
[
'title' => 'Order total',
'data' => '$1,160.50'
],
[
'border' => 'top',
'title' => 'Paying by',
'data' => 'PayPal'
]
];
$array = array_filter($array, function($ar) {
return ($ar['title'] == 'Order total'); //check condition and return record with key
});
$newArray = []; // your new array
$key = key($array); // get key of result array
$newArray = $array[$key]; // assign result record to new array
echo "<pre>"; print_r($newArray);
<强>输出强>: -
Array
(
[title] => Order total
[data] => $1,160.50
)
希望它会对你有所帮助: - )