up vote 0 down vote favorite
1

So I'm working on a website with Doctrine as ORM and I get the following array back as a result:

Array (
    [0] => Array (
        [c_cat_id] => 1
        [c_title] => Programas e projetos
        [p_menu] => PBA BR 163
        [p_page_id] => 1
    )
    [1] => Array (
        [c_cat_id] => 1
        [c_title] => Programas e projetos
        [p_menu] => Outros projetos
        [p_page_id] => 3
    )
) 

Is it possible to transform this array (in PHP) to something like this:

Array (
    [0] => Array (
        [c_cat_id] => 1
        [c_title] => Programas e projetos
        [pages] => Array (
            [0] => Array (
                [p_page_id] => 1
                [p_menu] => PBA BR 163
            )
            [1] => Array (
                [p_page_id] => 3
                [p_menu] => Outros projetos
            )
        )
    )
)

Thanks for your help, always eager to learn new ways of doing things and that's why I love StackOverflow ;)

link|flag

1  
This is very difficult to read. – Fletcher Moore Apr 20 at 20:02
With another formatting it would be easier to compare ;) – Felix Kling Apr 20 at 20:02
@Samir: I would not change the code such that it makes sense. Formatting and so is fine but don't add stuff because of guess. If something is wrong, the OP has to correct it. – Felix Kling Apr 20 at 20:10

2 Answers

up vote 1 down vote accepted

Tested and working:

Code:

$original = array(
  array(
    "c_cat_id" => "1",
    "c_title" => "Programas e projetos",
    "p_menu" => "PBA BR 163",
    "p_page_id" => "1"),
  array(
    "c_cat_id" => "1",
    "c_title" => "Programas e projetos",
    "p_menu" => "Outros projetos",
    "p_page_id" => "3"),
  array(
    "c_cat_id" => "2",
    "c_title" => "Another Cat",
    "p_menu" => "Outros projetos",
    "p_page_id" => "4"),
);
$result = array();

foreach ($original as $row) {
  $cat = $row['c_cat_id'];
  if (!isset($result[$cat])) {
    $result[$row['c_cat_id']] = array(
      'c_cat_id'=>$row['c_cat_id'],
      'c_title'=>$row['c_title'],
      'pages'=>array(),
    );
  }
  unset($row['c_cat_id'],$row['c_title']);
  $result[$cat]['pages'][] = $row;
}

var_dump($result);

Result:

array(2) {
  [1]=>
  array(3) {
    ["c_cat_id"]=>
    string(1) "1"
    ["c_title"]=>
    string(20) "Programas e projetos"
    ["pages"]=>
    array(2) {
      [0]=>
      array(2) {
        ["p_menu"]=>
        string(10) "PBA BR 163"
        ["p_page_id"]=>
        string(1) "1"
      }
      [1]=>
      array(2) {
        ["p_menu"]=>
        string(15) "Outros projetos"
        ["p_page_id"]=>
        string(1) "3"
      }
    }
  }
  [2]=>
  array(3) {
    ["c_cat_id"]=>
    string(1) "2"
    ["c_title"]=>
    string(11) "Another Cat"
    ["pages"]=>
    array(1) {
      [0]=>
      array(2) {
        ["p_menu"]=>
        string(15) "Outros projetos"
        ["p_page_id"]=>
        string(1) "4"
      }
    }
  }
}
link|flag
Works like a charm, thank's a lot ;) – Fverswijver Apr 20 at 20:18
Way cooler than mine :-/ – Fletcher Moore Apr 20 at 20:20
up vote 0 down vote

It looks like you want to take an Array of pages and turn it into an array of categories with each containing an array of pages.

$inputArray = array(...); // whatever you have originally
$catArray = array();
foreach($inputArray as $page) {
  addToCatArray($page);
}

function addToCatArray($page) {
  $found = false;
  foreach($catArray as $cat) {
    if ($cat['c_cat_id'] == $page['c_cat_id'] {
      $newPage = array('p_page_id' => $page['p_page_id'], 'p_menu' => $page['p_menu']);
      $cat['pages'][] = $newPage;
      $found = true;
      break;
    }
  }
  if (!$found) { // create a new category
    $newCat = array('c_cat_id' => $page['c_cat_id'], 'c_title' => $page['c_title']);
    $newPage = array('p_page_id' => $page['p_page_id'], 'p_menu' => $page['p_menu']);
    $newCat['pages'] = array($newPage);
    $catArray[] = $newCat;
  }
}
link|flag
Yeah sorry if I wasn't that clear but this is exactly what I want. – Fverswijver Apr 20 at 20:08

Your Answer

 
or
never shown

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