Jump to content

[SOLVED] Compare and combine multidimensional arrays...head spinning...need help


Recommended Posts

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

        )

 

 

 

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?

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..

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
     }
  }
}

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.. ;)

 

...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.

...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

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!)

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);
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.