danush
danush

Reputation: 13

array to string conversion error in codeginiter in controller

$data = array('first_name' => $this->input->post('fname'),
'middle_name' => $this->input->post('mname'),

'last_name' => $this->input->post('lname'),
'email_name' => $this->input->post('email'),
'pwd_name' => $this->input->post('pwd'),

    'cno_name' => $this->input->post('cno'),
    'gender_name' => $this->input->post('gender'),
    'country_name' => $this->input->post('country'),
    'lang_name' => $this->input->post('lang')

);
            echo $data;

i want to echo or print $data, but it showing error Severity: Notice

Message: Array to string conversion

Upvotes: 1

Views: 782

Answers (3)

sg-
sg-

Reputation: 2176

Method 1

foreach($data as $key => $val)
    echo($key.' => '.(is_array($val)?implode(',', $val):$val).'<br>');

Method 2

var_dump($data);

Method 3

print_r($data);

Upvotes: 3

Gorakh Yadav
Gorakh Yadav

Reputation: 302

$data = array('first_name' => $this->input->post('fname'),
'middle_name' => $this->input->post('mname'),

'last_name' => $this->input->post('lname'),
'email_name' => $this->input->post('email'),
'pwd_name' => $this->input->post('pwd'),

    'cno_name' => $this->input->post('cno'),
    'gender_name' => $this->input->post('gender'),
    'country_name' => $this->input->post('country'),
    'lang_name' => $this->input->post('lang')

);

Now There is foreach loop in php. You have to traverse the array.

    foreach($data  as $key => $value)
    {
      echo $key." has the value". $value;
    }

If you simply want to add commas between values, consider using implode

    $string=implode(",",$data);
    echo $string;

Upvotes: 0

JYoThI
JYoThI

Reputation: 12085

1) your trying to echo the array variable ($data)

2) your not able to use echo for array variable

solution:

3) you can use var_dump($data) or print_r($data).

Upvotes: 0

Related Questions