簡體   English   中英

如何在foreach中使用多維數組中的索引循環

[英]how to loop with index in multidimaintial array in foreach

我有多個陣列和所有陣列的數量是相等的,我通過索引要循環的問題,先來第一數組中的索引0 fiscal_year然后進入第二陣列收益與指數0后結束,開始再次為索引1循環

就像行,每一行都是這樣

1996-12 | 101.2

1997-12 | 249.801

array(15) {
  ["fiscal_year"]=>
  array(21) {
    [0]=>
    string(7) "1996-12"
    [1]=>
    string(7) "1997-12"
    [2]=>
    string(7) "1998-12"
    [3]=>
    string(7) "1999-12"
    [4]=>
    string(7) "2000-12"
    [5]=>
    string(7) "2001-12"
    [6]=>
    string(7) "2002-12"
    [7]=>
    string(7) "2003-12"
    [8]=>
    string(7) "2004-12"
    [9]=>
    string(7) "2005-12"
    [10]=>
    string(7) "2006-12"
    [11]=>
    string(7) "2007-12"
    [12]=>
    string(7) "2008-12"
    [13]=>
    string(7) "2009-12"
    [14]=>
    string(7) "2010-12"
    [15]=>
    string(7) "2011-12"
    [16]=>
    string(7) "2012-12"
    [17]=>
    string(7) "2013-12"
    [18]=>
    string(7) "2014-12"
    [19]=>
    string(7) "2015-12"
    [20]=>
    string(3) "TTM"
  }
  ["revenue"]=>
  array(21) {
    [0]=>
    string(5) "101.2"
    [1]=>
    string(7) "249.801"
    [2]=>
    string(7) "493.699"
    [3]=>
    string(7) "548.891"
    [4]=>
    string(7) "543.159"
    [5]=>
    string(7) "536.404"
    [6]=>
    string(7) "474.765"
    [7]=>
    string(7) "509.099"
    [8]=>
    string(7) "588.991"
    [9]=>
    string(7) "643.405"
    [10]=>
    string(7) "732.012"
    [11]=>
    string(6) "808.35"
    [12]=>
    string(7) "777.969"
    [13]=>
    string(7) "758.925"
    [14]=>
    string(7) "773.743"
    [15]=>
    string(7) "652.235"
    [16]=>
    string(7) "650.632"
    [17]=>
    string(7) "667.031"
    [18]=>
    string(7) "636.799"
    [19]=>
    string(7) "594.883"
    [20]=>
    string(7) "594.883"
  }

我的代碼是這樣的,它不能正常工作,它是插入第一行並停止

for ($x = 0; $x <= count($company_data['fiscal_year']); $x++) {
    foreach ($company_data as $key => $value) {
        foreach($value as $key2 => $value2){
            $this->db->set('company_name', $this->input->post('company_name'));
            $this->db->set('company_code', $this->input->post('company_code'));
            $this->db->set('company_hide', 1);
            $this->db->set('company_created', time());
            $this->db->set($key,$value2);
            break;
        }

    }
    $this->db->insert('d_company');

}

您只需要一個for循環,而不是三個嵌套的循環:

for ($x = 0; $x < count($company_data['fiscal_year']); $x++) {
    $fiscal_year = $company_data['fiscal_year'][$x];
    $revenue     = $company_data['revenue'][$x];
    print "$fiscal_year: $revenue\n"; 
}

產生:

1996-12: 101.2
1997-12: 249.801
1998-12: 493.699
1999-12: 548.891
2000-12: 543.159
2001-12: 536.404
2002-12: 474.765
2003-12: 509.099
2004-12: 588.991
2005-12: 643.405
2006-12: 732.012
2007-12: 808.35
2008-12: 777.969
2009-12: 758.925
2010-12: 773.743
2011-12: 652.235
2012-12: 650.632
2013-12: 667.031
2014-12: 636.799
2015-12: 594.883
TTM: 594.883

您首先需要有一個foreach然后是可選循環(while,for等),例如

$to_update = '';

foreach($company_data as $item){
    $count = count($item);

    for($i = 0; $i <= $count; $i++){
        switch($item[$i]){
            case 'company_name':
                                $to_update .= $item[$i] . ' => ' . $this->input->post('value') . ',';
                                break;
            case 'company_hide':
                                $to_update .= $item[$i] . ' => 1,';
                                break;
            case 'company_created':
                                $to_update .= $item[$i] . ' => ' . $time() . ',';
                                break;
            default:
                                $to_update .= $item[$i] . ' => ' . $this->input->post('value') . ',';
                                break;
    }
}

$this->model->upate($to_update);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM