Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am having a problem trying to explode an array, connection to database works and tested, the commented print line works, but the explode doesn't, every time i explode the item it
prints ArrayArrayAray. I have been stuck on this for so long, looked everywhere, followed examples but to no avail. If anyone could help it will be much appreciated.

          $result_ = mysql_query("SELECT * FROM " .PRODUCT_INSTRUCTIONS. " order by group_code") or  
                        die($errorhandler->add("ERROR", __FILE__ . "=>" . __CLASS__ . "=>" . __FUNCTION__ . "=>" . __LINE__, "Failed getting items." . mysql_error()));

                         while($rows_=  mysql_fetch_array($result_)){
                                  $instruction = $rows_['instructions'];
                                 // print "<tr><td>".$rows_['group_code']."</td></tr>"." <tr><th>".$instruction."</th></tr>";
                                  $inst = explode("|", $instruction);

                                  for($i = 0; $i < count($inst); $i++){
                                      echo $inst;
                                  }

                              }

                        ?>
share|improve this question

closed as off-topic by deceze, Barmar, LinkinTED, Maerlyn, Niet the Dark Absol Jul 3 at 10:06

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – deceze, Barmar, LinkinTED, Maerlyn, Niet the Dark Absol
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
You are echo $inst;ing each time. In other words, you are outputting the array each time, not the array's contents. –  Niet the Dark Absol Jul 3 at 9:34
1  
echo $inst[$i]...!? –  deceze Jul 3 at 9:35
    
Explode returns an array of strings, I think you should echo $inst[$i]; –  MiPnamic Jul 3 at 9:36
    
Thanks for your contribution –  Martin Snitram Jul 3 at 11:48

4 Answers 4

up vote 2 down vote accepted

Add the index to the echo command.

for($i = 0; $i < count($inst); $i++){
    echo $inst[$i];
}
share|improve this answer
    
Wow awesome, thanks that worked perfectly :-) –  Martin Snitram Jul 3 at 9:42
    
No problem. Don't forget to mark correct! –  James Hunt Jul 3 at 9:45

To print off arrays, use var_dump() instead.

$instruction = $rows('instructions');
                             // print "<tr><td>".$rows_['group_code']."</td></tr>"." <tr><th>".$instruction."</th></tr>";
                              $inst = explode("|", $instruction);

                              for($i = 0; $i < count($inst); $i++){
                                  echo $inst[$i];
                              }
share|improve this answer

replace

echo $inst;

with

print_r($inst);
share|improve this answer
    
This isn't what he asked in the code, he wants the loop to echo each element, not dump the array 3 times. –  James Hunt Jul 3 at 9:46
    
Yes you right, your answer is correct, need to write echo $inst[$i]. –  Nebojsa Susic Jul 3 at 9:52
for($i = 0; $i < count($inst); $i++)
{
    //If $inst is an array it will print the array else it print as string
    if(is_array($inst))
        print_r($inst);
    else
        echo $inst;
}
share|improve this answer
    
When entering code in an answer, it's best to explain what it does. –  DavidG Jul 3 at 10:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.