我正在为糖制作一个库存控制系统,我需要打印并比较值,当我想打印一个数组多维数据的例子时,就会出现问题,这是我的数组:
Array
(
[0] => Array
(
[Azucar] => Array
(
[id] => 1
[nombre] => 21
[produccion] => verano.
[cantidad] => 10032.32
[fecha] =>
)
[grupo] => Array
(
[id] => 2
[categoria] => azucar
[subcategoria] => ingenio
[created] => 2008-02-13 18:34:56
)
[AzucarExistencia] => Array
(
[0] => Array
(
[id] => 1
[cantidad] => 3250
[cantidadtotal] => 325.12
[sacarosa] => 32
[refinada] => 956
)
[1] => Array
(
[id] => 2
[cantidad] => 4000
[cantidadtotal] => 564
[sacarosa] => 12
[refinada] => 780
)
[2] => Array
(
[id] => 3
[cantidad] => 4564
[cantidadtotal] => 654.32
[sacarosa] => 5451
[refinada] => 45
)
[3] => Array
(
[id] => 4
[cantidad] => 3244
[cantidadtotal] => 21.1
[sacarosa] => 123
[refinada] => 123.45
)
)
)
[1] => Array
(
[Azucar] => Array
(
[id] => 2
[nombre] => 32
[produccion] => verano.
[cantidad] => 9032.32
[fecha] =>
)
[grupo] => Array
(
[id] => 3
[categoria] => azucar
[subcategoria] => ingenio
[created] => 2008-02-13 18:34:56
)
[AzucarExistencia] => Array
(
[0] => Array
(
[id] => 6
[cantidad] => 3250
[cantidadtotal] => 325.12
[sacarosa] => 32
[refinada] => 956
)
[1] => Array
(
[id] => 7
[cantidad] => 4000
[cantidadtotal] => 564
[sacarosa] => 12
[refinada] => 780
)
[2] => Array
(
[id] => 8
[cantidad] => 4564
[cantidadtotal] => 654.32
[sacarosa] => 5451
[refinada] => 45
)
[3] => Array
(
[id] => 9
[cantidad] => 3244
[cantidadtotal] => 21.1
[sacarosa] => 123
[refinada] => 123.45
)
[4] => Array
(
[id] => 10
[cantidad] => 4564
[cantidadtotal] => 654.32
[sacarosa] => 5451
[refinada] => 45
)
[5] => Array
(
[id] => 11
[cantidad] => 3244
[cantidadtotal] => 21.1
[sacarosa] => 123
[refinada] => 123.45
)
)
)
)
我需要比较AzucarExistencia中的所有值,第一个foreach就是这样:
<?php foreach ($azucares as $azucar): ?>
<tr>
<td style="text-align: center;"><?php echo $azucar['Azucar']['username']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Azucar']['nombre']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Azucar']['apellido']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Azucar']['email']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Grupo']['categoria']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Grupo']['subcategoria']; ?></td>
</tr>
<?php endforeach; ?>
<?php unset($azucar); ?>
然后需要比较AzucarExistencia中的值,尝试:
<?php foreach ($azucares as $azucar): ?>
<tr>
<td style="text-align: center;"><?php echo $azucar['Azucar']['username']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Azucar']['nombre']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Azucar']['apellido']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Azucar']['email']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Grupo']['categoria']; ?></td>
<td style="text-align: center;"><?php echo $azucar['Grupo']['subcategoria']; ?></td>
<?php foreach ($azucar as $azucarexistencia): ?>
<td style="text-align: center;"><?php
if ($azucarexistencia['AzucarExistencia']['cantidad']<1500) {
echo 'peligro';
}
?></td>
</tr>
<?php endforeach; ?>
<?php unset($azucar); ?>
现在错误是
未定义的索引:azucarexistencia
或
未定义索引:AzucarExistencia
用debugkit检查了这些值,好吧它只是做了一个好的foreach,我尝试但我现在不怎么样。
模型
<?php
/**
*
*/
class Azucar extends AppModel
{
public $name ='User';
public $useTable = 'users';
public $primaryKey = 'id';
public $belongsTo = 'Grupo';
public $hasOne = 'Perfil';
public $hasMany = array('Deposito','AzucarExistencia');
}
?>
控制器
<?php
/**
*
*/
class AzucaresController extends AppController
{
public $components = array('Session','RequestHandler');
public function index()
{
$this->loadModel('Azucar');
$this->paginate = array(
'conditions' => array('Grupo.categoria' => 'Azucar'),
'limit' => 25
);
$this->set('azucares', $this->paginate('Azucar'));
}
public function logout() {
$this->redirect($this->Auth->logout());
}
}
?>
答案 0 :(得分:1)
将循环替换为
<?php foreach ($azucar['AzucarExistencia'] as $azucarexistencia): ?>
<td style="text-align: center;"><?php
if ($azucarexistencia['cantidad']<1500) {
echo 'peligro';
}
?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
你做错了是因为你试图遍历$azucar
的所有索引,所以它会遍历$azucar["Azucar"]
$azucar["grupo"]
$azucar["AzucarExistencia"]
但只有$azucar["AzucarExistencia"]
需要通过
答案 1 :(得分:0)
<?php
foreach ($azucares as $azucar){
foreach ($azucar['AzucarExistencia'] as $azucarexistencia){
if ($azucarexistencia['cantidad']<1500) {
echo 'peligro';
}
}
}
?>