0

I'm trying to convert this array of objects

Array
(
    [0] => stdClass Object
    (
        [group_name] => 1
        [filter_names] => Array
            (
                [0] => a
            )
    )

    [1] => stdClass Object
    (
        [group_name] => 1
        [filter_names] => Array
            (
                [0] => b
            )
    )
)

to make the objects above merged as one object depending on group_name

and the filter_names to be one array. Ex.

Array
(
    [0] => stdClass Object
    (
        [group_name] => 1
        [filter_names] => Array
            (
                [0] => a,
                [1] = >b
            )
    )
)

Is there any efficient way that we can achieve the above?

A proposed solution from @Djave

        $filter_results = [];
        foreach ($filter_array as $element) {

            // If there is already a group with the same name,
            // just add the element's filter name

            if( array_key_exists($element->group_name, $filter_results)){
                $result[$element->group_name]["filter_names"][] = $element->filter_names;
            }else{

            // Otherwise, create a new record in the result array,
            // with an array wrapping the single filter_name

                $filter_results[] = [
                  "group_name" => $element->group_name,
                  "filter_names" => [ $element->filter_names ]
                ];
            }
        }

Is giving me the following result

Array
(
    [0] => Array
    (
        [group_name] => 1
        [filter_names] => Array
            (
                [0] => Array
                    (
                        [0] => a
                    )

            )

    )

    [1] => Array
    (
        [group_name] => 1
        [filter_names] => Array
            (
                [0] => Array
                    (
                        [0] => b
                    )

            )

    )
)
2
  • Very similar to stackoverflow.com/questions/12706359/…, you could just add a if(array_key_exists($group['group_name'], $result)... Commented Jul 8, 2020 at 12:41
  • 3
    “Is there any efficient way that we can achieve the above?” - please do not ask like this, without at least presenting your own attempt first. Otherwise, this boils down to a code-writing request, and that is not what this site is for in the first place. How to Ask Commented Jul 8, 2020 at 12:52

1 Answer 1

1

You could do something like this:

$result = [];
foreach ($data as $element) {

    // If there is already a group with the same name,
    // just add the element's filter name

    if( array_key_exists($element['group_name'], $result)){
        $result[$element['group_name']]["filter_names"][] = $element["filter_names"];
    }else{

    // Otherwise, create a new record in the result array,
    // with an array wrapping the single filter_name

        $result[] = [
          "group_name" => $element['group_name'],
          "filter_names" => [ $element["filter_names"] ]
        ];
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

With the answer above I get some errors that Uncaught Error: Cannot use object of type stdClass as array in and it means this $element['group_name']. Its an object what lives inside the array. Although even if I change the array to object, still the result is not what expected. See updated question where I included the result of your example
You have tagged the question PHP, but have asked it with JSON data, it's hard to work out what you mean. It would probably be more useful if you used a print_r of your array of data, not json_encode in the question
all prints are ok now.

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.