0

在遍历这些数组时遇到了一些麻烦,我在每个数组 fstransid、前缀、nr 和日期中都有 4 个相同的属性,其余的“名称”、“fsa_code”、“fsa_name”、“debits_acc”和“credits_acc”我想要在以下 5 个数组中提取这些 1->5

解决这个问题的最佳方法是什么?

array(18) {
  [0]=>
  array(9) {
    ["fstransid"]=>
    string(4) "4019"
    ["prefix"]=>
    string(3) "PSF"
    ["nr"]=>
    string(3) "854"
    ["date"]=>
    string(10) "2012-06-05"
    ["name"]=>
    string(33) "MY NAME"
    ["fsa_code"]=>
    string(4) "1681"
    ["fsa_name"]=>
    string(17) "asdfasdfasdf"
    ["debits_acc"]=>
    string(6) "546.94"
    ["credits_acc"]=>
    string(4) "0.00"
  }
  [1]=>
  array(9) {
    ["fstransid"]=>
    string(4) "4019"
    ["prefix"]=>
    string(3) "PSF"
    ["nr"]=>
    string(3) "854"
    ["date"]=>
    string(10) "2012-06-05"
    ["name"]=>
    string(33) "MY NAME"
    ["fsa_code"]=>
    string(4) "2621"
    ["fsa_name"]=>
    string(8) "asafafasdfsas"
    ["debits_acc"]=>
    string(4) "0.00"
    ["credits_acc"]=>
    string(5) "60.75"
  }
  [2]=>
  array(9) {
    ["fstransid"]=>
    string(4) "4019"
    ["prefix"]=>
    string(3) "PSF"
    ["nr"]=>
    string(3) "854"
    ["date"]=>
    string(10) "2012-06-05"
    ["name"]=>
    string(33) "MY NAME"
    ["fsa_code"]=>
    string(4) "3540"
    ["fsa_name"]=>
    string(5) "sfdsdfsfssssssssssss"
    ["debits_acc"]=>
    string(4) "0.00"
    ["credits_acc"]=>
    string(5) "60.71"
  }
  [3]=>
  array(9) {
    ["fstransid"]=>
    string(4) "4019"
    ["prefix"]=>
    string(3) "PSF"
    ["nr"]=>
    string(3) "854"
    ["date"]=>
    string(10) "2012-06-05"
    ["name"]=>
    string(33) "MY NAME"
    ["fsa_code"]=>
    string(4) "6060"
    ["fsa_name"]=>
    string(27) "kostnader"
    ["debits_acc"]=>
    string(4) "9.00"
    ["credits_acc"]=>
    string(4) "0.00"
  }
  [4]=>
  array(9) {
    ["fstransid"]=>
    string(4) "4019"
    ["prefix"]=>
    string(3) "PSF"
    ["nr"]=>
    string(3) "854"
    ["date"]=>
    string(10) "2012-06-05"
    ["name"]=>
    string(33) "MY NAME"
    ["fsa_code"]=>
    string(4) "6064"
    ["fsa_name"]=>
    string(12) "asdfasdfasdsdssdssds"
    ["debits_acc"]=>
    string(5) "11.06"
    ["credits_acc"]=>
    string(4) "0.00"
  }
  [5]=>
  array(9) {
    ["fstransid"]=>
    string(4) "4019"
    ["prefix"]=>
    string(3) "PSF"
    ["nr"]=>
    string(3) "854"
    ["date"]=>
    string(10) "2012-06-05"
    ["name"]=>
    string(33) "MY NAME"
    ["fsa_code"]=>
    string(4) "3002"
    ["fsa_name"]=>
    string(35) "assssdsdsdsd"
    ["debits_acc"]=>
    string(4) "0.00"
    ["credits_acc"]=>
    string(6) "445.54"
  }
4

3 回答 3

2

试试这个

//$array is your array

//Array with the identical values
$identical_arr = array_slice($array[0], 0, 4);

//Array of repeated values in arrays
foreach ($array as $inner_array) {
  $differ_arr[] = array_slice($inner_array, 4);
}
于 2012-10-09T14:38:25.523 回答
0

您可以使用 foreach,例如:

foreach($multi_array 作为 $single_array){

     foreach($single_array as $key => $value){
          echo $key.":".$value;
     }

}
于 2012-10-09T14:38:02.940 回答
0
$result = array();
foreach ($arrays as $array) {
   if (!isset($prev_array)) {
      $prev_array = $array;
   }
   else {
      $result = array_intersect_assoc($prev_array, $array);
   }
}

如果您将所有数组放入$array,这将遍历它们并根据键/值找到交集(即它将找到键和值相同的数组之间的所有元素)。

于 2012-10-09T14:40:06.433 回答