PHP Loop Over array

They have: 218 posts

Joined: Apr 2001

Is there a way to loop over all the properties and values stored after a query, almost like a 'for-in' loop. Replacing this:

$curObj = mysql_fetch_array($result);

$output .= "&curObj" . = $curObj['propOne'];
$output .= "&curObj" . = $curObj['propTwo'];

With a more generic loop (and less typing).

Cheers,

TonyMontana

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

while ($row = mysql_fetch_array($result)) {
$output .= "&curObj". = $curObj['$row'];
}

echo $output;

I think?

They have: 218 posts

Joined: Apr 2001

I think that will loop over the rows in a query. Whereas, I want to loop over all the data from the columns, of one row, in this case.

$curObj = mysql_fetch_array($result); // one row

$mediaID = $curObj['mediaID']; // one column, multiple columns...
$output .= "&curObj" . "mediaID=" . $mediaID;

echo $output;

Which gets tedious when you have a lot of columns, and is not reusable....any other suggestions?

Cheers.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

All the fields in a recordset?

<?php
$curObj
= mysql_fetch_array($result);
foreach (
$curObj as $key=>$val) {
   
$output .= \"&curObj[$key]=$val\";
}
?>

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 218 posts

Joined: Apr 2001

Mark, that's not evaluating correctly for Flash.
It's something to do with this line, I think:

$output .= "&curObj[$key]=$val";

If you can figure that one out.

Cheers.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Oh, flash...

try this:

<?php
$curObj
= mysql_fetch_array($result);
foreach (
$curObj as $key=>$val) {
   
$output .= \"&$key=$val\";
}
?>

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 218 posts

Joined: Apr 2001

That works great! Thanks.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.