views:

79

answers:

2

Ok, not really sure how to do this. I have values that are being outputted from a SQL query like so:

$row[0] = array('lid' => 1, 'llayout' => 1, 'lposition' => 1, 'mid' => 1, 'mlayout' => 1, 'mposition' => 0);
$row[1] = array('lid' => 2, 'llayout' => 1, 'lposition' => 0, 'mid' => 2, 'mlayout' => 1, 'mposition' => 0);
$row[2] = array('lid' => 2, 'llayout' => 1, 'lposition' => 0, 'mid' => 3, 'mlayout' => 1, 'mposition' => 1);
$row[3] = array('lid' => 3, 'llayout' => 1, 'lposition' => 1, 'mid' => 4, 'mlayout' => 1, 'mposition' => 1);
$row[4] = array('lid' => 4, 'llayout' => 1, 'lposition' => 2, 'mid' => 5, 'mlayout' => 1, 'mposition' => 0);   

etc. etc.

Ok, so the best thing I can think of for this is to give lid and mid array keys and have it equal the mposition into an array within the while loop of query like so...

$old[$row['lid']][$row['mid']] = $mposition;

Now if I do this, I need to compare this array's keys with another array that I'll need to build based on a $_POST array[].

$new = array();
foreach($_POST as $id => $data)
{
   // $id = column, but we still need to get each rows position...
   $id = str_replace('col_', '', $id);
   // now get the inner array...
   foreach($data as $pos => $idpos)
       $new[$id][$idpos] = $pos;
}

Ok, so now I have 2 arrays of info, 1 from the database ($old), and another from the $_POST positions ($new), I hope I got the array keys correct.

Now I need to figure out which one's changed, comparing from the old to the new. And also, need to update the database with all of the new positions where new lid = the old lid, and the new mid = the old mid from each array. I'm sure I'll have to use array_key or array_key_intersect somehow, but not sure exactly how...???

Also, I don't think an UPDATE would be useful in a foreach loop, perhaps there's a way to do a CASE statement in the UPDATE query?

Also, Am I going about this the right way? OR should I do it another way instead of using a muli-dimensional array for this.

Basically I need to update in the database where each array within the same keys = changed value and I need to update it where mid = mid in the database (the second key value).

Also, I think it would kinda be like this:

$newest = array();

        foreach($old as $c => $d)
        {
            foreach($d as $e => $f)
                $newest[$c][$e] = $new[$c][$e];
        }

But isn't there a better way to do this with 1 or 2 php array functions? And I still also need to know which values have changed.... arggg

A: 

I'm editing this entire answer, since my last try was completely unhelpful.

I notice this:


foreach($old as $c => $d)
  {
    foreach($d as $e => $f)
      $newest[$c][$e] = $new[$c][$e];
  }

And I feel compelled to suggest giving more thought to your variable names. Highly descriptive variable names help for understanding your own code, and especially help for other people to understand your code.

Now, to try to tackle your question again...

I don't use array_diff_assoc() a lot, but I believe you would use it like this:

$temp_changed=array();
foreach($new as $id => $mid){
    $temp_changed = array_diff_assoc($mid, $old[$id]);
    if(count($temp_changed)){
        $changed[$id] = $temp_changed;
    }
    unset($temp_changed);
}

The array_diff_assoc checks keys and values, and in the above example returns an array of all key/value pairs in $new[$id] that are not in $old[$id]

So you will end up with the array $changed that has the same structure as $new and $old

Then you can just:

foreach($changed as $id=>$mid){
  // Do UPDATE
}

As for the way you cycle through multi-dimentional arrays with nested foreach statements, if there is a better way I don't know about it.

Syntax Error
That's what I'm doing: $old[$row['lid']][$row['mid']] = $mposition;will be equal to $new[$id][$idpos] = $pos; But the values will be different.How do I use array_diff_assoc() for this exactly?
SoLoGHoST
Also, is there a quicker way to do this:$new = array(); foreach($currLayout as $c => $d) { foreach($d as $e => $f) $new[$c][$e] = $newLayout[$c][$e]; }I think this is what I want to do, because I also want to get the values of all of them that haven't changed I guess it would be easier to UPDATE in the database that way.
SoLoGHoST
Ok, my last answer sucked so I completely edited and rewrote it. I hope this one is more helpful
Syntax Error
A: 

Ok, thanks guys for all of your help, really really appreciate it!! Though I found a better way, as it does it way faster than array_diff_assoc. This is the function that I found, and after having both arrays, I just use $updatedArray = array_diff_no_cast($new, $old);

function array_diff_no_cast(&$ar1, &$ar2) {
   $diff = Array();
   foreach ($ar1 as $key => $val1) {
      if (array_search($val1, $ar2) === false) {
         $diff[$key] = $val1;
      }
   }
   return $diff;
}

Cheers :)

SoLoGHoST