I have data saved in JSON format (Prestashop) - I need to get to that data - which is deeply nested in arrays.
Here is the function:
public static function getAllCustomizedDatas($id_cart, $id_lang = null, $only_in_cart = true, $id_shop = null)
{
$datas = parent::getAllCustomizedDatas($id_cart, $id_lang, $only_in_cart, $id_shop);
var_dump($datas);
/*
* Iterate over $datas, you're looking for
* [id_product][id_product_attribute][id_address_delivery][id_customization][datas]
* Datas will contain an array of fields broken by their type. You can then decode
* the ones that need to be decoded and return the result:
*/
return $datas;
}
if I var_dump $datas I see this (I formatted this to make it a little easier for myself to read):
array(1) {
[8]=> array(1) {
[0]=> array(1) {
[0]=> array(2) {
[22]=> array(4) {
["datas"]=> array(1) {
[1]=> array(1) {
[0]=> array(9) {
["id_customization"]=> string(2) "22"
["id_address_delivery"]=> string(1) "0"
["id_product"]=> string(1) "8"
["id_customization_field"]=> string(1) "2"
["id_product_attribute"]=> string(1) "0"
["type"]=> string(1) "1"
["index"]=> string(1) "2"
["value"]=> string(615) "[[{"name":"item[1][line1]","customization":"asdf"},{"name":"item[1][line2]","customization":""},{"name":"item[1][line3]","customization":""},{"name":"item[1][line4]","customization":""},{"name":"item[1][line5]","customization":""},{"name":"item[1][line6]","customization":""},{"name":"item[1][line7]","customization":""}],[{"name":"item[2][line1]","customization":"asdf"},{"name":"item[2][line2]","customization":""},{"name":"item[2][line3]","customization":""},{"name":"item[2][line4]","customization":""},{"name":"item[2][line5]","customization":""},{"name":"item[2][line6]","customization":""},{"name":"item[2][line7]","customization":""}]]"
["name"]=> string(20) "Client Customization"
}
}
}
["quantity"]=> int(2)
["quantity_refunded"]=> int(0)
["quantity_returned"]=> int(0)
}
[23]=> array(4) {
["datas"]=> array(1) {
[1]=> array(1) {
[0]=> array(9) {
["id_customization"]=> string(2) "23"
["id_address_delivery"]=> string(1) "0"
["id_product"]=> string(1) "8"
["id_customization_field"]=> string(1) "2"
["id_product_attribute"]=> string(1) "0"
["type"]=> string(1) "1"
["index"]=> string(1) "2"
["value"]=> string(615) "[[{"name":"item[1][line1]","customization":"asdf"},{"name":"item[1][line2]","customization":""},{"name":"item[1][line3]","customization":""},{"name":"item[1][line4]","customization":""},{"name":"item[1][line5]","customization":""},{"name":"item[1][line6]","customization":""},{"name":"item[1][line7]","customization":""}],[{"name":"item[2][line1]","customization":"asdf"},{"name":"item[2][line2]","customization":""},{"name":"item[2][line3]","customization":""},{"name":"item[2][line4]","customization":""},{"name":"item[2][line5]","customization":""},{"name":"item[2][line6]","customization":""},{"name":"item[2][line7]","customization":""}]]"
["name"]=> string(20) "Client Customization"
}
}
}
["quantity"]=> int(2)
["quantity_refunded"]=> int(0)
["quantity_returned"]=> int(0)
}
}
}
}
}
What will be the easiest way to get to the ["value"] portion of the deeply nested array?
This has to be fairly dynamic because depending on the amount of items this user has - the amount of arrays will change. In this example, there are 2 there (each with 2 items in 'value'). A user could add 3 or 4 or 10 items if they wanted to. But I'm just trying to get to ['value'] and convert that JSON into HTML for the view this is getting passed to.
Bonus: Know of a way to easily iterate through the JSON data?
The JSON data looks like this:
[
[{
"name": "item[1][line1]",
"customization": "asdf"
}, {
"name": "item[1][line2]",
"customization": ""
}, {
"name": "item[1][line3]",
"customization": ""
}, {
"name": "item[1][line4]",
"customization": ""
}, {
"name": "item[1][line5]",
"customization": ""
}, {
"name": "item[1][line6]",
"customization": ""
}, {
"name": "item[1][line7]",
"customization": ""
}],
[{
"name": "item[2][line1]",
"customization": "asdf"
}, {
"name": "item[2][line2]",
"customization": ""
}, {
"name": "item[2][line3]",
"customization": ""
}, {
"name": "item[2][line4]",
"customization": ""
}, {
"name": "item[2][line5]",
"customization": ""
}, {
"name": "item[2][line6]",
"customization": ""
}, {
"name": "item[2][line7]",
"customization": ""
}]
]
I ended up just nesting a bunch of foreach statements until I had arrived at the array I wanted to. It was ugly, but functional.
Then I did json_decode across the JSON and iterated across it.
https://packagist.org/packages/ishworkh/multi-level-array-iterator
This might be helpful in iterating through deep nested array. And then use key or hierarchy information to filter out value needed for you.
Lets say this is your array
$array = [
[
[
[
[
"datas" => [
[
[
"id_customization" => "22",
"id_address_delivery" => "0",
"id_product" => "8",
"id_customization_field" => "2",
"id_product_attribute" => "0",
"type" => "1",
"index" => "2",
"value" => '[[{"name":"item[1][line1]","customization":"asdf"},{"name":"item[1][line2]","customization":""},{"name":"item[1][line3]","customization":""},{"name":"item[1][line4]","customization":""},{"name":"item[1][line5]","customization":""},{"name":"item[1][line6]","customization":""},{"name":"item[1][line7]","customization":""}],[{"name":"item[2][line1]","customization":"asdf"},{"name":"item[2][line2]","customization":""},{"name":"item[2][line3]","customization":""},{"name":"item[2][line4]","customization":""},{"name":"item[2][line5]","customization":""},{"name":"item[2][line6]","customization":""},{"name":"item[2][line7]","customization":""}]]',
"name" => "Client Customization",
],
],
],
"quantity" => 2,
"quantity_refunded" => 0,
"quantity_returned" => 0,
],
[
[
[
[
"id_customization" => "23",
"id_address_delivery" => "0",
"id_product" => "8",
"id_customization_field" => "2",
"id_product_attribute" => "0",
"type" => "1",
"index" => "2",
"value" => '[[{"name":"item[1][line1]","customization":"asdf"},{"name":"item[1][line2]","customization":""},{"name":"item[1][line3]","customization":""},{"name":"item[1][line4]","customization":""},{"name":"item[1][line5]","customization":""},{"name":"item[1][line6]","customization":""},{"name":"item[1][line7]","customization":""}],[{"name":"item[2][line1]","customization":"asdf"},{"name":"item[2][line2]","customization":""},{"name":"item[2][line3]","customization":""},{"name":"item[2][line4]","customization":""},{"name":"item[2][line5]","customization":""},{"name":"item[2][line6]","customization":""},{"name":"item[2][line7]","customization":""}]]',
"name" => "Client Customization",
],
],
],
"quantity" => 2,
"quantity_refunded" => 0,
"quantity_returned" => 0,
],
],
],
],
];
You could create a function like this, that uses multi-level-array-iterator's iterate method to go over all the nested array and
yield out found values.
/**
* #param array $nestedArray
*
* #return Generator
*/
function extractValuesFromArray(array $nestedArray):Generator
{
// $key is the local index key, which in this case should be 'value' we are looking to filter for
foreach(\ArrayIterator\ArrayIteratorFacade::iterate($nestedArray) as $key => $ArrayElement)
{
if ('value' === $key)
{
yield $ArrayElement->getValue();
}
}
}
var_dump(iterator_to_array(extractValuesFromArray($array)));
should give
array(2) {
[0]=>
string(643) "[[{"name":"item[1][line1]","customization":"asdf"},{"name":"item[1][line2]","customization":""},{"name":"item[1][line3]","customization":""},{"name":"item[1][line4]","customization":""},{"name":"item[1][line5]","customization":""},{"name":"item[1][line6]","customization":""},{"name":"item[1][line7]","customization":""}],[{"name":"item[2][line1]","customization":"asdf"},{"name":"item[2][line2]","customization":""},{"name":"item[2][line3]","customization":""},{"name":"item[2][line4]","customization":""},{"name":"item[2][line5]","customization":""},{"name":"item[2][line6]","customization":""},{"name":"item[2][line7]","customization":""}]]"
[1]=>
string(643) "[[{"name":"item[1][line1]","customization":"asdf"},{"name":"item[1][line2]","customization":""},{"name":"item[1][line3]","customization":""},{"name":"item[1][line4]","customization":""},{"name":"item[1][line5]","customization":""},{"name":"item[1][line6]","customization":""},{"name":"item[1][line7]","customization":""}],[{"name":"item[2][line1]","customization":"asdf"},{"name":"item[2][line2]","customization":""},{"name":"item[2][line3]","customization":""},{"name":"item[2][line4]","customization":""},{"name":"item[2][line5]","customization":""},{"name":"item[2][line6]","customization":""},{"name":"item[2][line7]","customization":""}]]"
}
Related
I am currently stuck in json formatting for php. I have given my outputted json below. What I need to do is to make the format of the current json to the desired one. I am missing the arrays in the JSON format. Can anyone help me on this.
My code to print the json output is below:
$menuHead=array();
$i=0;
foreach($res as $key => $value){
$i=$key+1;
//$menuHead[$i]['menuHead']=$value['category'];
if(isset($menuHead[$key]['menuHead'])){
if($menuHead[$key]['menuHead']==$value['category']){
$menuHead[$key]['data'][$i]['itemName']=$value['sub_category'];
$menuHead[$key]['data'][$i]['price']=$value['price'];
$menuHead[$key]['data'][$i]['description']=$value['description'];
$menuHead[$key]['data'][$i]['itemId']=$value['id'];
$menuHead[$key]['data'][$i]['customizable']=$value['customizable'];
}else{
$menuHead[$i]['menuHead']=$value['category'];
$menuHead[$i]['data'][$i]['itemName']=$value['sub_category'];
$menuHead[$i]['data'][$i]['price']=$value['price'];
$menuHead[$i]['data'][$i]['description']=$value['description'];
$menuHead[$i]['data'][$i]['itemId']=$value['id'];
$menuHead[$i]['data'][$i]['customizable']=$value['customizable'];
}
}else{
$menuHead[$i]['menuHead']=$value['category'];
$menuHead[$i]['data'][$i]['itemName']=$value['sub_category'];
$menuHead[$i]['data'][$i]['price']=$value['price'];
$menuHead[$i]['data'][$i]['description']=$value['description'];
$menuHead[$i]['data'][$i]['itemId']=$value['id'];
$menuHead[$i]['data'][$i]['customizable']=$value['customizable'];
}
}
$final['MenuList']=$menuHead;
echo json_encode($final);
Current format:
{
"MenuList": {
"1": {
"menuHead": "Main Course",
"data": {
"1": {
"itemName": "Chicken Thai Curry",
"price": "599",
"description": "",
"itemId": "67",
"customizable": "1"
}
}
},
"2": {
"menuHead": "Refreshments",
"data": {
"2": {
"itemName": "Kingfisher Premium",
"price": "999",
"description": "Kingfisher beer",
"itemId": "69",
"customizable": "1"
},
"3": {
"itemName": "Mocktail",
"price": "999",
"description": "",
"itemId": "68",
"customizable": "1"
}
}
},
"4": {
"menuHead": "Rice biriyani",
"data": {
"4": {
"itemName": "Dal makni risotto",
"price": "499",
"description": "Dal makhni risotto",
"itemId": "66",
"customizable": "1"
}
}
}
}
}
Desired Format:
{
"menuList": [
{
"menuHead": "In Steamer (Momos)",
"data": [
{
"itemName": "Tandoori Momo",
"description": "",
"price": "150",
"itemId": "16",
"customizable": "0"
},
{
"itemName": "Fried Momo Pork",
"price": "100",
"description": "",
"itemId": "15",
"customizable": "0"
}
]
},
{
"itemName": "Rice and Noodles",
"data": [
{
"sub_category": "Tandoori Momo",
"description": "",
"price": "150",
"itemId": "16",
"customizable": "0"
},
{
"itemName": "Fried Momo Pork",
"price": "100",
"description": "",
"itemId": "15",
"customizable": "0"
}
]
}
]
}
Raw response is below:
array(4) { [0]=> array(7) { ["id"]=> string(2) "67" ["restaurant_id"]=> string(1) "5" ["category"]=> string(11) "Main Course" ["sub_category"]=> string(18) "Chicken Thai Curry" ["price"]=> string(3) "599" ["description"]=> string(0) "" ["customizable"]=> string(1) "1" } [1]=> array(7) { ["id"]=> string(2) "69" ["restaurant_id"]=> string(1) "5" ["category"]=> string(12) "Refreshments" ["sub_category"]=> string(18) "Kingfisher Premium" ["price"]=> string(3) "999" ["description"]=> string(15) "Kingfisher beer" ["customizable"]=> string(1) "1" } [2]=> array(7) { ["id"]=> string(2) "68" ["restaurant_id"]=> string(1) "5" ["category"]=> string(12) "Refreshments" ["sub_category"]=> string(8) "Mocktail" ["price"]=> string(3) "999" ["description"]=> string(0) "" ["customizable"]=> string(1) "1" } [3]=> array(7) { ["id"]=> string(2) "66" ["restaurant_id"]=> string(1) "5" ["category"]=> string(13) "Rice biriyani" ["sub_category"]=> string(17) "Dal makni risotto" ["price"]=> string(3) "499" ["description"]=> string(18) "Dal makhni risotto" ["customizable"]=> string(1) "1" } }
If you want a javascript compatible array, the index must start at 0. The easiest way to do that, is to use array_values():
$final['MenuList'] = array_values($menuHead);
The problem is that when your adding the data items, you need to add them without specific keys, as you add them with $i as in...
$menuHead[$key]['data'][$i]['itemName']=$value['sub_category'];
This will stop them being a normal array as you want it to be. For json_encode() an array must start at 0 and be sequential for it to be an array.
Instead create them in one go and add them to the end of the existing data using []...
$menuHead[$key]['data'][] = ['itemName' =>$value['sub_category'],
'price'=> $value['price'],
'description'=>$value['description'],
'itemId'=>$value['id'],
'customizable'=>$value['customizable']];
This needs to be done with each set of similar code, which includes the overall array itself, this can be done using
$final['MenuList'] = array_values($menuHead);
To try and fix the data you already have, which means no changes except adding the following code...
foreach ( $menuHead as $menu ) {
$menu['data'] = array_values($menu['data']);
}
$final['MenuList'] = array_values($menuHead);
Use array_values();
I fixed your code, it should work
$menuHead=array();
$i=0;
foreach($res as $key => $value){
$i=$key+1;
//$menuHead[$i]['menuHead']=$value['category'];
if(isset($menuHead[$key]['menuHead'])){
if($menuHead[$key]['menuHead']==$value['category']){
$menuHead[$key]['data'][$i]['itemName']=$value['sub_category'];
$menuHead[$key]['data'][$i]['price']=$value['price'];
$menuHead[$key]['data'][$i]['description']=$value['description'];
$menuHead[$key]['data'][$i]['itemId']=$value['id'];
$menuHead[$key]['data'][$i]['customizable']=$value['customizable'];
}else{
$menuHead[$i]['menuHead']=$value['category'];
$menuHead[$i]['data'][$i]['itemName']=$value['sub_category'];
$menuHead[$i]['data'][$i]['price']=$value['price'];
$menuHead[$i]['data'][$i]['description']=$value['description'];
$menuHead[$i]['data'][$i]['itemId']=$value['id'];
$menuHead[$i]['data'][$i]['customizable']=$value['customizable'];
}
}else{
$menuHead[$i]['menuHead']=$value['category'];
$menuHead[$i]['data'][$i]['itemName']=$value['sub_category'];
$menuHead[$i]['data'][$i]['price']=$value['price'];
$menuHead[$i]['data'][$i]['description']=$value['description'];
$menuHead[$i]['data'][$i]['itemId']=$value['id'];
$menuHead[$i]['data'][$i]['customizable']=$value['customizable'];
}
}
// i'am use array_values()
$final['MenuList']= array_values($menuHead);
echo json_encode($final);
I need to convert simple array to nested array according to specific rules. I've achived it but I'm looking for better solution.
SIMPLE:
array(4) {
[0]=>
array(2) {
["id"]=>
string(2) "11"
["type"]=>
int(3)
}
[1]=>
array(2) {
["id"]=>
string(2) "10"
["type"]=>
int(2)
}
[2]=>
array(2) {
["id"]=>
string(1) "1"
["type"]=>
int(1)
}
[3]=>
array(2) {
["id"]=>
string(1) "0"
["type"]=>
int(1)
}
}
EXPECTED EFFECT:
array(1) {
[0]=>
array(2) {
["type"]=>
int(1)
["child"]=>
array(1) {
[1]=>
array(2) {
["type"]=>
int(1)
["child"]=>
array(1) {
[10]=>
array(2) {
["type"]=>
int(2)
["child"]=>
array(1) {
[11]=>
array(2) {
["type"]=>
int(3)
["child"]=>
array(0) {
}
}
}
}
}
}
}
}
}
MY SOLUTION (not very satisfying):
$nestedArray = [];
foreach ($simpleArray as $item)
{
if (!empty($nestedArray))
{
$array = $nestedArray;
reset($array);
$firstKey = key($array);
}
$nestedArray[$item['id']]['child'] = $nestedArray;
$nestedArray[$item['id']]['type'] = $item['type'];
if (!empty($firstKey))
{
unset($nestedArray[$firstKey]);
}
}
As I said, I'm looking for more elegant way to achieve that. Rule are very simply: every next item is child of previous.
You could use recursion:
function nest($arr) {
return count($arr) ? ["type" => array_pop($arr)["type"], "child" => nest($arr)] : [];
}
With your example input, it would look like this:
$simpleArray = [
["id" => "11", "type" => 3],
["id" => "10", "type" => 2],
["id" => "1", "type" => 1],
["id" => "0", "type" => 1]
];
function nest($arr) {
return count($arr) ? ["type" => array_pop($arr)["type"], "child" => nest($arr)] : [];
}
$nested = nest($simpleArray));
$nested will have the following value:
[
"type" => 1,
"child" => [
"type" => 1,
"child" => [
"type" => 2,
"child" => [
"type" => 3,
"child" => []
]
]
]
]
I'm using a database system for some kind of gaming competition where players can team up and participate in events. Right now players have their own database, then there is a database for teams (team id, name, join password, etc) and a database where I save which player (player name/id is in which team name/id). My database call gives me an array with team_name, username, etc. I wanted to group the users by a common team_name value which worked, but I'm not able to rewrite it according to my needs.
I want to build some kind of api for my personal use in C# application. There I want the output of my script to be json and I want to show all the teams with some details. Right now im using the following code:
$team_keys = array();
foreach ($team_data AS $k => $sub_array)
{
$this_team = $sub_array['team_name'];
$team_keys[$this_team][$k] = array('username' => $sub_array['username']);
}
echo json_encode($team_keys, JSON_PRETTY_PRINT);
This gives me an output like this:
{
"Team1": {
"0": {
"username": "player1"
},
"1": {
"username": "player2"
},
"22": {
"username": "player3"
}
},
"Team2": {
"2": {
"username": "player4"
},
"3": {
"username": "player5"
}, .....
}
But I want to achieve something like:
{
"team_name": "Team1",
"team_password": "secret",
"creation_timestamp": "123456789",
"players": [
"Player1",
"Player2",
"Player3"
]
}, ....
I tried tons of different approaches but I was simply not able to regroup and rewrite the array to my needs. Hopefully someone can help me out.
EDIT: $team_data looks like this (I'm using JOIN to join my user, teams, and team_member tables to get all the data together):
array(83) {
[0]=>
array(4) {
["username"]=>
string(8) "Player1"
["team_name"]=>
string(8) "Team1"
["team_password"]=>
string(7) "secret"
["team_id"]=>
string(1) "1"
}
[1]=>
array(4) {
["username"]=>
string(11) "Player2"
["team_name"]=>
string(8) "Team1"
["team_password"]=>
string(7) "secret"
["team_id"]=>
string(1) "1"
}
[2]=>
array(4) {
["username"]=>
string(8) "Player3"
["team_name"]=>
string(10) "Team2"
["team_password"]=>
string(6) "ultrasecret"
["team_id"]=>
string(1) "2"
},...
So Input was the following data
array(4) {
[0]=>
array(4) {
["team_name"]=>
string(5) "Team1"
["username"]=>
string(7) "player1"
["team_password"]=>
string(6) "secret"
["team_id"]=>
int(1)
}
[1]=>
array(4) {
["team_name"]=>
string(5) "Team1"
["username"]=>
string(7) "player2"
["team_password"]=>
string(6) "secret"
["team_id"]=>
int(1)
}
[2]=>
array(4) {
["team_name"]=>
string(5) "Team2"
["username"]=>
string(7) "player1"
["team_password"]=>
string(6) "secret"
["team_id"]=>
int(2)
}
[3]=>
array(4) {
["team_name"]=>
string(5) "Team2"
["username"]=>
string(7) "player2"
["team_password"]=>
string(6) "secret"
["team_id"]=>
int(2)
}
}
This is my code with my testset of $team_data
<?php
$team_data = array(array('team_name' => 'Team1', 'username' => 'player1', 'team_password' => 'secret', 'team_id' => 1)
,array('team_name' => 'Team1', 'username' => 'player2', 'team_password' => 'secret', 'team_id' => 1)
,array('team_name' => 'Team2', 'username' => 'player1', 'team_password' => 'secret', 'team_id' => 2)
,array('team_name' => 'Team2', 'username' => 'player2', 'team_password' => 'secret', 'team_id' => 2)
);
$team_keys = array();
foreach ($team_data AS $player_dara) {
if (!isset($team_keys[$player_dara['team_id']])) {
$team_keys[$player_dara['team_id']] = array();
$team_keys[$player_dara['team_id']]['team_name'] = $player_dara['team_name'];
$team_keys[$player_dara['team_id']]['team_id'] = $player_dara['team_id'];
$team_keys[$player_dara['team_id']]['secret'] = $player_dara['secret'];
$team_keys[$player_dara['team_id']]['players'] = array();
}
$team_keys[$player_dara['team_id']]['players'][] = $player_dara['username'];
}
echo json_encode(array_values($team_keys), JSON_PRETTY_PRINT);
Output
[
{
"team_name": "Team1",
"team_id": 1,
"secret": null,
"players": [
"player1",
"player2"
]
},
{
"team_name": "Team2",
"team_id": 2,
"secret": null,
"players": [
"player1",
"player2"
]
}
]
Hi guys I'm trying to map key with values in PHP to create JSON object, can someone help please.
My keys array:
[
"ID",
"NAME",
"PRICE",
"TYPE"
]
My values array:
[
[
"1",
"Chicken Royal",
"25",
"Sandwich"
],
[
"2",
"Beef Whopper",
"30",
"Burger"
],
[
"3",
"Beef Royal",
"30",
"Burger"
]
]
What I'm looking for is:
[
"ID":"1",
"NAME":"Chicken Royal",
"PRICE":"25",
"TYPE":"Sandwich"
]
I've used this function
$result = array_map( function($k,$v) { return array('column' => $k,'value' => $v); }, array_keys($columnNames),$values);
With array_combine you can creates an array by using one array for keys and another for its values.
$keys = [
"ID",
"NAME",
"PRICE",
"TYPE"
];
$values = [
[
"1",
"Chicken Royal",
"25",
"Sandwich"
],
[
"2",
"Beef Whopper",
"30",
"Burger"
],
[
"3",
"Beef Royal",
"30",
"Burger"
]
];
$results = array_map(function($values) use ($keys) {
return array_combine($keys, $values);
}, $values);
var_dump($results);
Output:
array(3) {
[0]=>
array(4) {
["ID"]=>
string(1) "1"
["NAME"]=>
string(13) "Chicken Royal"
["PRICE"]=>
string(2) "25"
["TYPE"]=>
string(8) "Sandwich"
}
[1]=>
array(4) {
["ID"]=>
string(1) "2"
["NAME"]=>
string(12) "Beef Whopper"
["PRICE"]=>
string(2) "30"
["TYPE"]=>
string(6) "Burger"
}
[2]=>
array(4) {
["ID"]=>
string(1) "3"
["NAME"]=>
string(10) "Beef Royal"
["PRICE"]=>
string(2) "30"
["TYPE"]=>
string(6) "Burger"
}
}
Below is the result and I want to remove duplicate from the array
I tried using this code: $login_data1['items'] = array_values(array_map("unserialize", array_unique(array_map("serialize", $login_data1['items']))));
{
"items": [
{
"id": "2",
"tags": [
{
"name": "Microsoft"
}
],
"type": "manual",
},
{
"id": "1",
"tags": [
{
"name": "Snow Leopard"
}
],
"type": "faq"
},
{
"id": "2",
"tags": [
{
"name": "Microsoft"
}
],
"type": "manual"
}
],
}
I tried using $login_data1['items'] = array_unique($login_data1['items'] ,SORT_REGULAR); but this adds serial numbers at the each json response
Try as using array_unique
$json = '{
"items": [
{
"id": "2",
"tags": [
{
"name": "Microsoft"
}
],
"type": "manual"
},
{
"id": "1",
"tags": [
{
"name": "Snow Leopard"
}
],
"type": "faq"
},
{
"id": "2",
"tags": [
{
"name": "Microsoft"
}
],
"type": "manual"
}
]
}';
foreach(json_decode($json, true) as $key => $value){
$input = array_unique($value,SORT_REGULAR);
}
If its an array then simply use
array_unique($login_data['items'],SORT_REGULAR);
Fiddle
array_unique works perfectly if you pass a multidimensional array.
$login_data1['items'] = array_unique($login_data1['items'], SORT_REGULAR);
It doesn't work with your json because it's an array of object. Infact:
$array = json_decode($json);
var_dump($array);
returns:
object(stdClass)#1 (1) {
["items"]=>
array(3) {
[0]=>
object(stdClass)#2 (3) {
["id"]=>
string(1) "2"
["tags"]=>
array(1) {
[0]=>
object(stdClass)#3 (1) {
["name"]=>
string(9) "Microsoft"
}
}
["type"]=>
string(6) "manual"
}
[1]=>
object(stdClass)#4 (3) {
["id"]=>
string(1) "1"
["tags"]=>
array(1) {
[0]=>
object(stdClass)#5 (1) {
["name"]=>
string(12) "Snow Leopard"
}
}
["type"]=>
string(3) "faq"
}
[2]=>
object(stdClass)#6 (3) {
["id"]=>
string(1) "2"
["tags"]=>
array(1) {
[0]=>
object(stdClass)#7 (1) {
["name"]=>
string(9) "Microsoft"
}
}
["type"]=>
string(6) "manual"
}
}
}
Your json should look like this:
{
"items": [
{
"id": "2",
"tags": {
"name": "Microsoft"
},
"type": "manual"
},
{
"id": "1",
"tags": {
"name": "Snow Leopard"
},
"type": "faq"
},
{
"id": "2",
"tags": {
"name": "Microsoft"
},
"type": "manual"
},
{
"id": "2",
"tags": {
"name": "Microsoft"
},
"type": "manual"
}
]
}
And now:
$array = json_decode($json);
var_dump($array);
returns:
array(1) {
["items"]=>
array(4) {
[0]=>
array(3) {
["id"]=>
string(1) "2"
["tags"]=>
array(1) {
["name"]=>
string(9) "Microsoft"
}
["type"]=>
string(6) "manual"
}
[1]=>
array(3) {
["id"]=>
string(1) "1"
["tags"]=>
array(1) {
["name"]=>
string(12) "Snow Leopard"
}
["type"]=>
string(3) "faq"
}
[2]=>
array(3) {
["id"]=>
string(1) "2"
["tags"]=>
array(1) {
["name"]=>
string(9) "Microsoft"
}
["type"]=>
string(6) "manual"
}
[3]=>
array(3) {
["id"]=>
string(1) "2"
["tags"]=>
array(1) {
["name"]=>
string(9) "Microsoft"
}
["type"]=>
string(6) "manual"
}
}
}
And array_unique works.
I got the solution for this.
$login_data1['items'] =array_values(array_unique($login_data1['items'] ,SORT_REGULAR));
Try array_value and array_unique together and serial number will be removed!
$login_data1['items'] =array_values(array_unique($login_data1['items'] ,SORT_REGULAR));