-1

I have been fighting with this all day and would like another set of eyes to maybe give me some insight. I'm not sure I am even approaching this the correct way. I have two arrays, namely a multidimensional array and a single array, I am trying to add up the contents of a multidimensional array whose values ​​are taken from a single array, where the contents of the multidimensional array are the same as the array keys of a single array. I'm building an app with Laravel.

$multidimensionArray = array:6 [▼
  "A2" => array:3 [▼
    0 => "A1"
    1 => "A2"
    2 => "A3"
  ]
  "A1" => array:4 [▼
    0 => "A1"
    1 => "A6"
    2 => "A5"
    3 => "A2"
  ]
  "A6" => array:1 [▼
    0 => "A3"
  ]
  "A3" => array:2 [▼
    0 => "A3"
    1 => "A2"
  ]
  "A5" => array:1 [▼
    0 => "A4"
  ]
  "A4" => array:2 [▼
    0 => "A4"
    1 => "A3"
  ]
]

$singeArray = array:6 [▼
  "A1" => 675.58333333333
  "A2" => 1786.75
  "A3" => 2897.9166666667
  "A4" => 4009.0833333333
  "A5" => 5120.25
  "A6" => 6231.4166666667
]

how to add the value in $multidimensionalArray according to the key whose value is taken from $singleArray, then divided by the number of keys in $multidimensionalArray, for example: in key "A2" in multidimensionalArray has values ​​A1,A2,A3 and in singleArray the values ​​of A1,A2,A3 are 675.58333333333, 1786.75, 2897.9166666667, how to add them then divide by 3(according to the number of values ​​in A2).

I hope the result is like this

$result = array(
"A2" => 1786.75
"A1" => 3453.5
...etc

)
0

3 Answers 3

1

I would use a foreach and array_filter.

Since you want average of the values it can be done with array_sum.

This should work

$results = [];
foreach ($multidimensionArray as $key => $valueArr) {
    ## filter out all values of interest from $singleArray
    $filteredValues = array_filter($singeArray, function ($value, $identifier) use ($valueArr) {
        return in_array($identifier, $valueArr);
    }, ARRAY_FILTER_USE_BOTH);
    ## Here we get the average
    $results[$key] = array_sum($filteredValues) / count($filteredValues);
}
print_r($results);

Working sandbox link

Sign up to request clarification or add additional context in comments.

Comments

0

This should work for you:

$result = [];

array_walk($multidimensionArray, function($value, $key) use(&$result, $singeArray) {
   // get only those items from $singeArray which exist in $value
   // sum and divide by 3
   $result[ $key ]  = array_sum( array_intersect_key( $singeArray, array_flip($value) ) ) / 3;
});

var_dump( $result );

Edit: Updated my answer forgot to use array_flip

Comments

0

Assuming that the 2d array's row values are unique, the most efficient way to filter the data is to filter the 1d array by a flipped version of the targeted row's data -- this will be much less expensive that making iterated in_array() calls.

Code: (Demo)

$result = [];
foreach ($multi as $key => $row) {
    $values = array_intersect_key($single, array_flip($row));
    $result[$key] = array_sum($values) / count($values);
}
var_export($result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.