1

I have this situation:

A db table 'pageitems' with

zone | text
------------
ZONE1  text1
ZONE2  text2
ZONE3  text3
ZONE3  text4

and inside a foreach loop like this

foreach($pageitems as $items) {
...
}

I want to obtain an array like this

Array
 (
    [ZONE1] => Array(
        [0] => text1
    )

    [ZONE2] => Array
    (
        [0] => text2
    )

    [ZONE3] => Array
    (
        [0] => text3,
        [1] => text4
    )

 )

How to obtain this? Thanks

1 Answer 1

11

Assuming you don't need that comma after "text3"

$arr = array();
foreach($pageitems as $items) {
    $arr[$items['zone']][] = $items['text'];
}
Sign up to request clarification or add additional context in comments.

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.