1

I have an array like this:

$r = array();
$r[] = ['name' => 'Test', 'supplierId' => 34];
$r[] = ['name' => 'Test2', 'supplierId' => 31];
$r[] = ['name' => 'Test3', 'supplierId' => 32];
$r[] = ['name' => 'Test4', 'supplierId' => 34];
$r[] = ['name' => 'Test5', 'supplierId' => 30];
$r[] = ['name' => 'Test6', 'supplierId' => 32];

Now I want to take $r and get multiple arrays back, differenced by supplierId. So I am looking for this result:

    $r30 = ['name' => 'Test5', 'supplierId' => 30];
    $r32 = [
    ['name' => 'Test3', 'supplierId' => 32],
    ['name' => 'Test6', 'supplierId' => 32]
    ];

I tried it with, but here I do not have access to $sup in array_filter.

$supplier = array(30, 31, 32, 34);
$finalArray = [];

foreach ($supplier as $sup) {
    $finalArray[] = array_filter($r, function($value, $sup) {
        echo $sup;
        if ($value['supplierId'] == $sup) {
            return true;
        }
    });
}//foreach

Any idea how I can solve it? Is there no native function that accomplishes this - something like create_array_based_on('supplierId');?

Thanks

2
  • Would $r30 = [['name' => 'Test5', 'supplierId' => 30]]; be suitable? Commented Mar 21, 2017 at 14:12
  • Yep, it would be @AlexBlex Commented Mar 21, 2017 at 14:23

5 Answers 5

2

You can pass $sup to your anonymous function:

foreach ($supplier as $sup) {
    $finalArray[] = array_filter($r, function($value) use ($sup) {
                                                      ^^^^^^^^^^ you need to pass
                                                                 it like this
        echo $sup;
        if ($value['supplierId'] == $sup) {
            return true;
        }
    });
}//foreach

But personally I would probably just loop over the original array and use the supplier ID as a key.

Something like:

$results = [];
foreach ($r as $value) {
    $results[$value['supplierId']][] = $value;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try this out

$r = array();
$r[] = ['name' => 'Test', 'supplierId' => 34];
$r[] = ['name' => 'Test2', 'supplierId' => 31];
$r[] = ['name' => 'Test3', 'supplierId' => 32];
$r[] = ['name' => 'Test4', 'supplierId' => 34];
$r[] = ['name' => 'Test5', 'supplierId' => 30];
$r[] = ['name' => 'Test6', 'supplierId' => 32];

$supplier = array(30, 31, 32, 34);
$finalArray = [];


$i=0;
foreach ($supplier as $sup) {
    $value =$r[$i]['supplierId'];
    if($value==$sup)
    {
        $finalArray[] = $value;
    }

 $i++;

}//foreach

$finalArray is a new array with all values you need

Comments

0

You can combine array_filter and in_array as following:

$finalArray[] = array_filter($r, function($value) use ($supplier) {
    return in_array($value['supplierId'], $supplier);
});

Without foreach loop.

Comments

0

It would be much easier, instead of wanting new variables for each of them, like $r32, $r35, etc... if you just used the array keys to store the supplierid, e.g.

$r = array();
$r[] = ['name' => 'Test', 'supplierId' => 34];
$r[] = ['name' => 'Test2', 'supplierId' => 31];
$r[] = ['name' => 'Test3', 'supplierId' => 32];
$r[] = ['name' => 'Test4', 'supplierId' => 34];
$r[] = ['name' => 'Test5', 'supplierId' => 30];
$r[] = ['name' => 'Test6', 'supplierId' => 32];

$newArray = array();

foreach($r as $key => $element)
{
    if (!isset($newArray[$element['supplierId']])){
        $newArray[$element['supplierId']] = array();
    }
    $newArray[$element['supplierId']][] = $element;
}

That would give you an output of:

<pre>Array
(
    [34] => Array
        (
            [0] => Array
                (
                    [name] => Test
                    [supplierId] => 34
                )

            [1] => Array
                (
                    [name] => Test4
                    [supplierId] => 34
                )

        )

    [31] => Array
        (
            [0] => Array
                (
                    [name] => Test2
                    [supplierId] => 31
                )

        )

    [32] => Array
        (
            [0] => Array
                (
                    [name] => Test3
                    [supplierId] => 32
                )

            [1] => Array
                (
                    [name] => Test6
                    [supplierId] => 32
                )

        )

    [30] => Array
        (
            [0] => Array
                (
                    [name] => Test5
                    [supplierId] => 30
                )

        )

)
</pre>

Comments

0

It's about simple grouping nested arrays by some key value:

$finalArr = [];
foreach ($r as $arr) {
    $finalArr[$arr['supplierId']][] = $arr;
}

print_r($finalArr);

DEMO link

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.