Transform a multidimensional array in php -
this loop
$demo = array(); for($i=0;$i<count($big_array);$i++){ echo 'page['.$i.'][0]: '.$big_array[$i][0].'<br>'; for($j=1;$j<count($big_array[$i]);$j++){ echo 'email['.$i.']['.$j.']: '.$big_array[$i][$j].'<br>'; $demo[$big_array[$i][$j]][] = $big_array[$i][$j-1]; //something not ok } }
gives me this:
page[0][0]: http://www.example.com/impressum email[0][1]: sales@example.com email[0][2]: support@example.com page[1][0]: http://www.example.com/termsofuse email[1][1]: support@example.com email[1][2]: terms1@example.com email[1][3]: terms2@example.com email[1][4]: ad2@example.com page[2][0]: http://www.example.com/adpolicy email[2][1]: support@example.com email[2][2]: ad1@example.com email[2][3]: ad2@example.com email[2][4]: ad1@example.com
how can transform result:
sales@example.com http://www.example.com/impressum support@example.com http://www.example.com/impressum http://www.example.com/termsofuse http://www.example.com/adpolicy terms1@example.com http://www.example.com/termsofuse terms2@example.com http://www.example.com/termsofuse ad2@example.com http://www.example.com/termsofuse http://www.example.com/adpolicy ad1@example.com http://www.example.com/adpolicy
var_dump($big_array):
array ( 0 => array ( 0 => 'http://www.example.com/impressum', 1 => 'sales@example.com', 2 => 'support@example.com', ), 1 => array ( 0 => 'http://www.example.com/termsofuse', 1 => 'support@example.com', 2 => 'terms1@example.com', 3 => 'terms2@example.com', 4 => 'ad2@example.com', ), 2 => array ( 0 => 'http://www.example.com/adpolicy', 1 => 'support@example.com', 2 => 'ad1@example.com', 3 => 'ad2@example.com', 4 => 'ad1@example.com', ), )
$array = array ( 0 => array ( 0 => 'http://www.example.com/impressum', 1 => 'sales@example.com', 2 => 'support@example.com', ), 1 => array ( 0 => 'http://www.example.com/termsofuse', 1 => 'support@example.com', 2 => 'terms1@example.com', 3 => 'terms2@example.com', 4 => 'ad2@example.com', ), 2 => array ( 0 => 'http://www.example.com/adpolicy', 1 => 'support@example.com', 2 => 'ad1@example.com', 3 => 'ad2@example.com', 4 => 'ad1@example.com', ), ); print_r($array); $final = array(); foreach ( $array $group ) { ( $i=1; $i<count($group); $i++ ) { $final[$group[$i]][] = $group[0]; } } print_r($final);
here php playground result.
to format example:
foreach ( $final $email => $links ) { echo $email . "\n"; foreach ( $links $link ) { echo " " . $link . "\n"; } }
Comments
Post a Comment