I really could use some help with this. I can’t seem to get this right no matter what approach I take. Let me set this up so you can understand the task first.
First we have a data set called $play_array which holds the following information.
Array(
[506] => Array
(
[minus] => Array
(
[406] => Array
(
[all] => Array
(
[0] => 406
[1] => 460
[2] => 046
[3] => 064
[4] => 640
[5] => 604
[6] => 951
[7] => 915
[8] => 591
[9] => 519
[10] => 195
[11] => 159
[12] => 906
[13] => 960
[14] => 096
[15] => 069
[16] => 690
[17] => 609
[18] => 456
[19] => 465
[20] => 546
[21] => 564
[22] => 645
[23] => 654
[24] => 401
[25] => 410
[26] => 041
[27] => 014
[28] => 140
[29] => 104
[30] => 451
[31] => 415
[32] => 541
[33] => 514
[34] => 145
[35] => 154
[36] => 956
[37] => 965
[38] => 596
[39] => 569
[40] => 695
[41] => 659
[42] => 901
[43] => 910
[44] => 091
[45] => 019
[46] => 190
[47] => 109
)
)
[505] => Array
(
[all] => Array
(
[0] => 505
[1] => 550
[2] => 055
[3] => 050
[4] => 005
[5] => 500
[6] => 555
[7] => 000
)
)
)
)
);
To understand how I am setting up the array, this is the code I’ve created
foreach($play_array as $k=>$v){
foreach($v['minus'] as $key=>$val){
$all = file_get_contents("get-nums.php?math=" . $val);
$t_all = explode(", ", $all);
foreach($t_all as $tall){
unset($play_array[$k]['minus'][$key]);
$play_array[$k]['minus'][$val]['all'][] = $tall;
}
}
}
All is well and good up to this point. This is the part where I’m having trouble and can’t seem to quite get it right. I’m looping through the $play_array to get the minus keys which I want to display. Then I want to loop through the $play_array again and find the values that are within that that match the minus key or it’s values within the [minus][XX][all] area.
This is the present code for that. And while it’s close, it’s not quite right.
foreach($play_array as $k=>$v){
$min_array = $play_array[$k]['minus'];
foreach($min_array as $mk => $mv){
$all = $min_array[$mk]['all'];
$check = array_intersect_key(array_flip($all), $play_array);
if(!empty($check)){
echo $mk . ": ";
foreach($check as $ck=>$cv){
if($ck != $mk){
echo $ck . ', ';
}
}
}
if(!empty($check)){
echo "<br>";
}
}
}
Below is my current output, and then my desired output.
Current:
007: 070,
750: 070,
622: 717, 771, 762,
721: 762, 771, 717,
170: 107, 751,
071: 107, 751,
Desired:
007: 070, 750
622: 717, 771, 762, 712
170: 107, 751, 071
I think I’m missing something about using unset() to remove the keys so they don’t repeat, but I am not quite sure where. Any helps is greatly appreciated. I’ve been trying for hours with multiple approaches to get this right, and this is the closest I keep coming. Thanks!
2
Answers
In the last loop, I simply need to unset the key based on the array_flip
It seems like the issue is related to the way you’re handling the $play_array during the second loop. Instead of manipulating the original array directly, you can create a separate result array to store the desired output. Here’s an adjusted version of your code:
In this adjusted code, I’ve introduced a $result array to store the desired output. Instead of manipulating the original $play_array, the results are appended to this new array. The final loop is used to output the desired result based on the modified structure.
This should provide you with the desired output. If you encounter any issues or have further questions, feel free to ask!