0

This one only covers the first record in the array -- $form[items][0][description].
How could I iterate this to be able to echo succeeding ones i.e

$form[items][1][description];
$form[items][2][description];
$form[items][3][description];

and so on and so forth?

$array = $form[items][0][description];

function get_line($array, $line) {
  preg_match('/' . preg_quote($line) . ': ([^\n]+)/', $array['#value'], $match);
  return $match[1];
}

$anchortext = get_line($array, 'Anchor Text');
$url = get_line($array, 'URL');

echo '<a href="' . $url . '">' . $anchortext . '</a>';

?>
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
xchampionx
  • 106
  • 1
  • 11

2 Answers2

2

This should do the trick

foreach ($form['items'] as $item) {
  echo $item['description'] . "<br>";
}

I could help you more if I saw the body of your get_line function, but here's the gist of it

foreach ($form['items'] as $item) {
  $anchor_text = get_line($item['description'], 'Anchor Text');
  $url = get_line($item['description'], 'URL');
  echo "<a href=\"{$url}\">{$anchor_text}</a>";
}
maček
  • 76,434
  • 37
  • 167
  • 198
  • OK thanks for that. But how does it come with my code above? I still need to assign the values to the the $array variable. Still a noob so forgive me if I'm asking the obvious. – xchampionx Dec 29 '12 at 07:11
  • @xchampionx, please see my edit. I'm pretty sure I could help you quite a bit more if I saw what your `get_line` function was doing, though. – maček Dec 29 '12 at 07:14
  • How come the solution above returns empty/non-existent elements? – xchampionx Dec 30 '12 at 02:10
  • Checked it against php's empty() function but still the same. Neither do the solutions here - http://stackoverflow.com/questions/3654295/remove-empty-array-elements – xchampionx Dec 30 '12 at 02:26
0

You can use a for loop to iterate over this array.

for($i=0; $i< count($form['items']); $i++)
{
    $anchortext = get_line($form['items'][$i]['description'], 'Anchor Text');
    $url = get_line($form['items'][$i]['description'], 'URL');
    echo '<a href="' . $url . '">' . $anchortext . '</a>';
}
maček
  • 76,434
  • 37
  • 167
  • 198
Mike
  • 1,718
  • 3
  • 30
  • 58