坚持使用ARRAY解决方案

时间:2017-08-03 21:37:20

标签: php multidimensional-array

因为我是这个平台上的新手,所以我想问一下对你们所有人来说都很容易的事情。

我需要在日期内显示加入,所以日期不会重复,如果他们有类似的,他们会加入一个数组。

$newarray = array();
$i = 0;
$j = 0;
$fecha = $jsonOrdenado[0]['fecha_inicio']

foreach($jsonOrdenado as $a) {
    $newarray[$i]["fecha"] = $a['fecha_inicio'];
    $newarray[$j]["eventos"] = $a;

    if ($a['fecha_inicio'] == $a['fecha_inicio']) {
        $newarray[$i] = ($data[$i], $data[$a]);
        $i++;
    } else {
        $j++;
    }
}

1 个答案:

答案 0 :(得分:0)

$newarray = array();

//this will combine the events by date
foreach($jsonOrdenado as $a) {
  $fecha = $a['fecha_inicio'];
  $newarray[$fecha]["eventos"] = $a;
}

//see what it looks like
print_r($newarray);

如果有重复的事件,请使用他们的名字或任何有关他们的独特信息

foreach($jsonOrdenado as $a) {
  $fecha = $a['fecha_inicio'];
  //I don't know what your data looks like :)
  $eventname = $a['event_name_or_title_or_whatever']; 
  $newarray[$fecha]["eventos"][$eventname] = $a;
}

//see what it looks like
print_r($newarray);

如果您想稍后简化格式......

foreach($newarray as $fecha => $whatever){ //only the key matters
    $newarray[$fecha]['eventos'] = array_values($newarray[$fecha]['eventos']);
}

//see what it looks like
print_r($newarray);