Cedrix Cedrix
Cedrix Cedrix

Reputation: 113

warning illegal string offset php

Hi I am currently having an error that says illegal string offset and i already searh here I just know that you get that warning if you are treating a string as if it is an array but I am certain that I am using it as an array can anybody help me thanks

$data2 = array('EquipmentName' => $this->input->post('txt_equipb'),
                    'EquipmentType' => $this->input->post('txt_equiptype'),
                    'RequirementID' => $id2);

            foreach($data2 as $d) {
                $data2s = array('EquipmentName' => $d['EquipmentName'],
                    'EquipmentType' => $d['EquipmentType'],
                    'RequirementID' => $d['RequirementID']);
            }

Upvotes: 1

Views: 75

Answers (2)

Alex.Fu
Alex.Fu

Reputation: 69

You've misunderstand the meaning of foreach.(sigh)

Suggestions Provided:

just var_dump($d); before assignment of $data2s, and you'll know the result.

In the foreach, as you could see, each $d is only the value part of $data2, which means in every assignment of $data2s, there's no key as 'EquipmentName', only a simple string.

Upvotes: 1

Karlo Kokkak
Karlo Kokkak

Reputation: 3714

If you're just gonna loop once to set array values to a variable, you may just simply do it this way without using a foreach loop.

$data2 = array('EquipmentName' => $this->input->post('txt_equipb'),
                    'EquipmentType' => $this->input->post('txt_equiptype'),
                    'RequirementID' => $id2);
$data2s = $data2;

print_r( $data2s ); // check if has values

Upvotes: 0

Related Questions