1

I am trying to echo some values within two arrays . I have a variable called: $listDigitalDownloads

When I var_dump($listDigitalDownloads);

I get:

array(1) {
  [0]=>
  array(4) {
    ["digital_downloads_title"]=>
    string(5) "Title"
    ["digital_downloads_image"]=>
    string(7) "url.com"
    ["digital_downloads_author"]=>
    string(6) "Author"
    ["digital_download_files"]=>
    array(3) {
      [0]=>
      array(2) {
        ["digital_download_file_type"]=>
        string(11) "File Type 1"
        ["digital_download_file"]=>
        string(7) "url.pdf"
      }
      [1]=>
      array(2) {
        ["digital_download_file_type"]=>
        string(11) "File Type 2"
        ["digital_download_file"]=>
        string(7) "url.pdf"
      }
      [2]=>
      array(2) {
        ["digital_download_file_type"]=>
        string(11) "File Type 3"
        ["digital_download_file"]=>
        string(7) "url.pdf"
      }
    }
  }
}

I am trying to echo ["digital_download_file_type"] and ["digital_download_file"] using a foreach loop.

But when I var_dump ($listDigitalDownloadFiles); I just receive:

array(1) {
  [0]=>
  string(22) "digital_download_files"
}

I have tried:

foreach($listDigitalDownloadZero as $listDigitalDownloadFile) {
    $type = $listDigitalDownloadFile['digital_download_file_type'];
    echo $type;
}

But do not get any results.

Any help would be great

Here is my full code:

$listDigitalDownloads = get_field('digital_downloads_incentives'); 
echo '<pre>';
var_dump($listDigitalDownloads);
echo '</pre>';

foreach($listDigitalDownloads as $listDigitalDownload) {
    $title = $listDigitalDownload['digital_downloads_title'];
    $author = $listDigitalDownload['digital_downloads_author'];
    $image = $listDigitalDownload['digital_downloads_image'];
    $listDigitalDownloadFiles = ['digital_download_files'];
    echo '<pre>';
    var_dump ($listDigitalDownloadFiles);
    echo '</pre>';
    foreach($listDigitalDownloadZero as $listDigitalDownloadFile) {
        $type = $listDigitalDownloadFile['digital_download_file_type'];
        echo $type;
    }
}

I expect the output to echo the value of ['digital_download_file_type']

6
  • Include your array PHP format Commented Apr 30, 2019 at 17:53
  • I added echo '<pre>'; - which is how I formatted that var_dump. Is that what you mean? Commented Apr 30, 2019 at 18:45
  • Post the PHP array which you have in your code Commented Apr 30, 2019 at 18:45
  • The field(s) are generated from Advanced Custom Fields Wordpress Plugin and that is all the code I have on that page. Commented Apr 30, 2019 at 18:55
  • 1
    where does $listDigitalDownloadZero come from? It's undefined. Don't you want to do: foreach($listDigitalDownloadFiles as $listDigitalDownloadFile) { Commented Apr 30, 2019 at 18:55

1 Answer 1

1

You never assigned the variable $listDigitalDownloadZero. You need:

$listDigitalDownloadZero = $listDigitalDownload['digital_download_files'];

I suspect you actually intended to assign this to $listDigitalDownloadFiles, but you just assigned a literal array containing the same string each time, rather than indexing $listDigitalDownload.

Sign up to request clarification or add additional context in comments.

2 Comments

That was it! I meant for that variable to be: $listDigitalDownloadFiles = $listDigitalDownload['digital_download_files']; And then set: ``` foreach($listDigitalDownloadFiles as $listDigitalDownloadFile) { $type = $listDigitalDownloadFile['digital_download_file_type']; echo $type; } ```
Thank you very much! I just needed another set of eyes to see that!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.