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"

$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++;
}

Recommended Answers

All 2 Replies

This:

$total = count($children);

should be:

$total = count($children) - 1;

Thank you for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.