Issue
I have spent a week on this, and I am trying to come up with a way to access the data in the multi-dimensional array and output it to another array, restructured differently, based on a value within the array. Rather than post rows and rows of broken code, I present my example simplified:
Here is the original array:
array(2) {
[0] => array(2) {
["aaa"] => array(2) {
["a19a3234b881ce"] => array(2) {
["pid"] => int(29301)
["vid"] => int(29334)
["idx"] => int(88888)
}
["a7aa94c38aa49caa"] => array(2) {
["pid"] => int(20568)
["vid"] => int(26547)
["idx"] => int(88888)
}
}
["bbb"] => array(2) {
["bsfn"] => string(4) "Apple"
["bsln"] => string(8) "AppleApple"
}
}
[1] => array(2) {
["aaa"] => array(3) {
["abd296a10"] => array(2) {
["pid"] => int(56734)
["vid"] => int(98612)
["idx"] => int(99999)
}
["a31e920fde8"] => array(2) {
["pid"] => int(09800)
["vid"] => int(34521)
["idx"] => int(99999)
}
["a7aa94c38aa49caa"] => array(2) {
["pid"] => int(20568)
["vid"] => int(26547)
["idx"] => int(99999)
}
}
["bbb"] => array(2) {
["bsfn"] => string(4) "Ball"
["bsln"] => string(8) "BallBall"
}
}
}
Here is what I want to output:
array(2) {
["88888"] => array(3) {
["a19a3234b881ce"] => array(4) {
["pid"] => int(29301)
["vid"] => int(29334)
["idx"] => int(88888)
["arr"] => string(14) "a19a3234b881ce"
}
["a7aa94c38aa49caa"] => array(4) {
["pid"] => int(20568)
["vid"] => int(26547)
["idx"] => int(88888)
["arr"] => string(16) "a7aa94c38aa49caa"
}
["bbb"] => array(2) {
["bsfn"] => string(4) "Apple"
["bsln"] => string(8) "AppleApple"
}
}
["99999"] => array(4) {
["abd296a10"] => array(4) {
["pid"] => int(29301)
["vid"] => int(29334)
["idx"] => int(99999)
["arr"] => string(9) "abd296a10"
}
["a31e920fde8"] => array(4) {
["pid"] => int(20568)
["vid"] => int(26547)
["idx"] => int(99999)
["arr"] => string(11) "a31e920fde8"
}
["a7aa94c38aa49caa"] => array(4) {
["pid"] => int(20568)
["vid"] => int(26547)
["idx"] => int(99999)
["arr"] => string(16) "a7aa94c38aa49caa"
}
["bbb"] => array(2) {
["bsfn"] => string(4) "Ball"
["bsln"] => string(8) "BallBall"
}
}
}
Solution
Try this example:
$result = [];
foreach($data as $item) {
if (!empty($item['aaa']) && is_array($item['aaa'])) {
$key = array_values($item['aaa'])[0]['idx'] ?? null;
if ($key) {
$result[$key] = array_merge($item['aaa'], ['bbb' => $item['bbb'] ?? []]);
}
}
}
// Test result:
print_r($result);
Answered By - id'7238 Answer Checked By - Timothy Miller (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.