0

I'm having some trouble sending multidimensional array [N][N] from javascript to PHP, I already tried alot of solutions that I'd found here but I don't know what I'm doing wrong.

My JQuery code (saving values from a HTML table):

$rowArray = {};

for ($i = 0; $i < $myRows.length; $i++) {
    $row = $($myRows[$i]).find('td');
    $rowArray[$i] = {};
    for ($j = 0; $j < $row.length - 1; $j++) {
        $rowArray[$i][$j] = $($row[$j]).html();
    }
}

Then:

$myJsonString = JSON.stringify($rowArray);

$.ajax({
    type: "POST",
    url:"../../download/myStore.php",
    data: { table: $myJsonString },
    success: function(data){
        console.log(data);
    }
});

PHP Side:

echo $_POST['table']; //just to see what is coming, but i want to work as array

//$data = json_decode($_POST['table'],true); -> when I echo $data, the output is an error Array to String conversion

Output:

{"0":{"0":"Cadastrado em","1":"Data da Venda","2":"Empreendimento","3":"Bloco/<br>Unidade","4":"Cliente/<br>Parceiro","5":"Valor","6":"Filial","7":"Gerente","8":"Corretor","9":"Veículo"},"1":{"0":"27/04/2016","1":"11/04/2016","2":"Villa Flora Hortolândia - Cond. 06","3":"Bloco/Torre: 13, Unidade: 283","4":"Lidiane Sasaki Santana","5":"20.664.259","6":"Campinas","7":"NATAL","8":"WILLIAM PILOTO","9":"Internet"},"2":{"0":"12/04/2016","1":"12/04/2016","2":"Lifespace Curitiba","3":"Bloco/Torre: 1, Unidade: 2404","4":"ANA","5":"351.000","6":"Curitiba","7":"André Barbosa de Lima","8":"Daniele","9":"Google"},"3":{"0":"12/04/2016","1":"12/04/2016","2":"ROSSI ATUAL MORADA","3":"Bloco/Torre: 3, Unidade: 407","4":"BERNADETE STARKE","5":"245.000","6":"Curitiba","7":"André Barbosa de Lima","8":"Dranka","9":"Google"},"4":{"0":"12/04/2016","1":"12/04/2016","2":"Lifespace Curitiba","3":"Bloco/Torre: 2, Unidade: 1105","4":"FLAVIA AMARAL","5":"272.500","6":"Curitiba","7":"André Barbosa de Lima","8":"Jesus","9":"Yahoo"},"5":{"0":"12/04/2016","1":"12/04/2016","2":"Lifespace Curitiba","3":"Bloco/Torre: 2, Unidade: 1809","4":"itajana","5":"270.500","6":"Curitiba","7":"André Barbosa de Lima","8":"Daniele","9":"Site Rossi"},"6":{"0":"27/04/2016","1":"14/04/2016","2":"Villa Flora Hortolândia - Cond. 05","3":"Bloco/Torre: 5, Unidade: 41","4":"Andre Fernando Da Silva Gradino","5":"184.303","6":"Campinas","7":"NATAL","8":"TIAGO","9":"Cadastro Manual"}} 

How can I access each index inside each array to get my values?
Whenever I try to use an index like $_POST['table'][0] the output is just '{'.
Sorry for my english and sorry for this noob question, but I'm stucked here for hours and already tried alot of solution found here and on google.

1

3 Answers 3

2

As you already have inside your code (but commented out) you can use json_decode($_POST['table'],true); When you us this function you get a array, with 7 entrys (in this example).

Like this:

$data = json_decode($_POST['table'], true);

The error occurs, because you echo the variable. Use var_dump($data) to see, that its really a array. $data[0], $data[1], ... holds then the data.

EDIT: Because you have nested array, you have to access the subarray again, like this:

$data = json_decode($_POST['table'], true);
$var = $data[0][0]; // holds "Cadastrado em"
$var2 = $data[0][1]; // holds "Data da Venda"

I'm not sure, if this is intended, that you have an array in an array.

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

1 Comment

Omg, it works, sorry for the noob question, I was stucked for hours. Really Thanks!
1

Use json_decode to transfrom the string literal into a PHP object.

 $json = '["apple","orange","banana","strawberry"]';
 $ar = json_decode($json);
 // access first element of $ar array
 echo $ar[0]; // apple

Comments

-1

Your commented line

$data = json_decode($_POST['table'],true);

is good, but you can't echo an array. This will fail:

echo $data;

This will work:

print_r($data);

Comments

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.