简体   繁体   中英

How to convert a multidimensional associative array to JSON?

As you can see in the JSON file, this looks not so necessary. I get values from input-s and add it to the array and send it via AJAX. A simple array I know how to convert, but a multidimensional one is not. Can eat what that function? I tried to create an array with "keys", but there is a lot of trouble, I never reached the end , and I'm sure it's not right. Tell me what you can do.

i want this
{   
   "user1" : {
      first_name":"Michael",
      "last_name":"Podlevskykh",
       "phones" : {
           "phone_1":"5345",
            "phone_2":"345345",
            "phone_3":"123"
        }
    }
}

 //this is what i see
JSON
[
    {"first_name":"Michael"},
    {"last_name":"Podlevskykh"},
        [{"phone_1":"5345"},
            {"phone_2":"345345"},
            {"phone_3":"0991215078"}
        ]
]

PHP

//[["5345", "345345", "123"], "Michael", "Podlevskykh"]

$userInfo = (json_decode($_POST["phones"], true));

$namePhones = ["phone_1", "phone_2", "phone_3"];
$nameUser = ["first_name", "last_name"];
$jsonPhones = $userInfo;
$nameLName = $userInfo;

$jsonPhones = array_splice($jsonPhones, 0, 1);
$nameLName = array_splice($nameLName, -2);

foreach ($jsonPhones[0] as $key => $value) {
    $phones[] = array($namePhones[$key] => $jsonPhones[0][$key]);
}

foreach ($nameLName as $key => $value) {
    $usersName[] = array($nameUser[$key] => $nameLName[$key]);
}

array_push($usersName, $phones);

echo "<pre>";
echo json_encode($usersName);
//[ 
 // {"first_name":"Michael"},{"last_name":"Podlevskykh"},
//  [{"phone_1":"5345"},{"phone_2":"345345"},{"phone_3":"123"}]
//]

I don't get all the complications, I would do something like this if I'm sure the $input format is the same:

<?php
$input = '[["5345", "345345", "123"], "Michael", "Podlevskykh"]';
$input = json_decode($input, true);

$output = [
    'user1' => [
        'first_name' => $input[1],
        'last_name' => $input[2],
        'phones' => [
            'phone_1' => $input[0][0],
            'phone_2' => $input[0][1],
            'phone_3' => $input[0][2]
        ]
    ]
];

echo '<pre>';
echo json_encode($output);

If you want an object as output, you need to create an object:

$userInfo = (json_decode($_POST["phones"], true));
$namePhones = ["phone_1", "phone_2", "phone_3"];
$nameUser = ["first_name", "last_name"];
$jsonPhones = $userInfo;
$nameLName = $userInfo;

$jsonPhones = array_splice($jsonPhones, 0, 1);
$nameLName = array_splice($nameLName, -2);

$user = new stdClass();

foreach ($nameLName as $key => $value) {
    $user->{$nameUser[$key]} = $nameLName[$key];
}

$phones = new stdClass();

foreach ($jsonPhones[0] as $key => $value) {
    $phones->{$namePhones[$key]} = $jsonPhones[0][$key];
}

$user->phones = $phones;

$users = new stdClass();
$users->user1 = $user;

echo json_encode($users);

Output:

{"user1": {
   "first_name":"Michael",
   "last_name":"Podlevskykh",
   "phones":{
        "phone_1":"5345",
        "phone_2":"345345",
        "phone_3":"123"
        }
    }
}

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