1
votes

I am trying to push array and then implode them

$this->input->post('sarana')

this is array for my checkbox

<input type="checkbox" class="flat" name="sarana[]" id="sarana" value="listrik" />penerangan listrik 
<input type="checkbox" class="flat" name="sarana[]" id="sarana" value="listrik" />penerangan listrik 

and then I want push input type post mobil /motor /tv

<input type="text" name="mobil" placeholder="mobil" class="form-control input-sm">

controller

$tv=$this->input->post('tv');
$motor=$this->input->post('motor');
$mobil=$this->input->post('mobil');
$sarana=$this->input->post('sarana');
array_push($sarana, 'tv=$tv','motor=$motor','mobil=$mobil');
        $data = array(
            'idakun' =>29,
            'kondisirumah' =>$this->input->post('fisikrumah'), 
            'statusrumah' =>$this->input->post('pemilikanrumah'),
            'sarana' =>implode(",", $sarana),
            'ekonomi' =>$this->input->post('ekonomi'),
            'hpkeluarga' =>implode(",",$this->input->post('hpkeluarga')),

            );

but it is giving error as

array_push() expects parameter 1 to be array, null given

3

3 Answers

0
votes

You can do it using array_merge() function.

Just update:

$sarana=$this->input->post('sarana');
array_push($sarana, 'tv=$tv','motor=$motor','mobil=$mobil');

with:

$sarana=$this->input->post('sarana');
$sarana= array_merge($sarana, array('tv'=>$tv), array('motor'=>$motor), array('mobil'=>$mobil));
0
votes

I think you need to check for null in $sarana because if you did not check at least one from checkbox list with the name sarana[] then $this->input->post('sarana') will give you null value, so-

// this will make $sarana as array even if you did not check any of sarana checkbox list
$sarana=($this->input->post('sarana'))?$this->input->post('sarana'):[];
0
votes
$var= ""; 
      for ($x = 1; $x <= $request->input('cant'); $x++) {
        $array_numerico_indexado = array();
        array_push($array_numerico_indexado, $request->input('cama'.$x));
         foreach($array_numerico_indexado as $numero)
         {
          $var.= $numero.",<br>";
         }
      }
      echo $var;