2

I want to split an array according to sub_id

my main array:

$my_main_array = [
    ['id' => 1, 'sub_id' => 8],
    ['id' => 2, 'sub_id' => 9],
    ['id' => 3, 'sub_id' => 8],
    ['id' => 3, 'sub_id' => 9],
    ['id' => 3, 'sub_id' => 9],
];

The result should be,The first array:

    sub_id equal 8
    sub_id equal 8

The second array

        sub_id equal 9
        sub_id equal 9
        sub_id equal 9
    
7
  • 3
    What is the actual array? What have you tried? Commented Mar 2, 2016 at 9:00
  • Use array_filter() with a callback that matches against your condition Commented Mar 2, 2016 at 9:02
  • Hello! It would be helpful to include the code for what you have tried so we can diagnose your issue. Otherwise, your answers you receive may be too generic for you to apply to your case. Commented Mar 2, 2016 at 9:03
  • $actual_ar = array( 'sub_id'=>8, 'sub_id'=>8, 'sub_id'=>9, 'sub_id'=>9, 'sub_id'=>9 ) Commented Mar 2, 2016 at 9:03
  • 1
    That array cannot exist, since you're using the same key for every item. Commented Mar 2, 2016 at 9:12

4 Answers 4

2

I am a bit late, but I was facing this problem today, so I thought I could let my solution here because maybe someone find it useful.

You can let this function created:

function array_split($original_array, $callback) {
    $return = [[],[]]; // initialize both arrays we will use as output

    foreach( $original_array as $item ) {
        // Add each item to the corresponding array, according to the callback output
        $return[$callback($item)][] = $item;
    }

    return $return;
}

and then use it like this:

[$sub_id_9_array, $sub_id_8_array] = array_split(
    $my_main_array, 
    function($item) { return $item['sub_id'] == 8;}
);
Sign up to request clarification or add additional context in comments.

1 Comment

I like the idea, but I don't like variable variables. Hope you wouldn't mind. it only reverses the output, false then true
1
$arrayA = [];
$arrayB = [];

array_map(function($value) use (&$arrayA, &$arrayB) {
    if ($value['sub_products_id'] == 8) {
        $arrayA[] = $value;
    } else {
        $arrayB[] = $value;
    }
}, $originalArray);

4 Comments

Whilst this code may be useful it would be much better if it had some explanation of what the code does, how it works and how it answers the question.
@AdrianHHH basically this code would produce output where $arrayA[] will be [8,8] and $arrayB[] will be [9,9,9].
I don't think you should use array_map. If you discard its return value (and don't even build it), it's completely useless. I juste ran some (very basic) benchmarks and I found that a foreach is at least twice as fast as this array_map usage (it builds and return an extra array with all values to null and adds costly function calls).
Also a foreach would be much clearer
0

Use array_filter() with a callback that matches against your condition

$key = 8;
$first = array_filter(
    $originalArray,
    function($value) use ($key) {
        return $value['sub_products_id'] == $key;
    }
);

$key = 9;
$second = array_filter(
    $originalArray,
    function($value) use ($key) {
        return $value['sub_products_id'] == $key;
    }
);

3 Comments

Iterating twice over the array is not what I'd call awesome.
Well apologies that an answer that works simply isn't awesome
Totally depends on the array size, this is probably fine in most cases
0
$inputArray = [
    [ 'id' => 1, 'sub_id' => 8 ],
    [ 'id' => 2, 'sub_id' => 9 ],
    [ 'id' => 3, 'sub_id' => 8 ],
    [ 'id' => 3, 'sub_id' => 9 ],
    [ 'id' => 3, 'sub_id' => 9 ],
];

[ 'a' => $arrayA, 'b' => $arrayB ] = array_reduce(
    $inputArray,
    static fn($carry, $item) => array_merge_recursive(
        $carry,
        [ $item['sub_id'] === 8 ? 'a' : 'b' => [ $item ] ]
    ),
    [ 'a' => [], 'b' => [] ]
);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.