Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get array elements like print_r?

Posted on 2010-08-12
4
Medium Priority
?
441 Views
Last Modified: 2012-05-10
How can I iterate over the elements of an array in the same way as the print_r output?

For example, print_r displays the array as:

Array ( [0] => Array ( ) [1] => Array ( [0] => A ) [2] => Array ( [0] => B ) [3] => Array ( [0] => B [1] => A ) [4] => Array ( [0] => C ) [5] => Array ( [0] => C [1] => A ) [6] => Array ( [0] => C [1] => B ) [7] => Array ( [0] => C [1] => B [2] => A ) )

If I use foreach to access each element, there are error messages for each array element for which there are no values:

foreach ($array as $i1 => $n1) {
        foreach ($array as $i2 => $n2){
           
           echo $array[$i1][$i2];
        }  
}

0
Comment
Question by:pillmill
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 2

Accepted Solution

by:
gtagliani earned 2000 total points
ID: 33422322
try this:

for ( $i = 0 ; $i < count($array) ; $i++ )
{
      echo "<br>";
      for ( $j = 0 ; $j < count($array[$i]) ; $j++ )
      {
            echo $array[$i][$j] . " - ";
      }

}
0
 
LVL 4

Expert Comment

by:stevepicks
ID: 33422325
Have a look at
http://www.learnphp-tutorial.com/Arrays.cfm
at the bottom of the page, there is a two-dimensional array example
Hope it helps
Cheers
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 33422884
You can use isset() or empty() to test for the existence and content of variables before you use them in other PHP statements.  A good programming practice always!

But having said that, this sequence of code looks confused.

foreach ($array as $i1 => $n1) {
        foreach ($array as $i2 => $n2){
           
           echo $array[$i1][$i2];
        }  
}

I have never seen a design pattern that nested two iterators inside one another - when accessing the same array or object.  If you want to post a code snippet that includes a piece of sample data, I can show you the ways to use the foreach() statements.  I would be glad to use the example you posted above, but it's too much of a nuisance to copy the print_r output and turn it back into an array, so just give us the starting data and I'll show you how print_r would do it.

best, ~Ray
0
 
LVL 4

Expert Comment

by:thor_victor
ID: 33430911
What are you trying to do exacly?

Compare each data value with something?

Or you are trying just to format as print_r?
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question