How to convert multidimensional array into json object [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
i have an issue in array to json conversion, I have an array and i want to convert this array into json objects, desired output given below so any one please help me.
Php array
Array
(
[0] => Array
(
[application_id] => 132
[application_status] => SUBMITTED
[reference_number] =>
[salutation] =>
[first_name] =>
[middle_name] =>
[last_name] =>
[mother_name] =>
)
[1] => Array
(
[application_id] => 148
[application_status] => SUBMITTED
[reference_number] =>
[salutation] =>
[first_name] =>
[middle_name] =>
[last_name] =>
[mother_name] =>
)
[2] => Array
(
[application_id] => 154
[application_status] => SUBMITTED
[reference_number] =>
[salutation] =>
[first_name] =>
[middle_name] =>
[last_name] =>
[mother_name] =>
)
[3] => Array
(
[application_id] => 182
[application_status] => SUBMITTED
[reference_number] =>
[salutation] =>
[first_name] =>
[middle_name] =>
[last_name] =>
[mother_name] =>
)
[4] => Array
(
[application_id] => 186
[application_status] => SUBMITTED
[reference_number] =>
[salutation] =>
[first_name] =>
[middle_name] =>
[last_name] =>
[mother_name] =>
)
)
Convert above array to json object like this:
[
{
"application_id": "1",
"application_status": "0",
"reference_number": "/index",
"salutation": "index",
"first_name": "Index",
"middle_name": "Home",
"last_name": "1",
},
{
"application_id": "1",
"application_status": "0",
"reference_number": "/index",
"salutation": "index",
"first_name": "Index",
"middle_name": "Home",
"last_name": "1",
},
{
"application_id": "1",
"application_status": "0",
"reference_number": "/index",
"salutation": "index",
"first_name": "Index",
"middle_name": "Home",
"last_name": "1",
},
{
"application_id": "1",
"application_status": "0",
"reference_number": "/index",
"salutation": "index",
"first_name": "Index",
"middle_name": "Home",
"last_name": "1",
},
{
"application_id": "1",
"application_status": "0",
"reference_number": "/index",
"salutation": "index",
"first_name": "Index",
"middle_name": "Home",
"last_name": "1",
},
]

Your desired output suggests it is an array of objects. Just loop over them and encode each subarray to json string, and decode it again to obtain an object:
foreach($array as $k =>$a){
$array[$k] = json_decode(json_encode($a));
}
If however you mean you want an array of json strings, omit the json_decode:
foreach($array as $k =>$a){
$array[$k] = json_encode($a);
}

You could just use json_encode for this problem? This will convert the passed argument to a JSON object.

Just use json_encode()
<?php
$array = Array
(
"0" => Array
(
"application_id" => "132",
"application_status" => "SUBMITTED",
"reference_number" => "",
"salutation" => "",
"first_name" => "",
"middle_name" => "",
"last_name" => "",
"mother_name" => ""
),
"1" => Array
(
"application_id" => "148",
"application_status" => "SUBMITTED",
"reference_number" => "",
"salutation" => "",
"first_name" => "",
"middle_name" => "",
"last_name" => "",
"mother_name" => ""
),
"2" => Array
(
"application_id" => "154",
"application_status" => "SUBMITTED",
"reference_number" => "",
"salutation" => "",
"first_name" => "",
"middle_name" => "",
"last_name" => "",
"mother_name" => ""
),
"3" => Array
(
"application_id" => "182",
"application_status" => "SUBMITTED",
"reference_number" => "",
"salutation" => "",
"first_name" => "",
"middle_name" => "",
"last_name" => "",
"mother_name" => ""
),
"4" => Array
(
"application_id" => "186",
"application_status" => "SUBMITTED",
"reference_number" => "",
"salutation" => "",
"first_name" => "",
"middle_name" => "",
"last_name" => "",
"mother_name" => ""
)
);
$json = json_encode($array);
print_r($json);
?>

Related

PHP Array to multidimensional json data

I have the following php code that produces a json data set
$json_data = array(
"code"=>"200",
"name"=>"My Name",
"serial_number"=>"serial"
);
$result = json_encode($json_data);
The dataset is only one level. As I am creating this data set with a php loop. I want to be able to have multiple people but the code element be outside the users. So basically I want the json data to look like this:
{
"code": "404",
"people": [
{
"name": "Person 1",
"serial_number": "xyz"
},
{
"name": "Person Two",
"serial_number": "123"
}
]
}
Basically you want to group array of objects by property "code". There are many questions about this. Here's one of the possible methods using array_reduce.
$json_data = [
[
"code" => "200",
"name" => "My Name1",
"serial_number" => "serial1",
], [
"code" => "200",
"name" => "My Name2",
"serial_number" => "serial2",
], [
"code" => "400",
"name" => "My Name3",
"serial_number" => "serial3",
],
];
$result = array_values(array_reduce($json_data, function ($agg, $item) {
if (!isset($agg[$item['code']])) {
$agg[$item['code']] = [
"code" => $item['code'],
"people" => [],
];
}
$agg[$item['code']]['people'][] = [
"name" => $item["name"],
"serial_number" => $item["serial_number"],
];
return $agg;
}, []));
print_r($result);
Output:
Array
(
[0] => Array
(
[code] => 200
[people] => Array
(
[0] => Array
(
[name] => My Name1
[serial_number] => serial1
)
[1] => Array
(
[name] => My Name2
[serial_number] => serial2
)
)
)
[1] => Array
(
[code] => 400
[people] => Array
(
[0] => Array
(
[name] => My Name3
[serial_number] => serial3
)
)
)
)

Search associative array for specific value based on set variable

I'm retrieving some JSON that I am converting to an associative array. The issue that I am having is I am trying to get the email value from the user's who id matches the value that I have already set as a variable.
Here is what the array looks like
Array
(
[object] => list
[data] => Array
(
[0] => Array
(
[object] => pro
[id] => pro_77c9c6a85d814e059a6a2690989bae29
[first_name] => Jane
[last_name] => Doe
[full_name] => Jane Doe
[initials] => JD
[email] => admin#testorg.com
[mobile_number] => 9998761234
[messaging_uuid] => 4547c231c3e7d0ff1796f47b88f166d5
[color_hex] => EF9159
[avatar_url] => /assets/add_image.png
[avatar_thumb_url] =>
[has_avatar] =>
[organization_name] => testorg
[is_admin] => 1
[permissions] => Array
(
[show_company_setup] => 1
[can_see_home_data] => 1
[show_reporting] => 1
)
[is_super_pro] =>
[is_archived] =>
[impersonated] =>
)
[1] => Array
(
[object] => pro
[id] => pro_0fcb8e8610e54c518078db77ced7530e
[first_name] => Robert
[last_name] => Jordan
[full_name] => Robert Jordan
[initials] => RJ
[email] => rj#testorg.com
[mobile_number] => 4547457742
[messaging_uuid] => 0fcb8e8610e54c518078db77ced7530e
[color_hex] => EF9159
[avatar_url] => /assets/add_image.png
[avatar_thumb_url] =>
[has_avatar] =>
[organization_name] => testorg
[is_admin] => 1
[permissions] => Array
(
[show_company_setup] => 1
[can_see_home_data] => 1
[show_reporting] => 1
)
[is_super_pro] =>
[is_archived] =>
[impersonated] =>
)
)
[url] => /pros
)
Im basically trying to match the value that I have set in my pro_id variable
pro_0fcb8e8610e54c518078db77ced7530e
To the 'ID' Key in the array above and get the 'email' value associated to that same array and store for use later in my code.
Here is what I have so far, but no joy
foreach ($proObject as $key => $value) {
if ($key['id'] == $pro_id)
$techmail = $key['email'];
}
From my understanding of your question, and if you want to get only one element, you should be able to use array_search with array_column to get the index of the searched element. Using that you can then access the element and its corresponding email value. If you might expect more than one element I would use array_filter.
One Element:
In short:
$i = array_search($pro_id, array_column($proObject, 'id'));
$element = ($i !== false ? $proObject[$i] : null);
print_r($element["email"]);
Full code:
<?php
$proObject = array(
array(
"object" => "pro",
"id" => "pro_77c9c6a85d814e059a6a2690989bae29",
"first_name" => "Jane",
"last_name" => "Doe",
"full_name" => "Jane Doe",
"initials" => "JD",
"email" => "admin#testorg.com",
"mobile_number" => "9998761234",
"messaging_uuid" => "4547c231c3e7d0ff1796f47b88f166d5",
"color_hex" => "EF9159",
"avatar_url" => "/assets/add_image.png",
"avatar_thumb_url" => "",
"has_avatar" => "",
"organization_name" => "testorg",
"is_admin" => "1",
"permissions" => array(
"show_company_setup" => "1",
"can_see_home_data" => "1",
"show_reporting" => "1",
) ,
"is_super_pro" => "",
"is_archived" => "",
"impersonated" => "",
) ,
array(
"object" => "pro",
"id" => "pro_0fcb8e8610e54c518078db77ced7530e",
"first_name" => "Robert",
"last_name" => "Jordan",
"full_name" => "Robert Jordan",
"initials" => "RJ",
"email" => "rj#testorg.com",
"mobile_number" => "4547457742",
"messaging_uuid" => "0fcb8e8610e54c518078db77ced7530e",
"color_hex" => "EF9159",
"avatar_url" => "/assets/add_image.png",
"avatar_thumb_url" => "",
"has_avatar" => "",
"organization_name" => "testorg",
"is_admin" => "1",
"permissions" => array(
"show_company_setup" => "1",
"can_see_home_data" => "1",
"show_reporting" => "1",
) ,
"is_super_pro" => "",
"is_archived" => "",
"impersonated" => "",
)
);
$pro_id = "pro_0fcb8e8610e54c518078db77ced7530e";
$i = array_search($pro_id, array_column($proObject, 'id'));
$element = ($i !== false ? $proObject[$i] : null);
print_r($element["email"]);
?>
Multiple Elements:
In short:
class idEqualsFilter {
private $id;
public function __construct($id) {
$this->id = $id;
}
function __invoke($i) {
return $this->id === $i["id"];
}
};
$elements = array_filter($proObject, new idEqualsFilter($pro_id));
print_r(array_column($elements, "email"));
Full code:
<?php
$proObject = array(
array(
"object" => "pro",
"id" => "pro_77c9c6a85d814e059a6a2690989bae29",
"first_name" => "Jane",
"last_name" => "Doe",
"full_name" => "Jane Doe",
"initials" => "JD",
"email" => "admin#testorg.com",
"mobile_number" => "9998761234",
"messaging_uuid" => "4547c231c3e7d0ff1796f47b88f166d5",
"color_hex" => "EF9159",
"avatar_url" => "/assets/add_image.png",
"avatar_thumb_url" => "",
"has_avatar" => "",
"organization_name" => "testorg",
"is_admin" => "1",
"permissions" => array
(
"show_company_setup" => "1",
"can_see_home_data" => "1",
"show_reporting" => "1",
),
"is_super_pro" => "",
"is_archived" => "",
"impersonated" => "",
),
array(
"object" => "pro",
"id" => "pro_0fcb8e8610e54c518078db77ced7530e",
"first_name" => "Robert",
"last_name" => "Jordan",
"full_name" => "Robert Jordan",
"initials" => "RJ",
"email" => "rj#testorg.com",
"mobile_number" => "4547457742",
"messaging_uuid" => "0fcb8e8610e54c518078db77ced7530e",
"color_hex" => "EF9159",
"avatar_url" => "/assets/add_image.png",
"avatar_thumb_url" => "",
"has_avatar" => "",
"organization_name" => "testorg",
"is_admin" => "1",
"permissions" => array
(
"show_company_setup" => "1",
"can_see_home_data" => "1",
"show_reporting" => "1",
),
"is_super_pro" => "",
"is_archived" => "",
"impersonated" => "",
)
);
$pro_id = "pro_0fcb8e8610e54c518078db77ced7530e";
class idEqualsFilter {
private $id;
public function __construct($id) {
$this->id = $id;
}
function __invoke($i) {
return $this->id === $i["id"];
}
};
$elements = array_filter($proObject, new idEqualsFilter($pro_id));
print_r(array_column($elements, "email"));
?>

how to remove the duplicates within an array of arrays based on the array key in php?

I have this multi dimensional array. At first glance it's duplicated because the id is the same for all three. but if you check the "notes" key, it's not. but I want to remove the duplicates from this array based form the "id" . how to do that? because array_unique does not work
[
{
"id": 69,
"text": "hello text",
"desc": "hello desc",
"state": false,
"type": false,
"notes": "Created",
"type_id": 25185,
"state_id": 0
},
{
"id": 69,
"text": "hello text",
"desc": "hello desc",
"state": false,
"type": false,
"notes": "Accepted",
"type_id": 25185,
"state_id": 0
},
{
"id": 69,
"text": "hello text",
"desc": "hello desc",
"state": false,
"type": false,
"notes": "In Progress",
"type_id": 25185,
"state_id": 0
}
];
Or shorter:
$result = array_values(array_column($yourArray, null, 'id')))
$result:
Array
(
[0] => stdClass Object
(
[id] => 69
[text] => hello text
[desc] => hello desc
[state] =>
[type] =>
[notes] => In Progress
[type_id] => 25185
[state_id] => 0
)
)
This will delete all but the last object with a matching id:
https://paiza.io/projects/mZ-e3zzmyFWq6XY-qjKLjg
<?php
$arr = [
(object)array(
"id" => 69,
"text" => "hello text",
"desc" => "hello desc",
"state" => false,
"type" => false,
"notes" => "Created",
"type_id" => 25185,
"state_id"=> 0
),
(object)array(
"id" => 69,
"text" => "hello text",
"desc" => "hello desc",
"state" => false,
"type" => false,
"notes" => "Accepted",
"type_id" => 25185,
"state_id" => 0
),
(object)array(
"id" => 69,
"text" => "hello text",
"desc" => "hello desc",
"state" => false,
"type" => false,
"notes" => "In Progress",
"type_id" => 25185,
"state_id" => 0
)
];
foreach($arr as $key1 => $obj1){
foreach($arr as $key2 => $obj2){
if($obj1->id === $obj2->id && $key1 !== $key2){
unset($arr[$key1]);
}
}
}
print_r($arr);
?>
Result:
Array
(
[2] => stdClass Object
(
[id] => 69
[text] => hello text
[desc] => hello desc
[state] =>
[type] =>
[notes] => In Progress
[type_id] => 25185
[state_id] => 0
)
)

How to convert JSON string in Array. Array within Array stuck me. How to handle with it?

I have a JSON String. I want it to be filled in Array. Here is Array which i receive.
My JSON Response is:
{
"model": "SyncData",
"unique_id": "c12fb356f90d032b",
"key": "sdjvnsdivbsnd",
"sync_data": {
"array_a": [{
"a_fav": "true",
"a_id": 1
}, {
"a_fav": "false",
"a_id": 2
}],
"array_b": [{
"b_fav": "false",
"b_id": 8
}],
"c_array": [{
"c_fav": "false",
"c_id": 15996
}],
"patient_list_array": [{
"unique_id": "sdvsdvsdvdsdv",
"p_status": "false",
"p_id": 1454943805215,
"p_note": "2",
"p_code": "8",
"p_timestamp": 1454943805216,
"p_name": "ABC XYZ",
"p_status": 1,
"p_room_no": "5"
}],
"array_d": [{
"d_assigned_id": "30",
"d_fav": "true"
}]
}
}
I want to store all this data in Array and from that in DataBase.
If you want this json string in an array than you can use
json_decode($string,true);
Note that, second param of json_decode will return the array if you need result in object form than remove the second param "true".
Your Code:
$string = '{
"model": "SyncData",
"unique_id": "c12fb356f90d032b",
"key": "sdjvnsdivbsnd",
"sync_data": {
"array_a": [{
"a_fav": "true",
"a_id": 1
}, {
"a_fav": "false",
"a_id": 2
}],
"array_b": [{
"b_fav": "false",
"b_id": 8
}],
"c_array": [{
"c_fav": "false",
"c_id": 15996
}],
"patient_list_array": [{
"unique_id": "sdvsdvsdvdsdv",
"p_status": "false",
"p_id": 1454943805215,
"p_note": "2",
"p_code": "8",
"p_timestamp": 1454943805216,
"p_name": "ABC XYZ",
"p_status": 1,
"p_room_no": "5"
}],
"array_d": [{
"d_assigned_id": "30",
"d_fav": "true"
}]
}
}';
$array = json_decode($string,true);
echo "<pre>";
print_r($array);
Result:
Array
(
[model] => SyncData
[unique_id] => c12fb356f90d032b
[key] => sdjvnsdivbsnd
[sync_data] => Array
(
[array_a] => Array
(
[0] => Array
(
[a_fav] => true
[a_id] => 1
)
[1] => Array
(
[a_fav] => false
[a_id] => 2
)
)
[array_b] => Array
(
[0] => Array
(
[b_fav] => false
[b_id] => 8
)
)
[c_array] => Array
(
[0] => Array
(
[c_fav] => false
[c_id] => 15996
)
)
[patient_list_array] => Array
(
[0] => Array
(
[unique_id] => sdvsdvsdvdsdv
[p_status] => 1
[p_id] => 1454943805215
[p_note] => 2
[p_code] => 8
[p_timestamp] => 1454943805216
[p_name] => ABC XYZ
[p_room_no] => 5
)
)
[array_d] => Array
(
[0] => Array
(
[d_assigned_id] => 30
[d_fav] => true
)
)
)
)
Simple code to use to visualise your JSON String
<?php
$string = '{
"model": "SyncData",
"unique_id": "c12fb356f90d032b",
"key": "sdjvnsdivbsnd",
"sync_data": {
"array_a": [{
"a_fav": "true",
"a_id": 1
}, {
"a_fav": "false",
"a_id": 2
}],
"array_b": [{
"b_fav": "false",
"b_id": 8
}],
"c_array": [{
"c_fav": "false",
"c_id": 15996
}],
"patient_list_array": [{
"unique_id": "sdvsdvsdvdsdv",
"p_status": "false",
"p_id": 1454943805215,
"p_note": "2",
"p_code": "8",
"p_timestamp": 1454943805216,
"p_name": "ABC XYZ",
"p_status": 1,
"p_room_no": "5"
}],
"array_d": [{
"d_assigned_id": "30",
"d_fav": "true"
}]
}
}';
$obj = json_decode($string);
print_r($obj);
Which will output :
stdClass Object
(
[model] => SyncData
[unique_id] => c12fb356f90d032b
[key] => sdjvnsdivbsnd
[sync_data] => stdClass Object
(
[array_a] => Array
(
[0] => stdClass Object
(
[a_fav] => true
[a_id] => 1
)
[1] => stdClass Object
(
[a_fav] => false
[a_id] => 2
)
)
[array_b] => Array
(
[0] => stdClass Object
(
[b_fav] => false
[b_id] => 8
)
)
[c_array] => Array
(
[0] => stdClass Object
(
[c_fav] => false
[c_id] => 15996
)
)
[patient_list_array] => Array
(
[0] => stdClass Object
(
[unique_id] => sdvsdvsdvdsdv
[p_status] => 1
[p_id] => 1454943805215
[p_note] => 2
[p_code] => 8
[p_timestamp] => 1454943805216
[p_name] => ABC XYZ
[p_room_no] => 5
)
)
[array_d] => Array
(
[0] => stdClass Object
(
[d_assigned_id] => 30
[d_fav] => true
)
)
)
)

mongodb pull element from multidimensional array

Hi i have the following array and i want to pull the array with productId 100343
Array(
[_id] => MongoId Object
(
[$id] => 5388a02c8ead0ebf048b4569
)
[cartId] => 14ce496ac194d5d8ee8d0cd11bd3ef5a
[products] => Array
(
[100343] => Array
(
[productId] => 100343
[quantity] => 13
[name] => a
)
[100344] => Array
(
[productId] => 100344
[quantity] => 3
[name] => ab
)
[100345] => Array
(
[productId] => 100345
[quantity] => 1
[name] => abc
)
)
)
I've tried like this but it doesn't work
$c->update(
$aQuery,
array(
'$pull' => array('products'=>array('productId'=>'100343'))
)
);
That is not an array. Arrays in MongoDB are different in concept to what PHP calls an array, so the structure you have there is the same as this JSON representation:
{
"products": {
"100343": {
"productId": 100343,
"quantity": 13,
"name": "a"
},
"100344": {
"productId": 100344,
"quantity": 3,
"name": "ab"
},
"100345": {
"productId": 100345,
"quantity": 1,
"name": "abc"
}
}
}
That sort of structure is just a use of sub-documents and is not an array in the sense as used by MongoDB. To remove the entry as you want with this structure you use the $unset operator.
$c->update(
$aQuery,
array(
'$unset' => array('products.productId.100343' => "")
)
);
But really you need to change your data, so to serialize a proper array for MongoDB you need to structure code like this:
$data = array(
"products" => array(
array(
"productId" => 100343,
"quantity" => 13,
"name" => "a"
),
array(
"productId" => 100344,
"quantity" => 3,
"name" => "ab"
),
array(
"productId" => 100345,
"quantity" => 1,
"name" => "abc"
)
)
);
That produces a JSON structure that looks like this:
{
"products":[
{
"productId":100343,
"quantity":13,
"name":"a"
},
{
"productId":100344,
"quantity":3,
"name":"ab"
},
{
"productId":100345,
"quantity":1,
"name":"abc"
}
]
}
That is what MongoDB calls an array, so now you can use the $pull operator correctly:
$c->update(
$aQuery,
array(
'$pull' => array( 'products.productId' => 100343 )
)
);

Categories