0

I need to group rows in a multidimensional array by the value of a sub-array which is not consistently found in the same column.

$array = [
    147 => [4, 'req'],
    199 => ['5', 'opt'],
    212 => [2, 5, 'req']
];

It needs to be split into these arrays.

Array 1:

[
    147 => [4, 'req'],
    212 => [2, 5, 'req'],
]

Array 2:

[
    199 => ['5', 'opt'],
]
  

I know of array_filter(), but can´t figure out the function inside array_filter() that gives me the desired result.

I tried

$req = array_filter($my_array, function ($v, $k) {
    return $v == 'req';
}, ARRAY_FILTER_USE_BOTH);

I also tried

function filter_my_array($my_array, $search_term) {
  $new_array = array();
  foreach ($my_array as $subarray) {
    if (in_array($search_term, $subarray)) {
       $new_array[] = $subarray;
    }
  }
  return $new_array; 
} 
         
$req = filter_my_array($array, 'req');

Both approaches do not work.

6
  • 1
    array_filter does not split anything. It filters. Commented Oct 24, 2018 at 9:08
  • Are the values different each time or are they the same? Commented Oct 24, 2018 at 9:09
  • Your function works fine: 3v4l.org/dRo69 Commented Oct 24, 2018 at 9:10
  • Always "req" or "opt". Commented Oct 24, 2018 at 9:10
  • 1
    Updated version: 3v4l.org/ai5lH Commented Oct 24, 2018 at 9:14

3 Answers 3

4

You can create two arrays with the help of filtering by necessary values. You can use array_filter function to it, but you should find a necessary value in each element that passed to array_filter function.

For example, finding 'req' value

$req = array_filter($my_array, function ($v) {
    return in_array('req', $v);
});

For example, finding 'opt' value

$opt = array_filter($my_array, function ($v) {
    return in_array('opt', $v);
});

I used in_array function to finding values because each element is an array that has different quantity of elements

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

Comments

0

This seems to work for me:

function filter_my_array($my_array, $search_term) {
    $required_array = [];
    $filtered_array = [];
    foreach ($my_array as $key => $subarray) {
        $found_flag = false;
        foreach ($subarray as $cur_subarray) {
            if ($cur_subarray == $search_term) {
                $found_flag = true;
                break;
            }
        }

        if ($found_flag == true) {
            $required_array[$key] = $subarray;
        } else {
            $filtered_array[$key] = $subarray;
        }
    }

    return [$required_array, $filtered_array];
}

To retrieve the required array and the filtered array, call the function as follows:

list($required_array, $filtered_array) = filter_my_array($my_array, 'req');

where $my_array holds the array in your example.

Comments

0

While it is possible to populate variably named grouping arrays, it is not best practice. Demo

$array = [
    147 => [4, 'req'],
    199 => ['5', 'opt'],
    212 => [2, 5, 'req']
];

foreach ($array as $key => $row) {
    ${$row[array_key_last($row)]}[$key] = $row;
}
var_export($req);
var_export($opt);

Your IDE is going to struggle to realize that when these variables are accessed, these variables actually exist.


Instead, it would be more professional to deepen the array structure and use dynamic first level keys for grouping. Demo

$result = [];
foreach ($array as $key => $row) {
    $result[$row[array_key_last($row)]][$key] = $row;
}
var_export($result);

Regardless of which way you prefer, you should not use more than one loop or call an iterating function (array_filter()) more than once.

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.