foreach last element array

Thread Solved

Join Date: Jun 2009
Posts: 2
Reputation: cybershark is an unknown quantity at this point 
Solved Threads: 0
cybershark cybershark is offline Offline
Newbie Poster

foreach last element array

 
0
  #1
Sep 14th, 2009
Hi,

I'm trying to get the IF\ELSE statement within the foreach to print "keyword," or if it's the last element in the array just to print "keyword"
  1. $key = "mykey";
  2. $url = "http://access.alchemyapi.com/calls/text/TextGetKeywords";
  3. $text = <<<EOD
  4. Barrier Logic
  5. The barrier logic is a reliable cost effective solution to barrier electronics.
  6. This barrier logic is designed to drive a torque motor.
  7. FEATURES
  8. The smallest industry standard module, with all the requirements for a Parking or Access barrier.
  9. strong and Low Cost: Simple but effective for manual barriers.
  10. Input Functions: The inputs are simple normal open inputs with toggle, raise, lower and collision functions.
  11. Input Protection: The inputs are electrically isolated with opto-couplers to provide 500VDC isolation.
  12. Applications:
  13. Parking Barrier Control
  14. Access Barrier Control
  15. EOD;
  16. $curlPost = 'apikey=' . urlencode($key) . '&text=' . urlencode($text) . '';
  17. $ch = curl_init();
  18. curl_setopt($ch, CURLOPT_URL, $url);
  19. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  20. curl_setopt($ch, CURLOPT_POST, 1);
  21. curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
  22. $data = curl_exec($ch);
  23. curl_close($ch);
  24. //echo $data;
  25.  
  26. $xml = new SimpleXMLElement($data);
  27. $results = $xml->keywords;
  28. $children = $results->children();
  29. $total = count($children);
  30. //echo $total;
  31. $i=0;
  32. foreach($children AS $child) {
  33. if ($i < $total) {
  34. print $i . $child.", \n";
  35. }
  36. elseif ($i == $total) {
  37. print $child;
  38. }
  39. $i++;
  40. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: foreach last element array

 
0
  #2
Sep 14th, 2009
This:
  1. $total = count($children);
should be:
  1. $total = count($children) - 1;
Google is your friend.

Use [code] tags.

If you have found a solution to your problem, please mark the thread as SOLVED.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 2
Reputation: cybershark is an unknown quantity at this point 
Solved Threads: 0
cybershark cybershark is offline Offline
Newbie Poster

Re: foreach last element array

 
0
  #3
Sep 14th, 2009
Thank you for the help.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC