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>
<sp开发者_运维百科an 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?
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.
foreach ($arr as $row) {
echo '<li><span>', join('</span><span>', explode(',', $row, 3)), '</span></li>';
}
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>';
}
<?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
精彩评论