【发布时间】:2026-02-07 04:05:02
【问题描述】:
这是一个 var_dump 或从我的 MODEL 返回的数组:
array (size=5)
0 =>
array (size=3)
0 =>
object(stdClass)[19]
public 'X_SIZE' => string '1.75x3' (length=6)
1 =>
object(stdClass)[20]
public 'X_SIZE' => string '1.75x3.5(slim)' (length=14)
2 =>
object(stdClass)[21]
public 'X_SIZE' => string '2x3' (length=3)
1 =>
array (size=3)
0 =>
object(stdClass)[17]
public 'X_PAPER' => string '14ptGlossCoatedCoverwithUV(C2S)' (length=31)
1 =>
object(stdClass)[18]
public 'X_PAPER' => string '14ptPremiumUncoatedCover' (length=24)
2 =>
object(stdClass)[24]
public 'X_PAPER' => string '16ptDullCoverwithMatteFinish' (length=28)
2 =>
array (size=2)
0 =>
object(stdClass)[23]
public 'X_COLOR' => string '1000' (length=4)
1 =>
object(stdClass)[22]
public 'X_COLOR' => string '1002' (length=4)
3 =>
array (size=4)
0 =>
object(stdClass)[26]
public 'X_QTY' => string '100' (length=3)
1 =>
object(stdClass)[25]
public 'X_QTY' => string '250' (length=3)
2 =>
object(stdClass)[29]
public 'X_QTY' => string '500' (length=3)
3 =>
object(stdClass)[30]
public 'X_QTY' => string '1000' (length=4)
4 =>
array (size=3)
0 =>
object(stdClass)[28]
public 'O_RC' => string 'YES' (length=3)
1 =>
object(stdClass)[27]
public 'O_RC' => string 'NO' (length=2)
2 =>
object(stdClass)[33]
public 'O_RC' => string 'NA' (length=2)
我的控制器正在发送到我的视图,在我的视图中,上面的转储是:var_dump($printerOptions)
好的,所以我需要为每个数组创建一个下拉菜单,并为 O_RC 数组创建单选按钮。
我正在使用 codeigniter 并在 foreach 循环内使用 form_dropdown('',$val); 会导致数组中每个键和元素的多个下拉列表。
foreach ($printerOptions as $Options)
{
//var_dump($Options);
foreach ($Options as $Specs)
{
echo $Specs->X_SIZE;
echo $Specs->X_PAPER;
echo $Specs->X_COLOR;
echo $Specs->X_QTY;
echo $Specs->O_RC;
}
}
此 ^ 代码将回显相应数组的预期值,但由于某种原因,我在输出中出现 LOOPING 错误:
Undefined property: stdClass::$X_PAPER
Undefined property: stdClass::$X_COLOR
Undefined property: stdClass::$X_QTY
Undefined property: stdClass::$O_RC
查看我的数组,如何分别为每个数组创建一个下拉菜单?
感谢您的帮助。
模型 /// 更新 ///
class M_Pricing extends CI_Model {
function get_prices()
{
$table_by_product = 'printer_businesscards'; //replace with URI Segment
//Get all the columns in the table set by the page URI of $table_by_product variable
$get_all_col_names = $this->db->list_fields($table_by_product);
//Loop through the column names. All names starting with 'O_' are optional fields for the
//current product. Get all Distinct values and create a radio button list in form.
$resultArray = array();
foreach ($get_all_col_names as $key => $value) {
//get all o_types for the product by column name
if (preg_match('/O_/', $value))
{
$v = (array('Specs' => $value));
foreach ($v as $vals) {
$this->db->select($vals);
$this->db->distinct();
$qX = $this->db->get($table_by_product);
$resultArray[] = $qX->result();
}
}
//Get all x_types for the product by column name. All 'X_' types are specific product options.
//Create a dropdown menu with all DISTINCT product options.
if (preg_match('/X_/', $value))
{
$v = (array('Type' => $value));
foreach ($v as $vals) {
$this->db->select($vals);
$this->db->distinct();
$qX = $this->db->get($table_by_product);
$resultArray[] = $qX->result();
}
}
}
//return $resultArray
//var_dump($resultArray); die;
return $resultArray;
}
【问题讨论】:
-
@Aziz 感谢您的编辑。如何回答或提供一些帮助? ;)
-
你没有包含所有这些属性的单个对象——你有一个对象数组,每个对象都有一个 single 属性。但是它们不能相关,因为每个数组的对象数量不一致。例如,您只有 2 个
X_COLOR,但您有 3 个X_PAPER。这些有什么关系? -
@MichaelBerkowski 感谢您的输入我注意到数组不是正确循环遍历值的方式。如果我发布我的模型代码,您能否帮助我进行适当的修改,以便我可以拥有一个 FOREACH,我将能够在我的 VIEW 上进行迭代——谢谢! :)
-
你发帖我看看
-
@fabio,您可以循环遍历您的内部数组
foreach $Options as $option),或者将您的数组更改为对象数组,最后一个是首选,因为您似乎将数组转错了一边
标签: php arrays codeigniter foreach mysqli