Php - How to print this multidimensional array?

Suppose I have the following array:

Array ( [1284487200] => Array ( [title] => first title [link] => http%3A%2F%2Fexample1.com ) [1261271380] => Array ( [title] => second title [link] => http%3A%2F%2Fexample2.com ) 

I want to print an array as follows:

 <a href="http://example1.com">first title - 1284487200</a><br> <a href="http://example2.com">second title - 1261271380</a><br> 

Any ideas would be greatly appreciated!

UPDATE: the two answers provided have the same error that my domain is included in the link, for example. http://www.mydomain.com/http%3A%2F%example1.com

Anyway, can I fix this?

+2
source share
3 answers

the (most) difficult part is getting the key of the first array of dimensions, this is using:

 $key=>$data 

in foreach.

$ key represents the key to the passed array. $ data contents, in your example $ data contains a second dimensional array.

 foreach( $array AS $key=>$data ) { echo '<a href="'.urldecode( $data['link'] ).'">'.$data['title'].' - '.$key.'</a><br>'; } 

Additional information on the foreach instruction: http://php.net/manual/en/control-structures.foreach.php

+3
source
 foreach ($array as $key => $entry) { echo "<a href=\"{$entry['link']}\">{$entry['title']} - {$key}</a><br>"; } 

See: http://php.net/manual/en/control-structures.foreach.php

+3
source

Use a foreach , $key will be a number, and other information will be in $value as an array. I assume that you will want to decode the link using urldecode() :

 foreach($arr_name as $key => $value){ $link = urldecode($value['link']); echo '<a href="' . $link . '">' . $key . ' - ' . $value['title'] . '</a><br>'; } 
+1
source

Source: https://habr.com/ru/post/1436944/


All Articles