Heim > Fragen und Antworten > Hauptteil
$oldArr = array(
array(
0 => 'A0060750',
1 => 9787560618852),
array(
0 => '',
1 => 9787560618855),
array(
0 => '',
1 => 9787560618856),
array(
0 => 'A00607507',
1 => 9787560618857),
array(
0 => '',
1 => 9787560618858)
);
Konvertieren in
$newArr = array(
array(
0 => 'A0060750',
1 => 9787560618852),
array(
0 => 'A0060750',
1 => 9787560618855),
array(
0 => 'A0060750',
1 => 9787560618856),
array(
0 => 'A00607507',
1 => 9787560618857),
array(
0 => 'A00607507',
1 => 9787560618858)
);
简单说就是,如果二维数组中的,$oldArr[i][0] 的值为空,就以上一个不为空的值填充。
PHP中文网2017-07-03 11:42:02
<?php
$oldArray = [
['A0060750', 9787560618852],
['', 9787560618855],
['', 9787560618856],
['A00607507', 9787560618857],
['', 9787560618857]
];
$lastId = '';
foreach ($oldArray as &$item) {
if ($item[0]) {
$lastId = $item[0];
continue;
}
$item[0] = $lastId;
}
var_dump($oldArray);
迷茫2017-07-03 11:42:02
$sPrevName = '';
$aNewArr = [];
foreach($oldArr as $v){
if($v[0] == ''){
$v[0] = $sPrevName;
}
$aNewArr[] = $v;
$sPrevName = $v[0];
}