我已经通过ajax jQuery代码传递了json字符串,然后将其转换为多维关联数组。现在,我在打印此多维关联数组时遇到问题。
// Retrieve the string, which was sent via the POST parameter "user"
$user = $_POST['user'];
// Decode the JSON string and convert it into a PHP associative array.
$decoded = json_decode($user,true,10);
// var_dump the array so that we can view it's structure.
var_dump($decoded);
var userStr = JSON.stringify(connections);
$.ajax({
url: base_url+"ajax/add_google_user",
type: 'post',
data: {user: userStr},
success: function(response){
appendPre(response);
}
});
我正在通过var_dump($ decoded)进行打印; 我需要用回声打印怎么办?
array(5) {
[0] =>
array(5) {
["resourceName"] =>
string(27)"people/abc1"
["etag"] =>
string(45)"abc1"
["names"] =>
array(1) {
[0] =>
array(5) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(16)"618d80f98fe31c72"
}
}
["displayName"] =>
string(12)"Raghav verma"
["familyName"] =>
string(5)"verma"
["givenName"] =>
string(6)"Raghav"
["displayNameLastFirst"] =>
string(13)"verma, Raghav"
}
}
["emailAddresses"] =>
array(1) {
[0] =>
array(2) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(16)"618d80f98fe31c72"
}
}
["value"] =>
string(27)"raghav.verma12345@gmail.com"
}
}
["phoneNumbers"] =>
array(1) {
[0] =>
array(3) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(16)"618d80f98fe31c72"
}
}
["value"] =>
string(10)"9854251378"
["canonicalForm"] =>
string(13)"+919854251378"
}
}
}
[1] =>
array(5) {
["resourceName"] =>
string(27)"people/abc1"
["etag"] =>
string(45)"abc1"
["names"] =>
array(1) {
[0] =>
array(5) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(16)"69849e8b89f8c048"
}
}
["displayName"] =>
string(13)"Dinesh Chopra"
["familyName"] =>
string(6)"Chopra"
["givenName"] =>
string(6)"Dinesh"
["displayNameLastFirst"] =>
string(14)"Chopra, Dinesh"
}
}
["emailAddresses"] =>
array(1) {
[0] =>
array(2) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(16)"69849e8b89f8c048"
}
}
["value"] =>
string(28)"dinesh.chopra12345@gmail.com"
}
}
["phoneNumbers"] =>
array(1) {
[0] =>
array(3) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(16)"69849e8b89f8c048"
}
}
["value"] =>
string(10)"9562145678"
["canonicalForm"] =>
string(13)"+919562145678"
}
}
}
[2] =>
array(5) {
["resourceName"] =>
string(25)"people/abc1"
["etag"] =>
string(45)"abc1"
["names"] =>
array(1) {
[0] =>
array(5) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(14)"43f6888c09d0a8"
}
}
["displayName"] =>
string(12)"Rahul Sharma"
["familyName"] =>
string(6)"Sharma"
["givenName"] =>
string(5)"Rahul"
["displayNameLastFirst"] =>
string(13)"Sharma, Rahul"
}
}
["emailAddresses"] =>
array(1) {
[0] =>
array(2) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(14)"43f6888c09d0a8"
}
}
["value"] =>
string(28)"rahul.sharma123456@gmail.com"
}
}
["phoneNumbers"] =>
array(1) {
[0] =>
array(3) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(14)"43f6888c09d0a8"
}
}
["value"] =>
string(11)"97854 63214"
["canonicalForm"] =>
string(13)"+919785463214"
}
}
}
[3] =>
array(5) {
["resourceName"] =>
string(27)"people/abc1"
["etag"] =>
string(45)"abc1"
["names"] =>
array(1) {
[0] =>
array(5) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(16)"1bdbb17e08cff0b6"
}
}
["displayName"] =>
string(11)"Tarun Mehta"
["familyName"] =>
string(5)"Mehta"
["givenName"] =>
string(5)"Tarun"
["displayNameLastFirst"] =>
string(12)"Mehta, Tarun"
}
}
["emailAddresses"] =>
array(1) {
[0] =>
array(2) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(16)"1bdbb17e08cff0b6"
}
}
["value"] =>
string(27)"tarun.mehta123456@gmail.com"
}
}
["phoneNumbers"] =>
array(1) {
[0] =>
array(3) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(16)"1bdbb17e08cff0b6"
}
}
["value"] =>
string(11)"92635 47815"
["canonicalForm"] =>
string(13)"+919263547815"
}
}
}
[4] =>
array(4) {
["resourceName"] =>
string(26)"people/abc1"
["etag"] =>
string(45)"abc1"
["names"] =>
array(1) {
[0] =>
array(4) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(15)"8e3b6280a6e5da2"
}
}
["displayName"] =>
string(26)"aniltulipacademy@gmail.com"
["givenName"] =>
string(26)"aniltulipacademy@gmail.com"
["displayNameLastFirst"] =>
string(26)"aniltulipacademy@gmail.com"
}
}
["emailAddresses"] =>
array(1) {
[0] =>
array(2) {
["metadata"] =>
array(2) {
["primary"] =>
bool(true)
["source"] =>
array(2) {
["type"] =>
string(7)"CONTACT"
["id"] =>
string(15)"8e3b6280a6e5da2"
}
}
["value"] =>
string(26)"aniltulipacademy@gmail.com"
}
}
}
}
答案 0 :(得分:0)
由于需要打印多维数组,因此可以使用for
之类的任意迭代语句,使用echo
挖掘和打印数组的所有级别。
答案 1 :(得分:0)
如果将true作为第二个参数传递,则可以使用var_export,可以将内容放入变量。
$a = var_export([ 1 => 2 ], true);
echo $a;
如果出于某些晦涩的原因要使用var_dumps,则可以使用缓冲区来捕获输出:
ob_start();
var_dump([ "a" => "1" ]);
$a = ob_get_contents();
ob_end_clean();
echo $a;