0
2 차원 배열이 있습니다. db를 삽입하려고합니다. 내 데이터베이스가 내가이 출력을 가지고 일단 내가 배열2 차원 배열을 db에 삽입
while($r = $q->fetch())
$dat[]=array($r['Length'],$r['Quantity']);
Array
(
[0] => Array
(
[0] => Array
(
[0] => 12500
[1] => 8
)
)
[1] => Array
(
[0] => Array
(
[0] => 12500
[1] => 8
)
)
[2] => Array
(
[0] => Array
(
[0] => 12500
[1] => 8
)
)
[18] => Array
(
[0] => Array
(
[0] => 6400
[1] => 6
)
[1] => Array
(
[0] => 5558
[1] => 2
)
)
을 만들
ID | ORDERID | DIMENSION | QUANTITY | PACKETNAME
을 구성되어, 내가 위에서 언급 한, DB에 삽입해야합니다.
[1] => Array, dimension = 12500, 수량 8이고 패킷 이름은 2 이지만 [1] => Array의 경우 패킷 이름은 6400과 5558 모두 19가됩니다. 이름 - 1 씩 증가시킵니다.이 방법을 삽입 할 수 있는지 알 수 없습니다. 많은이있다 : 모든, 귀하의 경우 3 (부모, 자식, subchild에서 - 어떤 도움을
while($r = $q->fetch()) $dat[]=array($r['Duzina'] ,$r['Kolicina']);
// package size:
$n=10;
$cnt=0; // initialize package counter
$pack=array(); // set up first package
$d=array_shift($dat); // get first lot $d from $dat
do {
if ($d[1]>0){
$take=min($d[1],$n-$cnt); // decide how much to take out ...
$cnt+=$take; // increase package counter
$d[1]-=$take;$pack[]=array($d[0],$take); // tranfer the goods ...
// if package is full, start next one
if ($cnt==$n) {$packs[]=$pack; $pack=array(); $cnt=0;}
} else if(count($dat)>0){ // still elements in $dat left?
$d=array_shift($dat); // get the next lot $d from $dat
} else break;
} while(true);
if ($cnt>0) $packs[]=$pack; // collect the last "stranded" package ...
echo "<pre>";
//echo str_replace("]],","]],\n ",json_encode($packs)); // as JSON string
print_r($packs); // conventional (bulky) PHP object notation
echo "</pre>";
'json_encode()'는 배열을 저장할 수있는 문자열로 바꿉니다. 나중에이 데이터를 가져 오면'json_decode()'를 사용하여 배열이나 객체로 되돌릴 수 있습니다. – GrumpyCrouton
출력이 루프로 생성하는 것과 같은 배열로 보이지 않습니다. 추가 배열 중첩 수준이 있습니다. – Barmar
나는 이것을 전혀 모른다. 어디서나 '12580' 또는'5400 '이 보이지 않습니다.'12500'과 '6400'을 의미합니까? 그러나'6400'은'[18]'이 아니라 '[1]'에 있습니다. – Barmar