PHP Array - How to loop through an array

My array is as follows:

Array ( [questions] => q1 [qnum] => 1 [qtext] => I love crisps [page] => 1 ) Array ( [questions] => q2 [qnum] => 2 [qtext] => I love chocolate [page] => 1 )

All I would like to do is print the contents of the array in a loop, so it looks something like:

q1 1 I love crisps q2 2 I love chocolate

If anyone could supply the PHP and HTML code, that would be great, thanks.

This is a multi-dimensional array - I should have stated that. In addition, I want to be able to access each element of the array row, for example. I also want to just access the [qtext] element on its own.

Is this possible?


-------------Problems Reply------------

You can use the foreach construct here.

foreach($arr as $value)
echo "$value ";

If you wanted to print out the keys too you could use:

foreach($arr as $key => $value)
echo "$key: $value ";

And if you just want to print out for debugging there's always


You can use foreach like so

$arr = array("foo" => "bar", 12 => true);

foreach ($arr as $key => $value)
echo "$key => $value<br />\n";

If it's just an array of arrays, you would do something like this:

foreach ($multi_array as $arr)
foreach($arr as $item)
echo $item['questions'], ' ', $item['qnum'], ' ', $item['qtext'], '<br>';

Category:php Views:0 Time:2010-04-23
Tags: php

Related post

  • Improving stepping through an array twice (Nested loop on same array) 2011-08-30

    I have a large set of data that I want to cycle through in order to determine various statistics on the data set from a point in time 'D1' to a point in time in the future 'D2'. Basically, I want to add to a database each time the difference between

  • Replacing array in foreach loop with array modified in same loop 2008-10-23

    foreach($arrayOne as $value){ do function } In the above example, I'd like to pass $arrayOne into a loop, have a function operate that removes some elements of $arrayOne and then have the loop pass over the reduced $arrayOne on the elements that are

  • modern for loop for primitive array 2008-11-05

    Is there any performance difference between the for loops for primitive array? Assume double[] doubleArray = new double[300000]; for (double var: doubleArray) someComplexCalculation(var); or for ( int i = 0, y = doubleArray.length; i < y; i++) som

  • Using loops to create arrays 2008-11-12

    I am extremely new at php and I was wondering if someone could help me use either a for or while loop to create an array 10 elements in length --------------Solutions------------- $array = array(); $array2 = array(); // for example for ($i = 0; $i

  • how to loop through this array in php 2009-06-10

    I want to loop through this array: $securePages=array("admin.php","addslot.php","classpost.php"); $pagename="admin.php" Then if admin.php is found then execute this code: header("location:index.php"); exit(); How would I put together this looping sta

  • Make a for loop with an array and variable 2009-07-17

    I am using the code below to draw an object to the stage. However I want to be able to redraw the object every time I press Enter in my onEnterframe function. So I have to add the draw function again but with a new array. Also every time I push enter

  • JS looping and populating array. Which is faster? 2009-08-11

    I just saw a video of Nicholas Zakas of Yahoo, at GoogleTalks talking about speeding up your website. One of the things he mentioned was doing loops in reverse order to skip one of two comparisons: for (i = len; i--;) {} And he said to keep away from

  • Why do some programming languages restrict you from editing the array you're looping through? 2009-08-20

    Pseudo-code: for each x in someArray { // possibly add an element to someArray } I forget the name of the exception this throws in some languages. I'm curious to know why some languages prohibit this use case, whereas other languages allow it. Are th

  • Looping a multidimensional array in php 2009-10-11

    I have a multidimensional array like this: array(2) { [1]=> array(3) { ["eventID"]=> string(1) "1" ["eventTitle"]=> string(7) "EVENT 1" ["artists"]=> array(3) { [4]=> array(2) { ["name"]=> string(8) "ARTIST 1" ["description"]=> s

  • how to loop through associative array and echo out to list? 2009-10-15

    I have managed to get my first array working but no matter how many code examples I try I cannot step through each array row and echo the three columns / elements out to a . The var_dump of my array is:- array(27) { [3]=> array(3) { ["id"]=> st

  • how can you loop through multiple arrays parallel? 2009-10-21

    i have 4 arrays. ["one", "two", "three"] ["1", "2", "3" ["un", "deux", "trois"] ["ichi", "ni", "san"] is it possible to concatenate each element in their respective arrays ? so i end up with single lines of string like like "one, 1, un, ichi"\n "two,

  • Looping through an array of arrays, changing output on a given line(s) 2009-10-26

    This is what im using to loop through an array of arrays. $csvpre = explode("###", $data); $i = 0; $bgc = 0; foreach ( $csvpre AS $key => $value){ $info = explode("%%", $value); $i++; if($i == "1"){ echo "<tr bgcolor=#efefef><td></t

  • In ActionScript 3, how can I pass the current value of an array in a loop to an event listener 2009-11-11

    Code Example: var gospels : Array = ["john", "mark", "matthew", "paul"]; for each (var book : String in gospels) { var loader : URLLoader = new URLLoader(); loader.load(new URLRequest("" + name)); trace(book) // outputs current val

  • Loop through an Array of links and dynamically load the contents of each after a set interval 2010-01-12

    Using jQuery I would like to loop through an array of links and load the contents of each link into a DIV after a set interval. For example, if I have my parent page "parent.html" and an array of links - "test1.html, test2.html and test3.html" I woul

  • How to delete object from array inside foreach loop? 2010-02-21

    I iterate through an array of objects and want to delete one of the objects based on it's 'id' property, but my code doesn't work. foreach($array as $element) { foreach($element as $key => $value) { if($key == 'id' && $value == 'searched_v

  • ASP.NET looping through jagged array stored in session 2010-02-24

    im having some problems with jagged arrays stored in session for i have some code which creates a jagged array, them populates, and then stores this populated jagged array into session protected string[][] answersJArray; answersJArray[0] = ne

  • C# looping through an array 2010-03-09

    I am looping through an array of strings, such as (1/12/1992 apple truck 12/10/10 orange bicycle). The array's length will always be divisible by 3. I need to loop through the array and grab the first 3 items (I'm going to insert them into a DB) and

  • looping through JSON array 2010-03-27

    I have recently posted another question which straight away users pointed me in the right direction. $.ajax({ type: 'POST', url: './', data: 'token=' + token + '&re=8', cache: false, timeout: 5000, success: function(html){ auth(html); var JSON_ar

  • foreach loop from multiple arrays c# 2010-06-01

    This should be a simple question. All I want to know is if there is a better way of coding this. I want to do a foreach loop for every array, without having to redeclare the foreach loop. Is there a way c# projects this? I was thinking of putting thi

Copyright (C), All Rights Reserved.

processed in 0.166 (s). 11 q(s)