| | |
foreach last element array
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Jun 2009
Posts: 2
Reputation:
Solved Threads: 0
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"
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"
php Syntax (Toggle Plain Text)
$key = "mykey"; $url = "http://access.alchemyapi.com/calls/text/TextGetKeywords"; $text = <<<EOD Barrier Logic The barrier logic is a reliable cost effective solution to barrier electronics. This barrier logic is designed to drive a torque motor. FEATURES The smallest industry standard module, with all the requirements for a Parking or Access barrier. strong and Low Cost: Simple but effective for manual barriers. Input Functions: The inputs are simple normal open inputs with toggle, raise, lower and collision functions. Input Protection: The inputs are electrically isolated with opto-couplers to provide 500VDC isolation. Applications: Parking Barrier Control Access Barrier Control EOD; $curlPost = 'apikey=' . urlencode($key) . '&text=' . urlencode($text) . ''; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); $data = curl_exec($ch); curl_close($ch); //echo $data; $xml = new SimpleXMLElement($data); $results = $xml->keywords; $children = $results->children(); $total = count($children); //echo $total; $i=0; foreach($children AS $child) { if ($i < $total) { print $i . $child.", \n"; } elseif ($i == $total) { print $child; } $i++; }
This:
should be:
PHP Syntax (Toggle Plain Text)
$total = count($children);
PHP Syntax (Toggle Plain Text)
$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.
Use [code] tags.
If you have found a solution to your problem, please mark the thread as SOLVED.
![]() |
Similar Threads
- Find the biggest element in that array (C++)
- Ignore duplicate element in array and continue reading (Java)
- how to add the first element of first array of every record (Visual Basic 4 / 5 / 6)
- Comparing an element in an array (C++)
- Permutations of an N element array (C++)
- Stripping the last element off an array (Perl)
- inserting an element into an array in c language (C)
- inserting an element into an array in c language (C)
Other Threads in the PHP Forum
- Previous Thread: Joomla or Drupa for Beginner?
- Next Thread: security concerns
| Thread Tools | Search this Thread |
.htaccess ajax alerts apache api array autocomplete beginner binary broken cakephp checkbox class cms code convert cron curl database date display dynamic echo email emptydisplayvalue error explodefunction file files folder form forms function functions google hack href htaccess html htmlspecialchars image include insert integration ip java javascript joomla keywords limit link login loop mail menu methods mlm multiple mysql network object oop paypal pdf php problem query radio random recursion recursive redirect regex remote script search securephp server sessions shot sms soap source space sql subscription syntax system table tutorial update upload url validation validator variable video web xml youtube






