0

adding element in array using array_push( ) in php

for ($i = 0; $i < count($d); $i++){
    $ins_data[$i]=$this->input->post('question_type_rt_'.$i.'');
    $a=array();
    array_push($a,$ins_data[$i]);
    print_r($a);
    echo"<br>";
}

where it gives output

Array ( [0] => 1 )
Array ( [0] => 2 )
Array ( [0] => 1 )
Array ( [0] => 2 )
Array ( [0] => 1 )

but i want all element in same array

and create string from that array

like 12121

1 Answer 1

2

Your array needs to be initialised outside of the for loop to prevent overwriting the reference to it each time you run through the for loop:

$a=array();
for ($i = 0; $i < count($d); $i++){
  $ins_data[$i]=$this->input->post('question_type_rt_'.$i.'');
  array_push($a,$ins_data[$i]);
}
print_r($a);
Sign up to request clarification or add additional context in comments.

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.