0

I have an an array with objects:

Array ( [0] => 479,1,sometext [1] => 474,2,again text [2] => 472,3,and text ) 

The objects in array can be unlimited, but each object consists of 3 parts delimited by comma .

The result should be (in my example)- CORRECTED secondly

<li>
<span class="somecrap">array[0][0]</span>
<input type="text" class="secondcrap" value="array[0][1]"/>
<textarea class="3crap">array[0][2]</textarea>
</li>
<li>
<span class="somecrap">array[1][0]</span>
<input type="text" class="secondcrap" value="array[1][1]"/>
<textarea class="3crap">array[1][2]</textarea>
</li>
<li>
<span class="somecrap">array[2][0]</span>
<input type="text" class="secondcrap" value="array[2][1]"/>
<textarea class="3crap">array[2][2]</textarea>
</li>

etc.... depending of the count of the array objects

How would you accomplish this?

3
  • There is a corrected version of the result example. A bit more dificult. Commented Feb 24, 2011 at 11:01
  • I edited my answer to accommodate that - not too difficult Commented Feb 24, 2011 at 11:06
  • The results are corrected again, becouse Sam Dufel are so smart about arrays and gives for me too good solutions :D Commented Feb 24, 2011 at 11:15

4 Answers 4

1

Try this:

foreach($array as $entry) {
    echo "<li>\n";
    $elements = explode(',', $entry, 3);
    echo "<span class='somecrap'>{$elements[0]}</span>\n";
    echo "<input type='text' class='secondcrap' value='{$elements[1]}'/>\n";
    echo "<textarea class='3crap'>{$elements[2]}</textarea>\n";
    echo "</li>\n";
}

Updated to reflect your edited requirements. Untested.

Sign up to request clarification or add additional context in comments.

2 Comments

You have a speed as a rocket :) Anyway, I corrected my example of results. The trick is, tat every span has its own Id.
jam: Should the id/class names be the same for every <span>? Or do you want it to be dependent on the content of each array entry?
1
foreach ($arr as $row) {
    echo '<li><span>', join('</span><span>', explode(',', $row, 3)), '</span></li>';
}

1 Comment

+1 You might want to add the limit parameter for explode to limit the result to at most 3 parts.
0

Your array is not 2D, it should look like that:

 Array (
   [0] => Array( [0] => 479, [1] => 1, [2] => sometext),
   [1] => Array([0] >= 474, [1] => 2, [2] => again text),
   [2] => Array([0] => 472, [1] => 3, [2] => and text)
  )

After that, it's just a matter of nested foreach:

foreach($myArray as $level_one){
 echo '<li>';
 foreach($level_one as $item){
   echo '<span>' . $item . '</span>';
 }
 echo '</li>';
} 

Comments

0
<?php 
$array = //Your input
$fieldNames = array('crap', 'someCrap', '3rdCrap');

foreach ($array as $row) {
  $fields = explode(',', $row);
  echo "<li>";
  for($i = 0; $i < 3; ++$i) {
    echo "<span class='{$fieldNames[$i]}'>";
    if(isset($fields[$i]))
      echo $fields[$i];
    echo "</span>";
  }
  echo "</li>";
}
?>

^^ Edited for your new requirements

2 Comments

You have a speed as a rocket :) Anyway, I corrected my example of results. The trick is, tat every span has its own Id.
Wow. You are born with php arrays in your mind :) What do you say about the new example of results?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.