mysql - I Need to Simplify an array from an inner join an array php
Get the solution ↓↓↓I have an array from an inner join statement from 2 Mysql tables... I need them in a better working format for front end display:
mysql query:
SELECT weddings.id, weddings.wImage, images.iLink FROM images INNER JOIN weddings ON weddings.id = images.weddingId
OUTCOME:
Array
(
[0] => Array
(
[id] => 3
[wImage] => image1.jpg
[iLink] => image2.jpg
)
[1] => Array
(
[id] => 3
[wImage] => image1.jpg
[iLink] => image3.jpg
)
[2] => Array
(
[id] => 3
[wImage] => image1.jpg
[iLink] => image4.jpg
)
[3] => Array
(
[id] => 10
[wImage] => image11.jpg
[iLink] => image5.jpg
)
[4] => Array
(
[id] => 10
[wImage] => image11.jpg
[iLink] => image6.jpg
)
[5] => Array
(
[id] => 10
[wImage] => image11.jpg
[iLink] => image7.jpg
)
[6] => Array
(
[id] => 11
[wImage] => image12.jpg
[iLink] => image8.jpg
)
[7] => Array
(
[id] => 11
[wImage] => image12.jpg
[iLink] => image9.jpg
)
[8] => Array
(
[id] => 11
[wImage] => image12.jpg
[iLink] => image10.jpg
)
)
So I need to work this array further to get this expected OUTCOME:
Array
(
[0] => Array
(
[id] => 3
[wImage] => image1.jpg
[iLink] => Array
(
[image] => image2.jpg
[image] => image3.jpg
[image] => image4.jpg
)
)
[1] => Array
(
[id] => 10
[wImage] => image11.jpg
[iLink] => Array
(
[image] => image5.jpg
[image] => image6.jpg
[image] => image7.jpg
)
)
[2] => Array
(
[id] => 10
[wImage] => image12.jpg
[iLink] => Array
(
[image] => image8.jpg
[image] => image9.jpg
[image] => image10.jpg
)
)
)
Answer
Solution:
There are many ways, but assuming yourid
is unique (as I guess), you can use thisid
as an associative key in the output, so you can easily determine whether it exists already. In the last step you reindex the result array. See code comments for further explanation.
// $input_array is the outcome from sql
// $output_array will store the desired outcome
$output_array=[];
// loop through every item in input array
foreach ($input_array as $input_item) {
// if key with "id" already exists in output array
if (array_key_exists($input_item["id"], $output_array)) {
// append image to already existing "iLink" array
array_push($output_array[$input_item["id"]]["iLink"],
["image" => $input_item["iLink"]]
);
}
// if key with id not exists yet in output array
else {
// add current input item to output array,
// with "id" value as key
$output_array[$input_item["id"]] = [
"id" => $input_item["id"],
"wImage" => $input_item["wImage"],
// array with first image, can be appended later
"iLink" =>["image" => $input_item["iLink"]]
];
}
}
// now we have the result, but output array keys are not 0,1,2
// but the id values (3, 10 etc.), so we have to reindex
$output_array = array_values($output_array);
// now it is ready
var_dump($output_array);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: malformed utf-8 characters, possibly incorrectly encoded
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.