循环通过数组提取不同于每个数组的值

时间:2012-10-09 14:24:26

标签: php arrays extract

在循环遍历这些数组时遇到一些问题,我在每个数组中有4个相同的属性 fstransid,前缀,nr和日期,其余的“名称”,“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"
  }

3 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:0)

您可以使用foreach,例如:

foreach($multi_array as $single_array){

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

}

答案 2 :(得分:0)

$result = array();
foreach ($arrays as $array) {
   if (!isset($prev_array)) {
      $prev_array = $array;
   }
   else {
      $result = array_intersect_assoc($prev_array, $array);
   }
}

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