1

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 is not ok with this } } 

gives me this:

Page[0][0]: http://www.example.com/impressum Email[0][1]: [email protected] Email[0][2]: [email protected] Page[1][0]: http://www.example.com/termsofuse Email[1][1]: [email protected] Email[1][2]: [email protected] Email[1][3]: [email protected] Email[1][4]: [email protected] Page[2][0]: http://www.example.com/adpolicy Email[2][1]: [email protected] Email[2][2]: [email protected] Email[2][3]: [email protected] Email[2][4]: [email protected] 

How can I transform it to get this result:

[email protected] http://www.example.com/impressum [email protected] http://www.example.com/impressum http://www.example.com/termsofuse http://www.example.com/adpolicy [email protected] http://www.example.com/termsofuse [email protected] http://www.example.com/termsofuse [email protected] http://www.example.com/termsofuse http://www.example.com/adpolicy [email protected] http://www.example.com/adpolicy 
$big_array = [ [ 'http://www.example.com/impressum', '[email protected]', '[email protected]', ], [ 'http://www.example.com/termsofuse', '[email protected]', '[email protected]', '[email protected]', '[email protected]', ], [ 'http://www.example.com/adpolicy', '[email protected]', '[email protected]', '[email protected]', '[email protected]', ] ]; 
4
  • Can you var_export() the original array please? Commented Jun 14, 2015 at 6:28
  • Sure, take a look at it Commented Jun 14, 2015 at 6:31
  • the relationship between input and output is not clear Commented Jun 14, 2015 at 6:33
  • The input array lists Email addresses per URL - the required output lists URLs per Email. Correct? Commented Jun 14, 2015 at 6:36

2 Answers 2

0
$array = array ( 0 => array ( 0 => 'http://www.example.com/impressum', 1 => '[email protected]', 2 => '[email protected]', ), 1 => array ( 0 => 'http://www.example.com/termsofuse', 1 => '[email protected]', 2 => '[email protected]', 3 => '[email protected]', 4 => '[email protected]', ), 2 => array ( 0 => 'http://www.example.com/adpolicy', 1 => '[email protected]', 2 => '[email protected]', 3 => '[email protected]', 4 => '[email protected]', ), ); print_r($array); $final = array(); foreach ( $array as $group ) { for ( $i=1; $i<count($group); $i++ ) { $final[$group[$i]][] = $group[0]; } } print_r($final); 

Here is the PHP Playground result.

To format it like your example:

foreach ( $final as $email => $links ) { echo $email . "\n"; foreach ( $links as $link ) { echo " " . $link . "\n"; } } 
Sign up to request clarification or add additional context in comments.

3 Comments

Cool! I managed to get the same result by fixing my line as $demo[$big_array[$i][$j]][] = $big_array[$i][0];. Any ideas on how to print out the result in a way I showed in my question?
Like echo it out with spaces in your question? It looks like you have some random spaces in your answer. Format it exactly like you want it to be shown.
But the last two links in your example only have one space before it. (I'm pretty sure this isn't intended.) I updated my answer with how to format it.
0

It seems to me that all that needs to be done is to separate row elements by a newline and two spaces, and then separate rows by a newline.

Code: (Demo)

echo implode( "\n", array_map( fn($v) => implode("\n ", $v), $array ) ); 

Output

http://www.example.com/impressum [email protected] [email protected] http://www.example.com/termsofuse [email protected] [email protected] [email protected] [email protected] http://www.example.com/adpolicy [email protected] [email protected] [email protected] [email protected] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.