我在形成多维数组时遇到了麻烦。 我从数据库接收数据,然后我需要创建将通过api发送以进行打包的数组。
首先我从表单
获取值$idNaloga = $_GET['idNaloga'];
$duzina = $_GET['duzina'];
$sirina = $_GET['sirina'];
$visina = $_GET['visina'];
首先我查询预告片:
$truckTrailer = array(
'w' => $sirina,
'h' => $visina,
'd' => $duzina,
'max_wg' => '15000',
'id' => $kamion1
);
然后我使用这些值进行查询
$stmt = "SELECT DISTINCT(nazivPanela) as paket FROM `paneli_pakovanja_2017` WHERE idNaloga = $idNaloga";
$q = $conn->query($stmt);
$nazivPaketa = array();
while($r = $q -> fetch()){
$nazivPaketa[] = $r['paket'];
}
获取数据包名称后,我会查询每个数据包。在这个查询中,我应该得到每个看起来
的数据包array(
'w' => '100',
'h' => '90',
'd' => '1350',
'q' => '18',
'vr' => '1',
'wg' => '0',
'id' => 'Paket'
),
但我不,我从下面的查询中只得到一个数组,但我应该得到7.
foreach($nazivPaketa as $paket) {
$stmt2 = "SELECT max(duzina) as duzina, visinaPaketa, sirinaPaketa, nazivPanela FROM `paneli_pakovanja_2017` WHERE idNaloga = $idNaloga AND nazivPanela = $paket";
$q = $conn -> query($stmt2);
while($r=$q->fetch()) {
$paketi = array(
'w' => $r['sirinaPaketa'],
'h' => $r['visinaPaketa'],
'd' => $r['duzina'],
'q' => '1',
'vr' => '1',
'wg' => '0',
'id' => $r['nazivPanela']
);
}
}
最后我需要表单数组,它看起来像:
$data = array(
'bins' => array( // this array I have it is truck trailer
array(
'w' => '250',
'h' => '360',
'd' => '1360',
'max_wg' => '15000',
'id' => '1'
)
),
'items' => array( *// array I should get from $paketi
array(
'w' => '100',
'h' => '90',
'd' => '1350',
'q' => '18',
'vr' => '1',
'wg' => '0',
'id' => 'Paket'
),
array(
'w' => '100',
'h' => '90',
'd' => '900',
'q' => '23',
'vr' => '1',
'wg' => '0',
'id' => 'Paket2'
),
array(
'w' => '100',
'h' => '90',
'd' => '400',
'q' => '18',
'vr' => '1',
'wg' => '0',
'id' => 'Paket'
)
),
不知道我使用正确的方式,但需要某种帮助
答案 0 :(得分:1)
未经测试但也许您可以尝试如下。变量$paketi
在循环的每次迭代中被覆盖,但是你需要为所有查询存储它的记录,所以在外部循环之前声明一个数组并将内部循环中的内容添加到它中
$data=array();
foreach( $nazivPaketa as $paket ) {
$stmt2 = "SELECT max(duzina) as duzina, visinaPaketa, sirinaPaketa, nazivPanela
FROM `paneli_pakovanja_2017`
WHERE idNaloga = $idNaloga AND nazivPanela = $paket";
$q = $conn->query( $stmt2 );
$paketi=array();
while( $r=$q->fetch() ) {
$paketi[] = array(
'w' => $r['sirinaPaketa'],
'h' => $r['visinaPaketa'],
'd' => $r['duzina'],
'q' => '1',
'vr' => '1',
'wg' => '0',
'id' => $r['nazivPanela']
);
}
$data[]=$paketi;
}
echo '<pre>',print_r($data,1),'</div>';
要添加其他阵列,您可以尝试:
$data['bins']=$truckTrailer; /* assuming `$truckTrailer` is an array */
答案 1 :(得分:0)
您正在覆盖循环中的$ packeti变量...您应该将值添加到数组中。
$q = $conn -> query($stmt2);
$paketi = array();
while($r=$q->fetch()) {
$paketi[] = array(
'w' => $r['sirinaPaketa'],
'h' => $r['visinaPaketa'],
'd' => $r['duzina'],
'q' => '1',
'vr' => '1',
'wg' => '0',
'id' => $r['nazivPanela']
);
}
}
$ packeti []将确保您的包信息被添加到数组中,而不是完全替换它。