0

I have a list of arrays and need them to output with a printf statement

<?php $example = array("first" => "Bob", "last" => "Smith", "address" => "123 Spruce st" ); $example = array("first" => "Sara", "last" => "Blask", "address" => "5678 Maple ct" ); foreach ($example as $key => $val) { printf("<p>hello my name is %s %s and i live at %s</p>",$example['first'],$example['last'], $example['address']); } ?> 

The above just outputs the last array, i need it to loop through all of the arrays and produce a <p> with the supplied key => value combinations. this is only a simplified example as the real world code will be more complex in the outputted html

I tried

foreach ($example as $arr){ printf("<p>hello my name is %s %s and i live at %s</p>",$arr['first'],$arr['last'], $arr['address']); } 

but it only outputs a single character for each key => value

1
  • 3
    You're declaring $example twice - the second one will over-write the first one. That definitely won't help. Commented Sep 11, 2012 at 18:45

4 Answers 4

2

Try something like this:

// Declare $example as an array, and add arrays to it $example = array(); $example[] = array("first" => "Bob", "last" => "Smith", "address" => "123 Spruce st" ); $example[] = array("first" => "Sara", "last" => "Blask", "address" => "5678 Maple ct" ); // Loop over each sub-array foreach( $example as $val) { // Access elements via $val printf("<p>hello my name is %s %s and i live at %s</p>",$val['first'],$val['last'], $val['address']); } 

You can see from this demo that it prints:

hello my name is Bob Smith and i live at 123 Spruce st hello my name is Sara Blask and i live at 5678 Maple ct 
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome that's what i was missing declaring $example as an array! thank you. and +1 for the demo!
You're welcome! Just to clarify, it is not required to declare it as an array, as doing $example[] will implicitly create $example as an array. However, it is my preference and a general best-practice to define variables before you use them.
1

You need to declare example as an array as well to get a 2-dimensional array and then append to it.

$example = array(); $example[] = array("first" => "Bob", "last" => "Smith", "address" => "123 Spruce st" ); # appends to array $example $example[] = array("first" => "Sara", "last" => "Blask", "address" => "5678 Maple ct" ); 

Comments

0

You're overwriting $example on both lines. You need a multi-dimensional "array of arrays:"

$examples = array(); $examples[] = array("first" ... $examples[] = array("first" ... foreach ($examples as $example) { foreach ($example as $key => $value) { ... 

Of course, you can also do the printf immediately instead of assigning the arrays.

Comments

0

You'll have to make an array of your arrays and loop through the main array:

<?php $examples[] = array("first" => "Bob", "last" => "Smith", "address" => "123 Spruce st" ); $examples[] = array("first" => "Sara", "last" => "Blask", "address" => "5678 Maple ct" ); foreach ($examples as $example) { printf("<p>hello my name is %s %s and i live at %s</p>",$example['first'],$example['last'], $example['address']); } ?> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.