0

I have the following array

Array
(
[0] => Array
    (
        [id_product_option] => 1
        [id_option] => 1
        [id_product] => 3
        [option_value] => White
        [option_name] => color

    )

[1] => Array
    (
        [id_product_option] => 2
        [id_option] => 2
        [id_product] => 3
        [option_value] => 9oz
        [option_name] => size

    )

[2] => Array
    (
        [id_product_option] => 3
        [id_option] => 1
        [id_product] => 3
        [option_value] => Blue
        [option_name] => color

    )

)

What i need to do is loop through it and find the ones where the id_option values match and group them into a new array that should look like

Array
(
[0] => Array
    [0] => Array
        (
            [id_product_option] => 1
            [id_option] => 1
            [id_product] => 3
            [option_value] => White
            [additional_cost] => 0
            [is_active] => 1
            [created_on] => 2014-11-15 01:29:35
            [option_name] => color
            [option_text] => Color
        )

    [1] => Array
        (
            [id_product_option] => 3
            [id_option] => 1
            [id_product] => 3
            [option_value] => Blue
            [additional_cost] => 0
            [is_active] => 1
            [created_on] => 2014-11-15 01:29:35
            [option_name] => color
            [option_text] => Color
        )

[1] => Array
    (
        [id_product_option] => 2
        [id_option] => 2
        [id_product] => 3
        [option_value] => 9oz
        [additional_cost] => 0
        [is_active] => 1
        [created_on] => 2014-11-15 01:29:35
        [option_name] => size
        [option_text] => Size
    )


)

where the options with id_option 1 are grouped together

I tried the following but no luck

    $groupOptions = array();
    $prev = "";
    foreach($productOptions as $key=>$options) {
        $id_option = $options['id_option'];

        if($id_option != $prev) {
            $groupOptions[] = $productOptions[$key];
        }

        $prev = $id_option;
    }
3
  • Can you share what you have tried? Commented Nov 19, 2014 at 18:34
  • 1
    Why are you marking this down i dont understand. Better if you just dont say anything. This place is meant to help people not just think a question is stupid and mark it down Commented Nov 19, 2014 at 18:37
  • I don't know who down-voted your question, but it was probably before you had shared what you had tried. Commented Nov 19, 2014 at 18:39

2 Answers 2

1

You should use that id_option as the key in your new array, otherwise you're stuck having to hunt through the new array to find where the matching items are, which you're ALREADY doing in the first loop

$newarray = array();
foreach($oldarray as $item) {
   $newarray[$item['id_option']][] = $item;
}
Sign up to request clarification or add additional context in comments.

Comments

0

I have tested with you example and seems to work fine :

$notFactored; # you should provide here your input array
$factored = array();
foreach($notFactored as $nf) {
    $found = FALSE;
    foreach($factored as &$f) { # passed by address !
        if(!empty($f[0]) && $nf['id_option'] == $f[0]['id_option']) {
            $f[] = $nf;
            $found = TRUE;
            break;
        }
    }
    if(!$found) {
        $factored[count($factored)][] = $nf;
    }
}
print 'my factored array : ' . print_r($factored);

Hope that Helps :)

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.