PHP-Array-Füllung
给我你的怀抱
给我你的怀抱 2017-07-03 11:40:02
0
2
680

$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] 的值为空,就以上一个不为空的值填充。
给我你的怀抱
给我你的怀抱

Antworte allen(2)
洪涛
<?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);
迷茫
$sPrevName = '';
$aNewArr = [];
foreach($oldArr as $v){
    if($v[0] == ''){
        $v[0] = $sPrevName;
    }
    $aNewArr[] = $v;
    $sPrevName = $v[0];
}
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage