How to return each path and its hash from multidimensional array?
I'm trying to return each path of a multidimensional array along with its hash value. I've used this solution:
function newMethod($tree) {
$hash = "";
$final = array();
foreach ($tree as $key => &$mixed) {
if (is_array($mixed) || is_object($mixed)) {
$results = newMethod($mixed);
foreach ($results as $k => &$v) {
$hash = md5($key . $v);
if(is_array($mixed)) {
$final[$hash] = array($key => $v);
}
elseif(is_object($mixed)) {
$final[$hash] = array($key => (object)$v);
}
}
unset($results);
} else {
$hash = md5($key. $mixed);
$final[$hash] = array($key => $mixed);
}
}
return $final;
}
Using this function provides the hash value for each section of the path separately and doesn't connect them together. How can I change this solution to get an output like this:
Array
(
[0] => Array
(
[Hash of first path] => Array
(
[a] => b
)
)
[1] => Array
(
[Hash of second path] => Array
(
[c] => stdClass Object
(
[d] => Array
(
[1] => 11
)
)
)
)
)
...
The simple tree and a part of output are as follows:
$new = [
'a' => 'b',
'c' => (object)[
'd' => [1 => 11, 2 => 12],
'f' => 'Hello',
],
];
//output
[0] => Array
(
[187ef4436122d1cc2f40dc2b92f0eba0] => Array
(
[a] => b
)
)
[1] => Array
(
[595448f796fd1d2d5f192949d79ba9df] => Array
(
[c] => stdClass Object
(
[f801394efb8c6ca75f1ab6a639556520] => Array
(
[d] => Array
(
[698d51a19d8a121ce581499d7b701668] => Array
(
[1] => 11
)
)
)
)
)
)
...
php arrays multidimensional-array hash tree
add a comment |
I'm trying to return each path of a multidimensional array along with its hash value. I've used this solution:
function newMethod($tree) {
$hash = "";
$final = array();
foreach ($tree as $key => &$mixed) {
if (is_array($mixed) || is_object($mixed)) {
$results = newMethod($mixed);
foreach ($results as $k => &$v) {
$hash = md5($key . $v);
if(is_array($mixed)) {
$final[$hash] = array($key => $v);
}
elseif(is_object($mixed)) {
$final[$hash] = array($key => (object)$v);
}
}
unset($results);
} else {
$hash = md5($key. $mixed);
$final[$hash] = array($key => $mixed);
}
}
return $final;
}
Using this function provides the hash value for each section of the path separately and doesn't connect them together. How can I change this solution to get an output like this:
Array
(
[0] => Array
(
[Hash of first path] => Array
(
[a] => b
)
)
[1] => Array
(
[Hash of second path] => Array
(
[c] => stdClass Object
(
[d] => Array
(
[1] => 11
)
)
)
)
)
...
The simple tree and a part of output are as follows:
$new = [
'a' => 'b',
'c' => (object)[
'd' => [1 => 11, 2 => 12],
'f' => 'Hello',
],
];
//output
[0] => Array
(
[187ef4436122d1cc2f40dc2b92f0eba0] => Array
(
[a] => b
)
)
[1] => Array
(
[595448f796fd1d2d5f192949d79ba9df] => Array
(
[c] => stdClass Object
(
[f801394efb8c6ca75f1ab6a639556520] => Array
(
[d] => Array
(
[698d51a19d8a121ce581499d7b701668] => Array
(
[1] => 11
)
)
)
)
)
)
...
php arrays multidimensional-array hash tree
Can you post the tree?
– Ussaid Iqbal
Jan 19 at 9:41
Can you add the data in your desire output? should the "first path" be string as"a->b"
?
– dWinder
Jan 22 at 13:46
add a comment |
I'm trying to return each path of a multidimensional array along with its hash value. I've used this solution:
function newMethod($tree) {
$hash = "";
$final = array();
foreach ($tree as $key => &$mixed) {
if (is_array($mixed) || is_object($mixed)) {
$results = newMethod($mixed);
foreach ($results as $k => &$v) {
$hash = md5($key . $v);
if(is_array($mixed)) {
$final[$hash] = array($key => $v);
}
elseif(is_object($mixed)) {
$final[$hash] = array($key => (object)$v);
}
}
unset($results);
} else {
$hash = md5($key. $mixed);
$final[$hash] = array($key => $mixed);
}
}
return $final;
}
Using this function provides the hash value for each section of the path separately and doesn't connect them together. How can I change this solution to get an output like this:
Array
(
[0] => Array
(
[Hash of first path] => Array
(
[a] => b
)
)
[1] => Array
(
[Hash of second path] => Array
(
[c] => stdClass Object
(
[d] => Array
(
[1] => 11
)
)
)
)
)
...
The simple tree and a part of output are as follows:
$new = [
'a' => 'b',
'c' => (object)[
'd' => [1 => 11, 2 => 12],
'f' => 'Hello',
],
];
//output
[0] => Array
(
[187ef4436122d1cc2f40dc2b92f0eba0] => Array
(
[a] => b
)
)
[1] => Array
(
[595448f796fd1d2d5f192949d79ba9df] => Array
(
[c] => stdClass Object
(
[f801394efb8c6ca75f1ab6a639556520] => Array
(
[d] => Array
(
[698d51a19d8a121ce581499d7b701668] => Array
(
[1] => 11
)
)
)
)
)
)
...
php arrays multidimensional-array hash tree
I'm trying to return each path of a multidimensional array along with its hash value. I've used this solution:
function newMethod($tree) {
$hash = "";
$final = array();
foreach ($tree as $key => &$mixed) {
if (is_array($mixed) || is_object($mixed)) {
$results = newMethod($mixed);
foreach ($results as $k => &$v) {
$hash = md5($key . $v);
if(is_array($mixed)) {
$final[$hash] = array($key => $v);
}
elseif(is_object($mixed)) {
$final[$hash] = array($key => (object)$v);
}
}
unset($results);
} else {
$hash = md5($key. $mixed);
$final[$hash] = array($key => $mixed);
}
}
return $final;
}
Using this function provides the hash value for each section of the path separately and doesn't connect them together. How can I change this solution to get an output like this:
Array
(
[0] => Array
(
[Hash of first path] => Array
(
[a] => b
)
)
[1] => Array
(
[Hash of second path] => Array
(
[c] => stdClass Object
(
[d] => Array
(
[1] => 11
)
)
)
)
)
...
The simple tree and a part of output are as follows:
$new = [
'a' => 'b',
'c' => (object)[
'd' => [1 => 11, 2 => 12],
'f' => 'Hello',
],
];
//output
[0] => Array
(
[187ef4436122d1cc2f40dc2b92f0eba0] => Array
(
[a] => b
)
)
[1] => Array
(
[595448f796fd1d2d5f192949d79ba9df] => Array
(
[c] => stdClass Object
(
[f801394efb8c6ca75f1ab6a639556520] => Array
(
[d] => Array
(
[698d51a19d8a121ce581499d7b701668] => Array
(
[1] => 11
)
)
)
)
)
)
...
php arrays multidimensional-array hash tree
php arrays multidimensional-array hash tree
edited Jan 22 at 14:01
F.Ndr
asked Jan 19 at 9:37
F.NdrF.Ndr
275
275
Can you post the tree?
– Ussaid Iqbal
Jan 19 at 9:41
Can you add the data in your desire output? should the "first path" be string as"a->b"
?
– dWinder
Jan 22 at 13:46
add a comment |
Can you post the tree?
– Ussaid Iqbal
Jan 19 at 9:41
Can you add the data in your desire output? should the "first path" be string as"a->b"
?
– dWinder
Jan 22 at 13:46
Can you post the tree?
– Ussaid Iqbal
Jan 19 at 9:41
Can you post the tree?
– Ussaid Iqbal
Jan 19 at 9:41
Can you add the data in your desire output? should the "first path" be string as
"a->b"
?– dWinder
Jan 22 at 13:46
Can you add the data in your desire output? should the "first path" be string as
"a->b"
?– dWinder
Jan 22 at 13:46
add a comment |
0
active
oldest
votes
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%2f54265766%2fhow-to-return-each-path-and-its-hash-from-multidimensional-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f54265766%2fhow-to-return-each-path-and-its-hash-from-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
Can you post the tree?
– Ussaid Iqbal
Jan 19 at 9:41
Can you add the data in your desire output? should the "first path" be string as
"a->b"
?– dWinder
Jan 22 at 13:46