leave only unique elements in a multidimensional array
How to make only unique values remain
<?php
header('Content-Type: application/json');
function super_unique($array,$key){
$temp_array = ;
foreach ($array as &$v) {
if (!isset($temp_array[$v[$key]]))
$temp_array[$v[$key]] =& $v;
}
$array = array_values($temp_array);
return $array;
}
$arr = array();
$arr[0]['id'] = null;
$arr[0]['name'] = 'John';
$arr[1]['id'] = 12;
$arr[1]['name'] = 'John';
$arr[2]['id'] = null;
$arr[2]['name'] = null;
$arr[3]['id'] = 54;
$arr[3]['name'] = 'Ammie';
$arr[4]['id'] = 23;
$arr[4]['name'] = 'Martin';
$arr[5]['id'] = 54;
$arr[5]['name'] = null;
$arr[6]['id'] = 342;
$arr[6]['name'] = 'Anna';
$arr[7]['id'] = 64;
$arr[7]['name'] = 'Tom';
$arr[8]['id'] = 64;
$arr[8]['name'] = null;
$arr[9]['id'] = 364;
$arr[9]['name'] = null;
$arr[10]['id'] = null;
$arr[10]['name'] = 'Piter';
$arr = super_unique($arr,'id');
$arr = super_unique($arr,'name');
$arr = array_values($arr);
echo json_encode($arr);
?>
I want to achieve such a result
[
{
"id": 12,
"name": "John"
},
{
"id": 54,
"name": "Ammie"
},
{
"id": 23,
"name": "Martin"
},
{
"id": 342,
"name": "Anna"
},
{
"id": 64,
"name": "Tom"
},
{
"id": 364,
"name": null
},
{
"id": null,
"name": 'Piter'
}
]
it doesn’t display the result to me with Peter and John gives the id as null
it is necessary that if the id is the same, then check by name and leave only one and without null, if the only value is null, then only leave null
like id's: 364
null name
and also for names
i expected john id to be 12
Any suggestions? Thanks!
php arrays sorting multidimensional-array
add a comment |
How to make only unique values remain
<?php
header('Content-Type: application/json');
function super_unique($array,$key){
$temp_array = ;
foreach ($array as &$v) {
if (!isset($temp_array[$v[$key]]))
$temp_array[$v[$key]] =& $v;
}
$array = array_values($temp_array);
return $array;
}
$arr = array();
$arr[0]['id'] = null;
$arr[0]['name'] = 'John';
$arr[1]['id'] = 12;
$arr[1]['name'] = 'John';
$arr[2]['id'] = null;
$arr[2]['name'] = null;
$arr[3]['id'] = 54;
$arr[3]['name'] = 'Ammie';
$arr[4]['id'] = 23;
$arr[4]['name'] = 'Martin';
$arr[5]['id'] = 54;
$arr[5]['name'] = null;
$arr[6]['id'] = 342;
$arr[6]['name'] = 'Anna';
$arr[7]['id'] = 64;
$arr[7]['name'] = 'Tom';
$arr[8]['id'] = 64;
$arr[8]['name'] = null;
$arr[9]['id'] = 364;
$arr[9]['name'] = null;
$arr[10]['id'] = null;
$arr[10]['name'] = 'Piter';
$arr = super_unique($arr,'id');
$arr = super_unique($arr,'name');
$arr = array_values($arr);
echo json_encode($arr);
?>
I want to achieve such a result
[
{
"id": 12,
"name": "John"
},
{
"id": 54,
"name": "Ammie"
},
{
"id": 23,
"name": "Martin"
},
{
"id": 342,
"name": "Anna"
},
{
"id": 64,
"name": "Tom"
},
{
"id": 364,
"name": null
},
{
"id": null,
"name": 'Piter'
}
]
it doesn’t display the result to me with Peter and John gives the id as null
it is necessary that if the id is the same, then check by name and leave only one and without null, if the only value is null, then only leave null
like id's: 364
null name
and also for names
i expected john id to be 12
Any suggestions? Thanks!
php arrays sorting multidimensional-array
add a comment |
How to make only unique values remain
<?php
header('Content-Type: application/json');
function super_unique($array,$key){
$temp_array = ;
foreach ($array as &$v) {
if (!isset($temp_array[$v[$key]]))
$temp_array[$v[$key]] =& $v;
}
$array = array_values($temp_array);
return $array;
}
$arr = array();
$arr[0]['id'] = null;
$arr[0]['name'] = 'John';
$arr[1]['id'] = 12;
$arr[1]['name'] = 'John';
$arr[2]['id'] = null;
$arr[2]['name'] = null;
$arr[3]['id'] = 54;
$arr[3]['name'] = 'Ammie';
$arr[4]['id'] = 23;
$arr[4]['name'] = 'Martin';
$arr[5]['id'] = 54;
$arr[5]['name'] = null;
$arr[6]['id'] = 342;
$arr[6]['name'] = 'Anna';
$arr[7]['id'] = 64;
$arr[7]['name'] = 'Tom';
$arr[8]['id'] = 64;
$arr[8]['name'] = null;
$arr[9]['id'] = 364;
$arr[9]['name'] = null;
$arr[10]['id'] = null;
$arr[10]['name'] = 'Piter';
$arr = super_unique($arr,'id');
$arr = super_unique($arr,'name');
$arr = array_values($arr);
echo json_encode($arr);
?>
I want to achieve such a result
[
{
"id": 12,
"name": "John"
},
{
"id": 54,
"name": "Ammie"
},
{
"id": 23,
"name": "Martin"
},
{
"id": 342,
"name": "Anna"
},
{
"id": 64,
"name": "Tom"
},
{
"id": 364,
"name": null
},
{
"id": null,
"name": 'Piter'
}
]
it doesn’t display the result to me with Peter and John gives the id as null
it is necessary that if the id is the same, then check by name and leave only one and without null, if the only value is null, then only leave null
like id's: 364
null name
and also for names
i expected john id to be 12
Any suggestions? Thanks!
php arrays sorting multidimensional-array
How to make only unique values remain
<?php
header('Content-Type: application/json');
function super_unique($array,$key){
$temp_array = ;
foreach ($array as &$v) {
if (!isset($temp_array[$v[$key]]))
$temp_array[$v[$key]] =& $v;
}
$array = array_values($temp_array);
return $array;
}
$arr = array();
$arr[0]['id'] = null;
$arr[0]['name'] = 'John';
$arr[1]['id'] = 12;
$arr[1]['name'] = 'John';
$arr[2]['id'] = null;
$arr[2]['name'] = null;
$arr[3]['id'] = 54;
$arr[3]['name'] = 'Ammie';
$arr[4]['id'] = 23;
$arr[4]['name'] = 'Martin';
$arr[5]['id'] = 54;
$arr[5]['name'] = null;
$arr[6]['id'] = 342;
$arr[6]['name'] = 'Anna';
$arr[7]['id'] = 64;
$arr[7]['name'] = 'Tom';
$arr[8]['id'] = 64;
$arr[8]['name'] = null;
$arr[9]['id'] = 364;
$arr[9]['name'] = null;
$arr[10]['id'] = null;
$arr[10]['name'] = 'Piter';
$arr = super_unique($arr,'id');
$arr = super_unique($arr,'name');
$arr = array_values($arr);
echo json_encode($arr);
?>
I want to achieve such a result
[
{
"id": 12,
"name": "John"
},
{
"id": 54,
"name": "Ammie"
},
{
"id": 23,
"name": "Martin"
},
{
"id": 342,
"name": "Anna"
},
{
"id": 64,
"name": "Tom"
},
{
"id": 364,
"name": null
},
{
"id": null,
"name": 'Piter'
}
]
it doesn’t display the result to me with Peter and John gives the id as null
it is necessary that if the id is the same, then check by name and leave only one and without null, if the only value is null, then only leave null
like id's: 364
null name
and also for names
i expected john id to be 12
Any suggestions? Thanks!
php arrays sorting multidimensional-array
php arrays sorting multidimensional-array
asked Jan 1 at 5:25
Багдаулет СайынБагдаулет Сайын
205
205
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can rewrite your unique method as following:
//This function will get you the missing field from the array:
function getField($arr, $key, $value, $newKey) {
foreach($arr as $e)
if (($e[$key] == $value) && $e[$newKey])
return $e[$newKey];
}
$res = array();
foreach($arr as $a) {
if (!$a["id"] && !$a["name"])
continue; // if both null ignore
if (!$a["id"]) // if id missing go get it
$a["id"] = getField($arr, "name", $a["name"], "id");
if (!$a["name"]) // fill the name if missing
$a["name"] = getField($arr, "id", $a["id"], "name");
$res = $a; // as to result array
}
$r = array_map("json_decode", array_unique(array_map("json_encode", $res))); // remove all duplicate elements
This will out print:
echo json_encode(array_values($r));
[{"id":12,"name":"John"},{"id":54,"name":"Ammie"},{"id":23,"name":"Martin"},{"id":342,"name":"Anna"},{"id":64,"name":"Tom"},{"id":364,"name":null},{"id":null,"name":"Piter"}]
Thank you so much for your help. a happy new year and Merry Christmas
– Багдаулет Сайын
Jan 1 at 7:48
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993188%2fleave-only-unique-elements-in-a-multidimensional-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can rewrite your unique method as following:
//This function will get you the missing field from the array:
function getField($arr, $key, $value, $newKey) {
foreach($arr as $e)
if (($e[$key] == $value) && $e[$newKey])
return $e[$newKey];
}
$res = array();
foreach($arr as $a) {
if (!$a["id"] && !$a["name"])
continue; // if both null ignore
if (!$a["id"]) // if id missing go get it
$a["id"] = getField($arr, "name", $a["name"], "id");
if (!$a["name"]) // fill the name if missing
$a["name"] = getField($arr, "id", $a["id"], "name");
$res = $a; // as to result array
}
$r = array_map("json_decode", array_unique(array_map("json_encode", $res))); // remove all duplicate elements
This will out print:
echo json_encode(array_values($r));
[{"id":12,"name":"John"},{"id":54,"name":"Ammie"},{"id":23,"name":"Martin"},{"id":342,"name":"Anna"},{"id":64,"name":"Tom"},{"id":364,"name":null},{"id":null,"name":"Piter"}]
Thank you so much for your help. a happy new year and Merry Christmas
– Багдаулет Сайын
Jan 1 at 7:48
add a comment |
You can rewrite your unique method as following:
//This function will get you the missing field from the array:
function getField($arr, $key, $value, $newKey) {
foreach($arr as $e)
if (($e[$key] == $value) && $e[$newKey])
return $e[$newKey];
}
$res = array();
foreach($arr as $a) {
if (!$a["id"] && !$a["name"])
continue; // if both null ignore
if (!$a["id"]) // if id missing go get it
$a["id"] = getField($arr, "name", $a["name"], "id");
if (!$a["name"]) // fill the name if missing
$a["name"] = getField($arr, "id", $a["id"], "name");
$res = $a; // as to result array
}
$r = array_map("json_decode", array_unique(array_map("json_encode", $res))); // remove all duplicate elements
This will out print:
echo json_encode(array_values($r));
[{"id":12,"name":"John"},{"id":54,"name":"Ammie"},{"id":23,"name":"Martin"},{"id":342,"name":"Anna"},{"id":64,"name":"Tom"},{"id":364,"name":null},{"id":null,"name":"Piter"}]
Thank you so much for your help. a happy new year and Merry Christmas
– Багдаулет Сайын
Jan 1 at 7:48
add a comment |
You can rewrite your unique method as following:
//This function will get you the missing field from the array:
function getField($arr, $key, $value, $newKey) {
foreach($arr as $e)
if (($e[$key] == $value) && $e[$newKey])
return $e[$newKey];
}
$res = array();
foreach($arr as $a) {
if (!$a["id"] && !$a["name"])
continue; // if both null ignore
if (!$a["id"]) // if id missing go get it
$a["id"] = getField($arr, "name", $a["name"], "id");
if (!$a["name"]) // fill the name if missing
$a["name"] = getField($arr, "id", $a["id"], "name");
$res = $a; // as to result array
}
$r = array_map("json_decode", array_unique(array_map("json_encode", $res))); // remove all duplicate elements
This will out print:
echo json_encode(array_values($r));
[{"id":12,"name":"John"},{"id":54,"name":"Ammie"},{"id":23,"name":"Martin"},{"id":342,"name":"Anna"},{"id":64,"name":"Tom"},{"id":364,"name":null},{"id":null,"name":"Piter"}]
You can rewrite your unique method as following:
//This function will get you the missing field from the array:
function getField($arr, $key, $value, $newKey) {
foreach($arr as $e)
if (($e[$key] == $value) && $e[$newKey])
return $e[$newKey];
}
$res = array();
foreach($arr as $a) {
if (!$a["id"] && !$a["name"])
continue; // if both null ignore
if (!$a["id"]) // if id missing go get it
$a["id"] = getField($arr, "name", $a["name"], "id");
if (!$a["name"]) // fill the name if missing
$a["name"] = getField($arr, "id", $a["id"], "name");
$res = $a; // as to result array
}
$r = array_map("json_decode", array_unique(array_map("json_encode", $res))); // remove all duplicate elements
This will out print:
echo json_encode(array_values($r));
[{"id":12,"name":"John"},{"id":54,"name":"Ammie"},{"id":23,"name":"Martin"},{"id":342,"name":"Anna"},{"id":64,"name":"Tom"},{"id":364,"name":null},{"id":null,"name":"Piter"}]
answered Jan 1 at 7:07
dWinderdWinder
5,63131130
5,63131130
Thank you so much for your help. a happy new year and Merry Christmas
– Багдаулет Сайын
Jan 1 at 7:48
add a comment |
Thank you so much for your help. a happy new year and Merry Christmas
– Багдаулет Сайын
Jan 1 at 7:48
Thank you so much for your help. a happy new year and Merry Christmas
– Багдаулет Сайын
Jan 1 at 7:48
Thank you so much for your help. a happy new year and Merry Christmas
– Багдаулет Сайын
Jan 1 at 7:48
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993188%2fleave-only-unique-elements-in-a-multidimensional-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown