简体   繁体   English

访问多维数组PHP

[英]Accessing multidimensional array php

I have an array of data sent by jquery ajax to a php service: 我有一个由jQuery ajax发送到php服务的数据数组:

  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: 如果我对所有数据执行print_r,则在php响应页面中使用:

 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 我的问题是如何直接访问nome,cognome(...)值,因为如果我尝试这样做

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

I get 'n', and also with 我得到“ n”,也得到

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

I get the same value 'n'. 我得到相同的值“ n”。

Any suggestions? 有什么建议么?

In error log. 在错误日志中。

My problem is how direcly accessing nome, cognome (...) values because if I try doing 我的问题是如何直接访问nome,cognome(...)值,因为如果我尝试这样做

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

I get 'n', and also with 我得到“ n”,也得到

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

I get the same value 'n'. 我得到相同的值“ 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 原因是您访问的是字符串而不是数组,所以您总是得到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 这是参考链接, http://php.net/manual/en/function.parse-str.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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