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

时间:2021-02-27 13:39:44

Having some trouble looping through these arrays , i have 4 properties that identical in each array fstransid, prefix, nr and date, the rest "name","fsa_code", "fsa_name", "debits_acc" and "credits_acc" i'd like to extract these in the following 5 arrays 1->5

循环遍历这些数组有一些麻烦,我有4个属性在每个数组fstransid,前缀,nr和日期相同,其余的“名称”,“fsa_code”,“fsa_name”,“debits_acc”和“credits_acc”我喜欢在以下5个数组1-> 5中提取这些数据

What's the best way to approach this?

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

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 个解决方案

#1


2  

Try this

//$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);
}

#2


0  

You can use foreach, example:

你可以使用foreach,例如:

foreach($multi_array as $single_array){

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

}

#3


0  

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

If you put all of your arrays in $array, this will loop through them and find the intersection based on key/values (i.e. it will find all elements between the arrays where the keys and values are the same).

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

#1


2  

Try this

//$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);
}

#2


0  

You can use foreach, example:

你可以使用foreach,例如:

foreach($multi_array as $single_array){

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

}

#3


0  

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

If you put all of your arrays in $array, this will loop through them and find the intersection based on key/values (i.e. it will find all elements between the arrays where the keys and values are the same).

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