Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用多维数组?_Php_Arrays_Codeigniter_Foreach_Mysqli - Fatal编程技术网

Php 如何使用多维数组?

Php 如何使用多维数组?,php,arrays,codeigniter,foreach,mysqli,Php,Arrays,Codeigniter,Foreach,Mysqli,以下是从我的模型返回的var_转储或数组: 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' => stri

以下是从我的模型返回的var_转储或数组:

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和form_下拉菜单$val;foreach循环的内部会导致数组中每个键和元素出现多个下拉列表

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;
    }
}
这段代码将回显相应数组的预期值,但由于某些原因,我在输出中出现循环错误:

Undefined property: stdClass::$X_PAPER
Undefined property: stdClass::$X_COLOR
Undefined property: stdClass::$X_QTY
Undefined property: stdClass::$O_RC
查看我的阵列如何分别为每个阵列创建下拉菜单

谢谢你的帮助

模型///更新///
据我所知,结果数组应该是object或hash,如下所示:

array('Type'=>array(
   'X_SIZE'=>array('1.75x3','1.75x3.5(slim)','2x3'),
   'X_PAPER'=>array('14ptGlossCoatedCoverwithUV(C2S)','14ptPremiumUncoatedCover'),
///...),
'Specs'=>array(
//。。。 ;

通过这种结构进行循环:

//Types
echo 'Types';
foreach ($Options['Types'] as $type=>$values) {
echo $type;
echo "<select>";
foreach ($values as $value) {
echo "<option value="$value">$value</option>";
}
echo "</select>";
}

//Specs
echo 'Specs';
foreach ($Options['Specs'] as $type=>$values) {
 echo $type;
 echo "<select>";
 foreach ($values as $value) {
   echo "<option value="$value">$value</option>";
 }
 echo "</select>";
}
型号:

<?php
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();
        $resultArray['Options']=array();
        $resultArray['Specs']=array();

        foreach ($get_all_col_names as $key => $value) {
            //get all o_types for the product by column name
            $opt_type="";
            if (preg_match('/O_/', $value)) {$opt_type="Options";} elseif
               (preg_match('/X_/', $value)) {$opt_type="Specs";} else {continue;}
               if (!isset($resultArray[$opt_type][$value])) {$resultArray[$opt_type][$value]=array();}
                    $this->db->select($value);
                    $this->db->distinct();
                    $qX = $this->db->get($table_by_product);
                    foreach ($qX->result() as $v) {
                     $resultArray[$opt_type][$value][]=$v->$value;
                    }
           }      

        //return $resultArray
        //var_dump($resultArray); die;
        return $resultArray;
    }
}

@谢谢你的编辑。。给我一个答案或者一些帮助怎么样;您没有包含所有这些属性的单个对象-您有对象数组,每个对象都有一个属性。但是,它们不能相互关联,因为每个数组的对象数不一致。例如,您只有2张X_颜色的纸,但有3张X_颜色的纸。这些是如何关联的?@MichaelBerkowski感谢您的输入,我注意到数组并没有像它应该的那样正确循环值。如果我发布了我的模型代码,你能帮我做适当的修改吗?这样我就可以有一个FOREACH,我就可以在我的视图上迭代-谢谢如果你发邮件,我会看一看it@fabio,您可以将内部数组foreach$Options循环为$option,或者将数组更改为对象数组,最后一个是优先的,因为您似乎将该数组转到了错误的一侧
<?php
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();
        $resultArray['Options']=array();
        $resultArray['Specs']=array();

        foreach ($get_all_col_names as $key => $value) {
            //get all o_types for the product by column name
            $opt_type="";
            if (preg_match('/O_/', $value)) {$opt_type="Options";} elseif
               (preg_match('/X_/', $value)) {$opt_type="Specs";} else {continue;}
               if (!isset($resultArray[$opt_type][$value])) {$resultArray[$opt_type][$value]=array();}
                    $this->db->select($value);
                    $this->db->distinct();
                    $qX = $this->db->get($table_by_product);
                    foreach ($qX->result() as $v) {
                     $resultArray[$opt_type][$value][]=$v->$value;
                    }
           }      

        //return $resultArray
        //var_dump($resultArray); die;
        return $resultArray;
    }
}