質問

I have this array

Array
    (
        [0] => Array
            (

                [id] => book
                [name] => Array
                    (
                        [0] => author
                        [1] => title
                        [2] => genre
                        [3] => price
                        [4] => publish_date
                        [5] => description
                    )

            )

        [1] => Array
            (
                [id] => book
                [name] => Array
                    (
                        [0] => author
                        [1] => title
                        [2] => genre
                        [3] => price
                        [4] => publish_date
                        [5] => description
                    )

            )

    )
    Array
    (
        [0] => Array
            (
                [id] => library
                [name] => Array
                    (
                        [0] => room
                        [1] => room
                    )

            )

        [1] => Array
            (
                [id] => library
                [name] => Array
                    (
                        [0] => book
                        [1] => book
                        [2] => book
                        [3] => book
                   )   
            )  
    )

And i want my output look like

Array
    (
        [0] => Array
            (
                [id] => library
                [name] => Array
                     (
                        [0] => room
                        [1] => room
                    )
                     (
                        [0] => book
                        [1] => book
                        [2] => book
                        [3] => book
                    )

            )

    )
    Array
    (
    [0] => Array
    (

                [id] => book
                [name] => Array
                    (
                        [0] => author
                        [1] => title
                        [2] => genre
                        [3] => price
                        [4] => publish_date
                        [5] => description
                    )

            )
    }

I am trying to do that compare all id and name if it have same id so unique it get all the value [name] together if it have same name and and same id unique them Thank so much I am trying to do that but it only return with only unique id and it has warning : Undefined index: name

if (is_array($servico))
{
    foreach($servico as $data)
    {                              
        $result[$data['id']][] =  ($data['name']);                             
    }
}
役に立ちましたか?

解決

You need to check whether the new element is already in the array before adding it, so that you only get unique elements. And to prevent that warning about the undefined index, you need to initialize the element if it doesn't exist yet.

foreach ($servico as $data) {
    if (!isset($result[$data['id']])) {
        $result[$data['id']] = array();
    }
    if (!in_array($data['name'], $result[$data['id']])) {
        $result[$data['id']][] = $data['name'];
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top