0

I send this array to php and when I want to show the results the following error appears:

"Illegal string offset 'nombre_color'"

This is the array result made with print_r() in the same php before the error:

Array(
    [0] => Array
        (
            [med_id] => 1
            [codigo] => 1111111111
            [medida] => XL
            [nombre_color] => Negro
            [color] => #000000
            [precio] => 1111
            [anterior] => 1000
            [stock] => 100
            [disponible] => 1
        )
)

And this is the part of code that gives me error:

foreach($medidas as $medida){
    $med_id = $medida['med_id'];
    $codigo = $medida['codigo'];
    $medida = $medida['medida'];
    $nombre_color = $medida['nombre_color'];
    $color = $medida['color'];
    $precio = $medida['precio'];
    $anterior = $medida['anterior'];
    $stock = $medida['stock'];
    $disponible = $medida['disponible'];
}

Thanks in advance for your help.

2 Answers 2

1

The name of foreach var ($medida) and index of one element was the same

Sign up to request clarification or add additional context in comments.

Comments

0

array_shift() : Shift an element off the beginning of array

extract() : Import variables into the current symbol table from an array

You could use array_shift wit extract

$medidas = array_shift($medidas);
extract($medidas);//This will make each index as variable i.e.
echo $med_id;

Working example :- https://3v4l.org/bmVX6

1 Comment

Extract is lame - forget about IDE and static analysis.

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.