Array Problem, need to sort via Keys

Posted by sologhost on Stack Overflow See other posts from Stack Overflow or by sologhost
Published on 2010-03-31T00:55:30Z Indexed on 2010/03/31 1:13 UTC
Read the original article Hit count: 399

Filed under:
|
|
|

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, and another from the $_POST positions, 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.

© Stack Overflow or respective owner

Related posts about php

Related posts about arrays