0

I'm sending an array from javascript to PHP file. When doing echo $newArray inside the .php file, it shows me the array correctly:

array(4) { ["titulo"]=> string(28) "Curso de canvas y Javascript ["cantidad"]=> string(1) "1" ["valorItem"]=> string(2) "10"["idProducto"]=> string(3) "403" }
array(4) { ["titulo"]=> string(15) "Curso de jQuery" ["cantidad"]=> string(1) "1" ["valorItem"]=> string(2) "10"["idProducto"]=> string(3) "402" }

How do I access some element of the array? I have tried with this:

$newArray = $_POST["array"];

$title = json_decode($newArray[0]["titulo"]);

echo $title;

But this appears:

Warning: Illegal string offset 'titulo' in C:\xampp\htdocs\frontend\controladores\carrito.controlador.php on line 78

I tried everything that says in other questions of this type in the stack, but it has not worked

  • 3
    Where does that `[0]` come from? And why `json_decode()`? – Siguza Dec 08 '18 at 17:28
  • 1
    Your first output would not be the result of `echo` but of `var_dump`. Or are you really saying that you get *that* with `echo`? – trincot Dec 08 '18 at 17:29
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Alon Eitan Dec 08 '18 at 17:30
  • @trincot I get that answer by doing echo. if I do var_dump I get the same answer but in string format. –  Dec 08 '18 at 17:44
  • @Siguza It's what they recommended in other answers, that's why I tried that –  Dec 08 '18 at 17:46

1 Answers1

0

where is your javascript? You can do something like

$title = $_POST['titulo'] 

similarly do other things

rakesh shrestha
  • 1,335
  • 19
  • 37