datafan Posted January 15, 2008 Share Posted January 15, 2008 I have written tested and rewritten so many times, I am not even going to post my attemps at the code because they just don't work. I have 2 Multidimensional Arrays (MDA). I need to compare some values in MDA $a to MDA $b and if they match overwrite the values in $a with the values in $b...whew... I hope someone can understand this. To make it more complicated, if $a[0][0] matches say $b[0][0] (or some $b[][]) I then need to compare $a[0][0][1] with $b[0][0][1] and then overright the $a[0] with the entire $b[0]. Ok now to make it suck even more there can be multiple enties with the same value as $b[0][0][1] that need to stay in the order which they are already sorted while also being added to $a without messing up the original $b that we just added...anyone ready to kill me? Perhaps a explanation and actual array layout will help. I start out with all classrooms "Available" for every day of the month. So later in the script when I build my calendar, all rooms show as available every day, unless they are reserved. Here is a portion of the "Available" array, let's call it $a: Array //Array $a ( [0] => Array ( [0] => 1199088000 //epoch date [1] => c1 //classroom name [2] => [3] => [4] => Available //shows on calendar if room isn't reserved [5] => [6] => [7] => ) [1] => Array ( [0] => 1199088000 [1] => c2 [2] => [3] => [4] => Available [5] => [6] => [7] => ) [2] => Array ( [0] => 1199088000 [1] => c3 [2] => [3] => [4] => Available [5] => [6] => [7] => ) [3] => Array ( [0] => 1199088000 [1] => c4 [2] => [3] => [4] => Available [5] => [6] => [7] => ) And here is a portion of the reserved array, let's call it $b. What I was trying to explain was someone may reserve say c1 (classroom 1) for 5 hours and someone else may reserve it the same day for 2 hours following. As in my example here $b[0] and $b[1] reservations need to be written to $a while removing the $a[0] "Available" entry. Array //Array $b ( [0] => Array ( [0] => 1199088000 //epoch date [1] => c1 //classroom name [2] => 0800 //class start time [3] => 1500 //class end time [4] => 01-21~01-22c1800-300.txt //reservation file name [5] => no_wall_moves //wall move key [6] => tippet //unique password to delete reservation [7] => Jim's Class //class name ) [1] => Array ( [0] => 1199088000 [1] => c1 [2] => 1500 [3] => 1700 [4] => 01-21~01-22c1300-500.txt [5] => no_wall_moves [6] => something [7] => Jim's Second Class ) [2] => Array ( [0] => 1200902400 [1] => c2 [2] => 0800 [3] => 1700 [4] => 01-21~01-22c2c3800-500.txt [5] => no_wall_moves [6] => graphoidea [7] => Jim's Class ) [3] => Array ( [0] => 1201075200 [1] => c2 [2] => 0800 [3] => 1000 [4] => 01-21~01-22c2c3800-500.txt [5] => wall_move_entry [6] => graphoidea [7] => Jim's Class ) Quote Link to comment https://forums.phpfreaks.com/topic/86037-solved-compare-and-combine-multidimensional-arrayshead-spinningneed-help/ Share on other sites More sharing options...
teng84 Posted January 15, 2008 Share Posted January 15, 2008 you can have loop then do it this way for($x = 0; $y<=count($yourarraraorb);$x++){ for($y = 0;$y<=count($yourarraraorb[$x];$y++)){ //comparenow $yourarrara[$x ][$y] = $yourarrara[$x ][$y] } } something like that those that make sense? Quote Link to comment https://forums.phpfreaks.com/topic/86037-solved-compare-and-combine-multidimensional-arrayshead-spinningneed-help/#findComment-439376 Share on other sites More sharing options...
teng84 Posted January 15, 2008 Share Posted January 15, 2008 you can have loop then do it this way for($x = 0; $x<=count($yourarraraorb);$x++){ for($y = 0;$y<=count($yourarraraorb[$x];$y++)){ //comparenow $yourarrara[$x ][$y] = $yourarrara[$x ][$y] } } something like that those that make sense? sorry edited.. Quote Link to comment https://forums.phpfreaks.com/topic/86037-solved-compare-and-combine-multidimensional-arrayshead-spinningneed-help/#findComment-439378 Share on other sites More sharing options...
awpti Posted January 15, 2008 Share Posted January 15, 2008 Slight performance improvement: for($x = 0, $array_a_count = count($array_a); $x < $array_a_count; ++$x){ for($y = 0, $array_b_count = count($array_b); $y < $array_b_count < $y; ++$y)){ // compare if($array_a[$x][$y] === $array_b[$x][$y]) { //do something } } } This will stop it from running count() on each loop. You could also just do this: $array_a_count = count($array_a); $array_b_count = count($array_b); for($x = 0; $x < $array_a_count; ++$x){ for($y = 0; $y < $array_b_count < $y; ++$y)){ // compare if($array_a[$x][$y] === $array_b[$x][$y]) { //do something } } } Quote Link to comment https://forums.phpfreaks.com/topic/86037-solved-compare-and-combine-multidimensional-arrayshead-spinningneed-help/#findComment-439399 Share on other sites More sharing options...
teng84 Posted January 15, 2008 Share Posted January 15, 2008 nice catch hmm i should give a complete code not just an idea sorry i just hate coding everything its there part to improve my ideas. and it feels good when you're part of that code and not just running it without modification that is why most of the code i posted are dummies i give them some puzzle to solve.. Quote Link to comment https://forums.phpfreaks.com/topic/86037-solved-compare-and-combine-multidimensional-arrayshead-spinningneed-help/#findComment-439417 Share on other sites More sharing options...
Barand Posted January 15, 2008 Share Posted January 15, 2008 ...that is why most of the code i posted are dummies... It has been noticed amongst the mods here. Often we weren't sure whether you were deliberately trying to confuse the poster or just didn't have a clue what you were talking about. Either way, it's not very helpful. Now we know. Proceed with caution. Quote Link to comment https://forums.phpfreaks.com/topic/86037-solved-compare-and-combine-multidimensional-arrayshead-spinningneed-help/#findComment-439429 Share on other sites More sharing options...
teng84 Posted January 15, 2008 Share Posted January 15, 2008 ...that is why most of the code i posted are dummies... It has been noticed amongst the mods here. Often we weren't sure whether you were deliberately trying to confuse the poster or just didn't have a clue what you were talking about. Either way, it's not very helpful. Now we know. Proceed with caution. sorry man.. from now on ill try to post better but this Now we know. Proceed with caution. ??? it has triple meaning for me ouch .. again sorry Quote Link to comment https://forums.phpfreaks.com/topic/86037-solved-compare-and-combine-multidimensional-arrayshead-spinningneed-help/#findComment-439431 Share on other sites More sharing options...
awpti Posted January 15, 2008 Share Posted January 15, 2008 nice catch hmm i should give a complete code not just an idea sorry i just hate coding everything its there part to improve my ideas. and it feels good when you're part of that code and not just running it without modification that is why most of the code i posted are dummies i give them some puzzle to solve.. Using functions in a for() loop is a very very very very common mistake. I'm still guilty of doing it sometimes, just because it's an old habit that I'm trying to break. And definately, give good code examples. My code examples isn't tested, but i'm reasonably sure it'll work (the concept is correct, the execution may not be completely - YMMV!) Quote Link to comment https://forums.phpfreaks.com/topic/86037-solved-compare-and-combine-multidimensional-arrayshead-spinningneed-help/#findComment-439464 Share on other sites More sharing options...
datafan Posted January 15, 2008 Author Share Posted January 15, 2008 Thanks, you folks always make it look so easy. I had up to half a page of code in some of my attemps I'll run with you're suggestions. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/86037-solved-compare-and-combine-multidimensional-arrayshead-spinningneed-help/#findComment-439513 Share on other sites More sharing options...
teng84 Posted January 15, 2008 Share Posted January 15, 2008 yeah but barand get me ??? Quote Link to comment https://forums.phpfreaks.com/topic/86037-solved-compare-and-combine-multidimensional-arrayshead-spinningneed-help/#findComment-439516 Share on other sites More sharing options...
datafan Posted January 15, 2008 Author Share Posted January 15, 2008 yeah but barand get me ??? Things don't always come across the way they should on the internet. Sometimes misunderstood. Quote Link to comment https://forums.phpfreaks.com/topic/86037-solved-compare-and-combine-multidimensional-arrayshead-spinningneed-help/#findComment-439533 Share on other sites More sharing options...
datafan Posted January 16, 2008 Author Share Posted January 16, 2008 Here's what ended up working for me. I take away (unset) the duplicate values from the edates array, and then merge the arrays. I later sort the array with a neat function someone helped me with on this site. <?php foreach ($lines as $key => $value) { foreach ($edates as $key2 => $value2) { if ($value[0] == $value2[0] && $value[1] == $value2[1]) { unset ($edates[$key2]); } } } //now merge the arrays $edates = array_merge($lines, $edates); ?> Quote Link to comment https://forums.phpfreaks.com/topic/86037-solved-compare-and-combine-multidimensional-arrayshead-spinningneed-help/#findComment-441242 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.