0

I am having issue to display data from array, how can i retrieve data from bellow array?

 Array
    (
        [downloads] => Array
            (
                [0] => Array
                    (
                        [url] => http://samplelink.com
                        [release_notes] => Array
                            (
                                [0] => lorem ispum
                                [1] => lorem ispum.
                                [2] => lorem ispum.
                            )

                    )

                [1] => Array
                    (
                        [url] => http://samplelink.com
                        [release_notes] => Array
                            (
                                [0] => lorem ispum.
                                [1] => lorem ispum.
                                [2] => lorem ispum.
                                [3] => lorem ispum.
                                [4] => lorem ispum.
                            )

                    )

            )

        [result_code] => OK
    )

i have tried this, but did't gave exact answer, and how can i put sequence no. in result data for every data?

foreach ($data as $innerArray) {
            if (is_array($innerArray)){
                foreach ($innerArray as $value) {
                    echo '<p>'.$value['url'].'</p>';
                    echo '<p>'.$value['release_notes'].'</p>';
                }
            } 
        }
Abhee
  • 425
  • 1
  • 5
  • 17

3 Answers3

1

You can try this

foreach ($data['downloads'] as $downloads) {

    echo '<p>'.$downloads['url'].'</p>';

    if (is_array($downloads['release_notes'])) {
        foreach ($downloads['release_notes'] as $release_note) {
            echo '<p>'.$release_note.'</p>';
        }
    }

}
Oleksandr Pobuta
  • 407
  • 5
  • 11
1

This is a little crude but hope it helps. I found this neat tool in google and you can copy/paste the code below there to see how it works. http://phptester.net/

$parentArray = [
    'downloads' => [
        [
            'url' => 'http://www.google.com/',
            'release_notes' => [
                    'lorem ipsum',
                    'lorem ipsum',
                    'lorem ipsum'
            ]
        ],
        [
            'url' => 'http://www.google.com/',
            'release_notes' => [
                    'lorem ipsum',
                    'lorem ipsum',
                    'lorem ipsum'
            ]
        ]       
    ],
    'result_code' => 'OK'
];

// Loop Through Downloads
foreach($parentArray['downloads'] as $download) {

    // Parse Through Single Download
    foreach($download as $key => $item) {

        echo $key . ': ';

        if(is_array($item)) {
            echo '<br />';

            echo '<ul>';

            foreach($item as $key => $item) {

                echo '<li>' . $item . '</li>';

            }

            echo '</ul>';

        }

        else {
            echo $item;
        }


    }

    echo '<hr />';

}

It outputs this:

Output

1

If your array can have more or less than 3 levels of arrays (undefined three level), you need to use recursivity.

function RecurSearch($MyArray)
{
    foreach ($MyArray AS $key => $SubElement)
    {
        if ($key === "url")
        {
            echo "<p>" . $SubElement . "</p>";
        }
        else if ($key === "release_notes")
        {
            foreach($SubElement AS $ReleaseNote)
            {
                echo "<p>" . $ReleaseNote . "</p>";
            }
        }
        else if (is_array($SubElement))
        {
            RecurSearch($SubElement);
        }
    }
}

Output :

http://samplelink.com

lorem ipsum

lorem ipsum.

lorem ipsum.

http://samplelink.com

lorem ipsum.

lorem ipsum.

lorem ipsum.

lorem ipsum.

lorem ipsum.

Cid
  • 14,968
  • 4
  • 30
  • 45