简体   繁体   中英

Accessing multidimensional array php

I have an array of data sent by jquery ajax to a php service:

  requestAjax = jQuery.ajax({
            type: "POST",
            url: "ajax/ajax.salva-valutazione.php",
            data: {formdata:formdata, arrAccessori: arrAccessori},
        //  data:formdata,
            dataType: "json",
            success: function(risposta){
               alert(".."+risposta);
             }
    });

In the php response page I use, if I do a print_r of all data:

 error_log(print_r($arrValutazione, TRUE) );

I get in error log:

  [formdata] => nome=John&cognome=Doe&indirizzo=My+address&citta=London&user_name=fedepupo&clienteID=1

While if I do

 error_log(print_r($arrValutazione['formdata'], TRUE) );

I obtain

nome=John&cognome=Doe&indirizzo=My+address&citta=London&user_name=fedepupo&clienteID=1

In error log.

My problem is how direcly accessing nome, cognome (...) values because if I try doing

 error_log(print_r($arrValutazione['formdata']['cognome'], TRUE) );

I get 'n', and also with

error_log(print_r($arrValutazione['formdata'][0]['cognome'], TRUE) );

I get the same value 'n'.

Any suggestions?

In error log.

My problem is how direcly accessing nome, cognome (...) values because if I try doing

error_log(print_r($arrValutazione['formdata']['cognome'], TRUE) );

I get 'n', and also with

error_log(print_r($arrValutazione['formdata'][0]['cognome'], TRUE) );

I get the same value 'n'.

 $arrValutazione['formdata'] = nome=John&cognome=Doe&indirizzo=My+address&citta=London&user_name=fedepupo&clienteID=1 

Reason is you are accessing string not array, so you get always n

Here is demo

$ php -r '$string="abcdefgh"; 
  echo $string[0].PHP_EOL;
  echo $string[2].PHP_EOL; 
  echo $string["unknown_index"].PHP_EOL;'
a
c
PHP Warning:  Illegal string offset 'unknown_index' in Command line code on line 4
a

Use below code to get value of each variable

parse_str($arrValutazione['formdata'],$output);
print_r($output);
$nome = $output['nome'];
$cogname = $output['cogname'];

Similarily you can fetch any variable detail.

Please try the below code,

parse_str($arrValutazione['formdata'],$formdata);
error_log(print_r($formdata['cognome'], TRUE));

Here is the reference link, http://php.net/manual/en/function.parse-str.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM