I'm trying to convert this array I create in JS to then used in my controller to used in a foreach and used the data of the array. I'm using the framework codeigniter.
Here where I create the array in my JS file.
function get_array(){
var datos = []; // Array
$("#tbl_esctructura tbody > tr").each(function() {
var item = $(this).find('td:eq(1)').text();
var cantidad = $(this).find('td:eq(4)').text();
datos.push({
"item": item,
"cantidad": cantidad
});
});
datos = JSON.stringify(datos);
$.ajax({
data: {
'datos': datos
},
url: "<?php echo base_url() ?>Controller/data_from_array",
type: 'POST',
dataType : "json",
success: function(response) {
}
});
}
the data I send to the controller look like this.
[{"item":"1","cantidad":"2"},{"item":"2","cantidad":"4"}]
Now my controller PHP
public function data_from_array(){
$data = $this->input->post('datos', TRUE);
$items = explode(',', $data);
var_dump($items);
foreach ($items as $row) {
echo $row->item.'<br>';
}
}
and the var_dump($items) this is the result
array(2) { [0]=> string(12) "[{"item":"1"" [1]=> string(16) ""cantidad":"1"}]" }
}
And in this echo I get this error Message: Trying to get property 'item' of non-object
And I don't know what I'm doing wrong
datosstill stringified?