Home >Backend Development >PHP Tutorial >How Can I Echo Multi-Dimensional Array Contents in PHP Without Showing the Array Structure?

How Can I Echo Multi-Dimensional Array Contents in PHP Without Showing the Array Structure?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-18 04:07:13335browse

How Can I Echo Multi-Dimensional Array Contents in PHP Without Showing the Array Structure?

Echoing Array Contents in PHP

Question:

I have an array with multiple levels of data. How can I display its contents without the array structure?

Code:

$results = [
    'data' => [
        [
            'page_id' => 204725966262837,
            'type' => 'WEBSITE',
        ],
        [
            'page_id' => 163703342377960,
            'type' => 'COMMUNITY',
        ],
    ]
];

// Unsuccessful attempt
foreach ($results as $result) {
    echo $result->type;
    echo "\n";
}

Answer:

To display the contents of an array without its structure, you can use one of the following methods:

  1. print_r() and echo:
echo '<pre class="brush:php;toolbar:false">';
print_r($results);
echo '
';
  1. var_dump(): Provides detailed information about the array, including data types and lengths.
  2. Foreach Loop: Iterate over the array and echo the desired values.
foreach ($results['data'] as $data) {
    echo $data['type'] . "\n";
}

The above is the detailed content of How Can I Echo Multi-Dimensional Array Contents in PHP Without Showing the Array Structure?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn