Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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

share|improve this question

1 Answer 1

up vote 11 down vote accepted

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

$arr = array();
foreach($pageitems as $items) {
    $arr[$items['zone']][] = $items['text'];
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.