简体   繁体   English

数组,需要通过键排序

[英]Array, need to sort via Keys

Ok, not really sure how to do this. 好的,不太确定如何执行此操作。 I have values that are being outputted from a SQL query like so: 我有从SQL查询输出的值,如下所示:

$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... 好的,所以我能想到的最好的办法是提供lid和mid数组键,并使它等于查询的while循环内的数组位置,就像这样...

$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[]. 现在,如果要执行此操作,则需要将该数组的键与另一个需要基于$ _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. 好的,所以现在我有2个信息数组,其中1个来自数据库($ old),另一个来自$ _POST位置($ new),我希望我能得到正确的数组键。

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. 并且,还需要使用所有新位置更新数据库,其中新的lid =旧的盖子,新的mid =每个数组中的旧的mid。 I'm sure I'll have to use array_key or array_key_intersect somehow, but not sure exactly how...??? 我确定我必须以某种方式使用array_key或array_key_intersect,但不确定具体如何...?

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? 另外,我认为在foreach循环中UPDATE不会有用,也许有一种方法可以在UPDATE查询中执行CASE语句?

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). 基本上,我需要在数据库中更新相同键内的每个数组=更改后的值,并且需要在数据库中mid = mid(第二个键值)处进行更新。

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? 但是没有更好的方法用1或2个php数组函数来做到这一点吗? And I still also need to know which values have changed.... arggg 而且我还需要知道哪些值已更改。...arggg

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: 我不经常使用array_diff_assoc(),但我相信您会这样使用它:

$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] array_diff_assoc检查键和值,在上面的示例中,返回$ new [$ id]中所有不在$ old [$ id]中的所有键/值对的数组

So you will end up with the array $changed that has the same structure as $new and $old 因此,您将最终获得与$ new和$ old具有相同结构的数组$ changed

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. 至于使用嵌套的foreach语句遍历多维数组的方式,是否有更好的方法我不知道。

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. 尽管我找到了一种更好的方法,但是它比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); 这是我找到的函数,在拥有两个数组之后,我只使用$ 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 :) 干杯:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM