2016-05-06 13 views
1

Ich versuche, zwei Arrays zusammenführen, aber NULL bekommen. Unten ist mein CodeWie mehrere Arrays in Codeigniter zusammengeführt werden

$a = 1; 
foreach($codes as $values) { 
$id = $values['id']; 
$post_data = array ( 
    "id" => $id, 
    "name" => $this->input->post('Name'), 
    "from_date" => $this->input->post('FromDate'), 
    "to_date" => $this->input->post('ToDate') 
    ); 
    $this->data['output' . $a++] = $this->my_modal->simple_post($post_data); 
} 

$this->data['output'] = array_merge($this->data['output1'], $this->data['output2']); 

var_dump($this->data['output']); 

Alle Vorschläge werden geschätzt. Dank ..

Antwort

0

Sie haben den ersten Parameter (NULL) von array_merge();

Und was $this->input->$id ist zu löschen? Meinst du nicht $id?

Und in diesem Umfeld ist es besser, verwenden array_push();:

$a = 1; 
$this->data['output'] = array(); 
foreach($codes as $values) 
{ 
    $id = $values['id']; 
    $post_data = array ( 
    "id" => $id, 
    "name" => $this->input->post('Name'), 
    "from_date" => $this->input->post('FromDate'), 
    "to_date" => $this->input->post('ToDate') 
    ); 

    $new_data = $this->my_modal->simple_post($post_data); 
    array_push($this->data['output'], $new_data); 
} 

var_dump($this->data['output']); 
+0

Danke .. Seine Arbeit .. –

0
$a = 1; 
$this->data['output'] = array(); 
foreach($codes as $values){ 
$id = $values['id']; 
$post_data = array ( 
"id" => $id, 
"name" => $this->input->post('Name'), 
"from_date" => $this->input->post('FromDate'), 
"to_date" => $this->input->post('ToDate') 
); 

$data['output2']= $this->my_modal->simple_post($post_data); 
if(count($this->data['output1']) > 1) { 
     $this->data['all']  = array_merge($this->data['output1'],$data['output2']); 
    }else { 
     $this->data['all']  = $data['output1']; 
    } 
} 
print_r($this->data['all']); 
0

Ihr Code ist vollkommen richtig, das einzige Problem ist, dass Sie den Zähler mit $a = 1 beginnen und tun $a++, die zur Folge haben werden 2. So existiert die output1 nicht. Jedoch, wenn Sie schreiben (beachten Sie die subtile Änderung):

$a = 1; 
foreach($codes as $values) { 
    $id = $values['id']; 
    $post_data = array ( 
     "id" => $id, 
     "name" => $this->input->post('Name'), 
     "from_date" => $this->input->post('FromDate'), 
     "to_date" => $this->input->post('ToDate') 
    ); 
    $this->data['output' . $a] = $this->my_modal->simple_post($post_data); // $a = 1 now 
    $a++; // $a becomes 2 here 
} 

$this->data['output'] = array_merge($this->data['output1'], $this->data['output2']); 

var_dump($this->data['output']); 
Verwandte Themen