PHP - Ordenar array por fecha descendente

 
Vista:
sin imagen de perfil
Val: 10
Ha aumentado su posición en 14 puestos en PHP (en relación al último mes)
Gráfica de PHP

Ordenar array por fecha descendente

Publicado por Félix (3 intervenciones) el 04/09/2020 02:59:00
Hola a todos,
Bien hallados.
Tengo la siguiente matriz (es mucho más grande) que quisiera ordenar por fecha descendente (la más nueva, la primera). Como veis no necesito que me devuelva todos los campos (solo título, autor, medio y fecha)
Lo cierto es que no tengo ni idea de cómo hacerlo.
Muchas gracias por leer mi consulta.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
 
$documentacion = array(
  array('id' => '1','orden' => '99','rutaFichero' => '2007AlonsoIran.pdf','autor' => 'Jesús Alonso Blanco','titulo' => 'Irán, la próxima guerra en Oriente Medio','medio' => 'Revista Ejército de tierra español nº797(septiembre 2007)','tipo' => '1','fecha' => '2007-09-01'),
  array('id' => '6','orden' => '99','rutaFichero' => '2008JMAPolmilitar.pdf','autor' => 'Jesús María Alemany','titulo' => 'Política militar','medio' => 'Heraldo de Aragón','tipo' => '1','fecha' => '2008-01-27'),
  array('id' => '7','orden' => '99','rutaFichero' => '2008JMAEncartelado.pdf','autor' => 'Jesús María Alemany','titulo' => 'El encartelado','medio' => 'Heraldo de Aragón','tipo' => '7','fecha' => '2008-03-09'),
  array('id' => '8','orden' => '99','rutaFichero' => '200804MCGascon.pdf','autor' => 'María Carmen Gascón','titulo' => 'Los medios no deben amputar nuestra capacidad','medio' => 'El Periódico de Aragón','tipo' => '8','fecha' => '2008-04-10'),
  array('id' => '9','orden' => '99','rutaFichero' => 'HAcolumna78.pdf','autor' => 'Jesús María Alemany','titulo' => 'Pluridimensional','medio' => 'Heraldo de Aragón','tipo' => '1','fecha' => '2008-06-01'),
  array('id' => '375','orden' => '0','rutaFichero' => NULL,'autor' => 'Alberto Gago','titulo' => '<a href=https://www.thetechnolawgist.com/2020/07/22/datos-un-recurso-todavia-sin-explotar-para-el-bien-comun/ target=_blank>Datos: un recurso todavía sin explotar para el bien común</a>','medio' => 'The Technolawgist','tipo' => '4','fecha' => '2020-07-22'),
  array('id' => '376','orden' => '0','rutaFichero' => 'HAcolumna386.pdf','autor' => 'Jesús María Alemany','titulo' => 'Diplomacia en las UCIs','medio' => 'Heraldo de Aragón','tipo' => '4','fecha' => '2020-07-26'),
  array('id' => '377','orden' => '0','rutaFichero' => 'HAcolumna387.pdf','autor' => 'Jesús María Alemany','titulo' => 'Vacaciones','medio' => 'Heraldo de Aragón','tipo' => '4','fecha' => '2020-08-09'),
  array('id' => '378','orden' => '0','rutaFichero' => 'HAcolumna388.pdf','autor' => 'Jesús María Alemany','titulo' => 'Ventanas abiertas','medio' => 'Heraldo de Aragón','tipo' => '4','fecha' => '2020-08-23')
);
 
 
$is_page_builder_used = et_pb_is_pagebuilder_used( get_the_ID() );
 
?>
 
<div id="main-content">
 
<?php if ( ! $is_page_builder_used ) : ?>
 
	<div class="container">
		<div id="content-area" class="clearfix">
			<div id="left-area">
 
<?php endif; ?>
 
			<?php while ( have_posts() ) : the_post(); ?>
 
				<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
 
				<?php if ( ! $is_page_builder_used ) : ?>
 
					<h1 class="entry-title main_title"><?php the_title(); ?></h1>
				<?php
					$thumb = '';
 
					$width = (int) apply_filters( 'et_pb_index_blog_image_width', 1080 );
 
					$height = (int) apply_filters( 'et_pb_index_blog_image_height', 675 );
					$classtext = 'et_featured_image';
					$titletext = get_the_title();
					$thumbnail = get_thumbnail( $width, $height, $classtext, $titletext, $titletext, false, 'Blogimage' );
					$thumb = $thumbnail["thumb"];
 
					if ( 'on' === et_get_option( 'divi_page_thumbnails', 'false' ) && '' !== $thumb )
						print_thumbnail( $thumb, $thumbnail["use_timthumb"], $titletext, $width, $height );
				?>
 
				<?php endif; ?>
 
					<div class="entry-content">
 
					<div class="et_pb_section et_pb_section_0 et_section_regular">
						<div class=" et_pb_row et_pb_row_0">
							<div class="et_pb_column et_pb_column_4_4  et_pb_column_0 et_pb_css_mix_blend_mode_passthrough et-last-child">
								<div class="et_pb_text et_pb_module et_pb_bg_layout_light et_pb_text_align_left  et_pb_text_0">
									<div class="et_pb_text_inner">
										<table id="documentacion">
										  	<thead>
										    	<tr>
										      		<th>Documento</th>
										      		<th>Autor</th>
										      		<th>Medio</th>
										      		<th>Fecha</th>
									    		</tr>
										  	</thead>
										  	<tbody>
										  		<?php
										  		foreach ($documentacion as $doc) {
										  			echo '<tr><td><a class="enlaceDocumento" target="_blank" href="/wp-content/uploads/'.$doc['rutaFichero'].'">'.$doc['titulo'].'</a></td><td class="autor">'.$doc['autor'].'</td><td class="medio">'.$doc['medio'].'</td><td class="fecha">'.$doc['fecha'].'</td></tr>';
										  		}
										  		?>
											</tbody>
										</table>
									</div>
								</div>
							</div>
						</div>
					</div>
 
						<?php
 
get_footer();
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 10
Ha aumentado su posición en 14 puestos en PHP (en relación al último mes)
Gráfica de PHP

Ordenar array por fecha descendente

Publicado por Félix (3 intervenciones) el 05/09/2020 08:14:10
¡Jo, muchas gracias Joel!

Pero como integraría la llamada a la función orderBy($documentacion, 'fecha', 'desc'); para que me lo presentara en

1
2
3
4
5
6
7
8
9
10
11
12
13
<table id="documentacion">
	<thead>
	    	<tr>
			<th>Documento</th>
			<th>Autor</th>
			<th>Medio</th>
			<th>Fecha</th>
		</tr>
	</thead>
  	<tbody>
 		<?php
	  		foreach ($documentacion as $doc) {
  			echo '<tr><td><a class="enlaceDocumento" target="_blank" href="/wp-content/uploads/'.$doc['rutaFichero'].'">'.$doc['titulo'].'</a></td><td class="autor">'.$doc['autor'].'</td><td class="medio">'.$doc['medio'].'</td><td class="fecha">'.$doc['fecha'].'</td></tr>';


Disculpa el abuso. Como ves ando bastante perdido
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de joel
Val: 3.828
Oro
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Ordenar array por fecha descendente

Publicado por joel (1269 intervenciones) el 05/09/2020 09:41:39
Me alegra que te sirva...

Yo creo que lo implementaría algo así:

1
2
3
$documentacionOrdenada=orderBy($documentacion, 'fecha', 'desc');
foreach ($documentacionOrdenada as $doc) {
...

De esta manera, estas recorriendo el array ordenado en vez del array desordenado.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
sin imagen de perfil
Val: 10
Ha aumentado su posición en 14 puestos en PHP (en relación al último mes)
Gráfica de PHP

Ordenar array por fecha descendente

Publicado por Félix (3 intervenciones) el 06/09/2020 20:53:28
Gracias :) Perdona mi retraso en la respuesta y agradecimiento, pero estaba intentando no dar más mal... Creo que introduzco función y llamada de forma muy deficiente:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
 
function orderBy($items, $attr, $order)
{
    $sortedItems = [];
    foreach ($items as $item) {
        $key = is_object($item) ? $item->{$attr} : $item[$attr];
        $sortedItems[$key] = $item;
    }
    if ($order === 'desc') {
        krsort($sortedItems);
    } else {
        ksort($sortedItems);
    }
 
    return array_values($sortedItems);
}
?>
 
<?php
 
$documentacion = array(
  array('id' => '1','orden' => '99','rutaFichero' => '2007AlonsoIran.pdf','autor' => 'Jesús Alonso Blanco','titulo' => 'Irán, la próxima guerra en Oriente Medio','medio' => 'Revista Ejército de tierra español nº797(septiembre 2007)','tipo' => '1','fecha' => '2007-09-01'),
  array('id' => '6','orden' => '99','rutaFichero' => '2008JMAPolmilitar.pdf','autor' => 'Jesús María Alemany','titulo' => 'Política militar','medio' => 'Heraldo de Aragón','tipo' => '1','fecha' => '2008-01-27'),
  array('id' => '7','orden' => '99','rutaFichero' => '2008JMAEncartelado.pdf','autor' => 'Jesús María Alemany','titulo' => 'El encartelado','medio' => 'Heraldo de Aragón','tipo' => '7','fecha' => '2008-03-09'),
  array('id' => '8','orden' => '99','rutaFichero' => '200804MCGascon.pdf','autor' => 'María Carmen Gascón','titulo' => 'Los medios no deben amputar nuestra capacidad','medio' => 'El Periódico de Aragón','tipo' => '8','fecha' => '2008-04-10'),
  array('id' => '9','orden' => '99','rutaFichero' => 'HAcolumna78.pdf','autor' => 'Jesús María Alemany','titulo' => 'Pluridimensional','medio' => 'Heraldo de Aragón','tipo' => '1','fecha' => '2008-06-01'),
  array('id' => '10','orden' => '99','rutaFichero' => '2008SGSanchez.pdf','autor' => 'Gervasio Sánchez','titulo' => 'Palabras de agradecimiento al Jurado del Premio Ortega y Gasset','medio' => 'mayo 2008','tipo' => '8','fecha' => '2008-05-01')
);
 
 
get_header();
 
$is_page_builder_used = et_pb_is_pagebuilder_used( get_the_ID() );
 
?>
 
<div id="main-content">
 
<?php if ( ! $is_page_builder_used ) : ?>
 
	<div class="container">
		<div id="content-area" class="clearfix">
			<div id="left-area">
 
<?php endif; ?>
 
			<?php while ( have_posts() ) : the_post(); ?>
 
				<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
 
				<?php if ( ! $is_page_builder_used ) : ?>
 
					<h1 class="entry-title main_title"><?php the_title(); ?></h1>
				<?php
					$thumb = '';
 
					$width = (int) apply_filters( 'et_pb_index_blog_image_width', 1080 );
 
					$height = (int) apply_filters( 'et_pb_index_blog_image_height', 675 );
					$classtext = 'et_featured_image';
					$titletext = get_the_title();
					$thumbnail = get_thumbnail( $width, $height, $classtext, $titletext, $titletext, false, 'Blogimage' );
					$thumb = $thumbnail["thumb"];
 
					if ( 'on' === et_get_option( 'divi_page_thumbnails', 'false' ) && '' !== $thumb )
						print_thumbnail( $thumb, $thumbnail["use_timthumb"], $titletext, $width, $height );
				?>
 
				<?php endif; ?>
 
					<div class="entry-content">
 
					<div class="et_pb_section et_pb_section_0 et_section_regular">
						<div class=" et_pb_row et_pb_row_0">
							<div class="et_pb_column et_pb_column_4_4  et_pb_column_0 et_pb_css_mix_blend_mode_passthrough et-last-child">
								<div class="et_pb_text et_pb_module et_pb_bg_layout_light et_pb_text_align_left  et_pb_text_0">
									<div class="et_pb_text_inner">
										<table id="documentacion">
										  	<thead>
										    	<tr>
										      		<th>Documento</th>
										      		<th>Autor</th>
										      		<th>Medio</th>
										      		<th>Fecha</th>
									    		</tr>
										  	</thead>
										  	<tbody>
										  		<?php
 
 
$documentacionOrdenada=orderBy($documentacion, 'fecha', 'desc');
										  		foreach ($documentacion as $doc) {
										  			echo '<tr><td><a class="enlaceDocumento" target="_blank" href="/wp-content/uploads/'.$doc['rutaFichero'].'">'.$doc['titulo'].'</a></td><td class="autor">'.$doc['autor'].'</td><td class="medio">'.$doc['medio'].'</td><td class="fecha">'.$doc['fecha'].'</td></tr>';
										  		}
										  		?>
											</tbody>
										</table>
									</div>
								</div>
							</div>
						</div>
					</div>
 
 
						<?php
 
						//the_content();
 
						if ( ! $is_page_builder_used )
							wp_link_pages( array( 'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'Divi' ), 'after' => '</div>' ) );
					?>
					</div> <!-- .entry-content -->
 
				<?php
					if ( ! $is_page_builder_used && comments_open() && 'on' === et_get_option( 'divi_show_pagescomments', 'false' ) ) comments_template( '', true );
				?>
 
				</article> <!-- .et_pb_post -->
 
			<?php endwhile; ?>
 
<?php if ( ! $is_page_builder_used ) : ?>
 
			</div> <!-- #left-area -->
 
			<?php get_sidebar(); ?>
		</div> <!-- #content-area -->
	</div> <!-- .container -->
 
<?php endif; ?>
 
</div> <!-- #main-content -->
 
<?php
 
get_footer();
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar