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
                    )

            )

    )
)
Konstantinos Natsios
  • 2,874
  • 9
  • 39
  • 74
  • Very similar to https://stackoverflow.com/questions/12706359/how-to-group-subarrays-by-a-column-value, you could just add a `if(array_key_exists($group['group_name'], $result)...` – Djave Jul 08 '20 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. [ask] – CBroe Jul 08 '20 at 12:52

1 Answers1

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"] ]
        ];
    }
}
Djave
  • 8,595
  • 8
  • 70
  • 124
  • 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 – Konstantinos Natsios Jul 08 '20 at 13:04
  • 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 – Djave Jul 08 '20 at 13:21
  • all prints are ok now. – Konstantinos Natsios Jul 08 '20 at 13:39