PDA

View Full Version : Foreach annidati: soluzione?


robertino_salemi
24-10-2014, 10:54
Salve a tutti,
posto qui perchč č pių una domanda di PHP che di Wordpress:

Ho il seguente codice:

//PAGINE DEI GRUPPI
foreach ( $menu as $mkey => $m ) {
$key = array_search( 'edit.php?post_type=gpages', $m );
if ( $key )
unset( $menu[$mkey] );
}
foreach ( $menu as $mkey => $m ) {
$key = array_search( 'post-new.php?post_type=page', $m );
if ( $key )
unset( $menu[$mkey] );
}


Il seguente codice fa un foreach su $menu e cerca la voce "x" ed eventualmente la rimuove.

Se volessi unire i due foreach come potrei fare?

Grazie.

Don[ITA]
24-10-2014, 11:18
foreach ( $menu as $mkey => $m ) {
if(array_search('edit.php?post_type=gpages', $m) || array_search('post-new.php?post_type=page', $m)) unset($menu[$mkey]);
}

wingman87
24-10-2014, 11:21
Aggiungo solo per robertino_salemi che quei foreach non sono annidati, sono annidati se sono uno dentro l'altro.

robertino_salemi
24-10-2014, 18:03
Ho risolto cosė grazie alla segnalazione di un utente:

<?php
//PAGINE DEI GRUPPI
$to_search = array('edit.php?post_type=gpages', 'post-new.php?post_type=page');
foreach ( $menu as $mkey => $m ) {
foreach($to_search as $find) {
$key = array_search( $find, $m );
if ( $key )
unset( $menu[$mkey] );
}
}
?>


Grazie.