0

Does any on know how can I convert this

$events = array(
array("type" => "off-site", "title" => "aaa", "nid" => "11"),
array("type" => "off-site", "title" => "bbb", "nid" => "22"),
array("type" => "installation", "title" => "ccc", "nid" => "33"),
array("type" => "opening", "title" => "ddd", "nid" => "44"),
array("type" => "opening", "title" => "eee", "nid" => "55"),
array("type" => "opening", "title" => "fff", "nid" => "66")
);

into this

$events_processed = array(
"off-site" => array(
        array(
            "title" => "aaa",
            "nid" => "11"
        ),
        array(
            "title" => "bbb",
            "nid" => "22"
        )
),
"installation" => array(
        array(
            "title" => "ccc",
            "nid" => "33"
        )
),
"opening" => array(
        array(
            "title" => "ddd",
            "nid" => "44"
        ),
        array(
            "title" => "eee",
            "nid" => "55"
        ),
        array(
            "title" => "fff",
            "nid" => "66"
        )
)
);

using php?

I've already tried to apply different methods from different posts here but with no success. I need the array to be nested so I can reorder the array by "type".Hi

1
  • have you tried using array_walk()? or just looping through the array would likely be easier Commented Mar 20, 2012 at 17:25

6 Answers 6

1

You can the code below, please note that your current output is not valid because you missed the array index ...

Example 1

    $events_processed = array();
    foreach($events as $options)
    {
        $events_processed[$options['type']][] = array("title"=>$options['title'],"nid"=>$options['nid']);
    }
    var_dump($events_processed);

OR

Example 2 (@dleiftah suggestion)

    $defautKey = "type" ; 
    foreach($events as $options)
    {
        $type = $options[$defautKey] ;
        unset($options[$defautKey]);
        $events_processed[$type][] = $options;
    }
     var_dump($events_processed);

Both Result Would be Like this but number 2 is more flexible

        array
          'off-site' => 
            array
              0 => 
                array
                  'title' => string 'aaa' (length=3)
                  'nid' => string '11' (length=2)
              1 => 
                array
                  'title' => string 'bbb' (length=3)
                  'nid' => string '22' (length=2)
          'installation' => 
            array
              0 => 
                array
                  'title' => string 'ccc' (length=3)
                  'nid' => string '33' (length=2)
          'opening' => 
            array
              0 => 
                array
                  'title' => string 'ddd' (length=3)
                  'nid' => string '44' (length=2)
              1 => 
                array
                  'title' => string 'eee' (length=3)
                  'nid' => string '55' (length=2)
              2 => 
                array
                  'title' => string 'fff' (length=3)
                  'nid' => string '66' (length=2)
Sign up to request clarification or add additional context in comments.

2 Comments

However, if there is more information added to the event array, this code would have to be modified to keep it ...
i agree @dleiftah he needs to modify ... have updated the ansder for alternative
0

Just loop through and reorganize ...

$result = array();
foreach($events as $event){
    $key = $event['type'];
    unset($event['type']);
    $result[$key][] = $event;
}

print_r($result);

Comments

0

Here's what I did:

$array_processed = array();
foreach( $events as $evt){
    $array_processed[$evt['type']][] = array('title'=>$evt['title'], 'nid'=>$evt['nid']);
}

print_r($array_processed);

Comments

0

There is no built in function that will do this. You will have to loop through this array and make a new array.

$events=array(contents-of-array);
$proc_events=array(
    'off-site' => array(),
    'installation' => array(),
    'opening' => array()
);
foreach($events as $key => $value)
{
    switch($value['type'])
    {
        case 'off-site':
        $proc_events['offsite']=array('title' => $value['title'], 'nid' => $value['nid']);
        break;

        case 'installation':
        $proc_events['installation']=array('title' => $value['title'], 'nid' => $value['nid']);
        break;

        case 'opening':
        $proc_events['opening']=array('title' => $value['title'], 'nid' => $value['nid']);
        break;
    }
}

The above should work.

Comments

0

The shortest code to do this should be:

foreach ($events as $event)
    $events_processed[$event['type']][] = array('title' => $event['title'], 'nid' => $event['nid']);

Comments

0

Here is what I was able to do:

<?php
$events = array(
    array("type" => "off-site", "title" => "aaa", "nid" => "11"),
    array("type" => "off-site", "title" => "bbb", "nid" => "22"),
    array("type" => "installation", "title" => "ccc", "nid" => "33"),
    array("type" => "opening", "title" => "ddd", "nid" => "44"),
    array("type" => "opening", "title" => "eee", "nid" => "55"),
    array("type" => "opening", "title" => "fff", "nid" => "66")
);

foreach ($events as $event) {
    $events_processed[$event['type']][] = array('title' => $event['title'],
                                   'nid' => $event['nid']
    );
}
echo '<pre>';
print_r($events_processed);
?>

This prints the following:

Array
(
    [off-site] => Array
        (
            [0] => Array
                (
                    [title] => aaa
                    [nid] => 11
                )

            [1] => Array
                (
                    [title] => bbb
                    [nid] => 22
                )

        )

    [installation] => Array
        (
            [0] => Array
                (
                    [title] => ccc
                    [nid] => 33
                )

        )

    [opening] => Array
        (
            [0] => Array
                (
                    [title] => ddd
                    [nid] => 44
                )

            [1] => Array
                (
                    [title] => eee
                    [nid] => 55
                )

            [2] => Array
                (
                    [title] => fff
                    [nid] => 66
                )

        )

)

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.