7

I have an multidimensional array which includes IDs and URLs. I would like to output the URLs only.

$abstract_details = array(
                        array(
                            'id' => 68769782222,
                            'url' => 'http://yourclick.ch'
                        ),
                        array(
                            'id' => 111,
                            'url' => 'http://google.com'
                        )
                    );

foreach ($abstract_details as $abstract_detail) {
    foreach ($abstract_detail as $get_abstract_detail) {
        $result .= $get_abstract_detail . '<br>';
    }
}

When I run my code I get both information. How is it possible to take control of what I want to show?

Reza
  • 513
  • 3
  • 7
  • 20
  • 3
    To get the url: `foreach ($abstract_details as $abstract_detail) { echo $abstract_detail['url'] . '
    ';}`. Run it: [https://eval.in/605347](https://eval.in/605347)
    – FirstOne Jul 14 '16 at 13:13
  • 1
    Please read this : http://php.net/manual/en/language.types.array.php#language.types.array.syntax.accessing – sglessard Jul 14 '16 at 13:16
  • 1
    @FirstOne I don't even know why I thought that it needs to be complicated but that just works fine. Thank you ;) – Reza Jul 14 '16 at 13:21

3 Answers3

17

Use array_column that will prevent you from foreach loop

$url = array_column($abstract_details, 'url');

echo implode('<br/>', $url); 
Saty
  • 22,443
  • 7
  • 33
  • 51
2
foreach ($abstract_details as $abstract_detail) {
       $result .= $abstract_detail['url']
    }
dios231
  • 714
  • 1
  • 9
  • 21
2

You don't even need nested loop here, if you want to print only the url. Try this:

foreach ($abstract_details as $abstract_detail) {
   $result .= $abstract_detail['url'] . '<br>';
}

Output:

http://yourclick.ch
http://google.com
Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32