-2

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
            )
        )
    )
)
3
  • 1
    This is very difficult to read. Commented Apr 20, 2010 at 20:02
  • With another formatting it would be easier to compare ;) Commented Apr 20, 2010 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. Commented Apr 20, 2010 at 20:10

3 Answers 3

1

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"
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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;
  }
}

1 Comment

Yeah sorry if I wasn't that clear but this is exactly what I want.
0
  • As you iterate each row of the input array, split the data into two arrays -- the parent level data and the child level data. array_chunk() is concise and suitable for this asked question, but other scenarios may require different techniques.

  • Check if the grouping identifier has been encountered before. If not, create a new reference variable, fill it with the required array, then push it into the result array. If the identifier has been encountered already, just push the child data as a new subarray into the appropriate reference array.

  • Using references this way avoids the need to reindex the result array after looping

Code: (Demo)

$result = [];
foreach ($array as $row) {
    [$parent, $child] = array_chunk($row, 2, true);
    if (!isset($ref[$parent['c_cat_id']])) {
        $ref[$parent['c_cat_id']] = $parent + ['pages' => [$child]];
        $result[] =& $ref[$parent['c_cat_id']];
        continue;
    }
    $ref[$parent['c_cat_id']]['pages'][] = $child;
}

var_export($result);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.