1

i have multi array and the count for all arrays is equal , the problem that i want to loop by the index , like to start with the index 0 in the first array fiscal_year then go to the second array revenue with index 0 after finish,start again to loop for the index 1 for

it will be like rows,and every row will be like this

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"
  }

my code is like this and it is not work good,it is insert the first row and stop

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');

}
2
  • no man , i add break to jump to the next array Commented Apr 20, 2016 at 15:47
  • I have seen it later... but why do you use an foreach in this place then? Commented Apr 20, 2016 at 15:48

2 Answers 2

2

You just need a single for loop, not three nested ones:

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"; 
}

Which produces:

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
Sign up to request clarification or add additional context in comments.

Comments

0

you first need to have a foreach then is optional loop (while, for, etc) like

$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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.