Skip to main content
added 1477 characters in body
Source Link
gnarf
  • 106.6k
  • 26
  • 129
  • 161

Something like this might workTested and working:

Code: <?php

$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[$row['c_cat_id']]$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[$row['c_cat_id']]['pages'][]$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"
      }
    }
  }
}

Something like this might work:

$result = array();

foreach ($original as $row) {
  if (!isset($result[$row['c_cat_id']]) {
    $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[$row['c_cat_id']]['pages'][] = $row;
}

var_dump($result);

Tested and working:

Code: <?php

$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"
      }
    }
  }
}
Source Link
gnarf
  • 106.6k
  • 26
  • 129
  • 161

Something like this might work:

$result = array();

foreach ($original as $row) {
  if (!isset($result[$row['c_cat_id']]) {
    $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[$row['c_cat_id']]['pages'][] = $row;
}

var_dump($result);