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
Release: 2024-12-18 04:07:13
Original
353 people have browsed it

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";
}
Copy after login

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 '
';
Copy after login
  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";
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template