sublevel4 Posted September 18, 2012 Share Posted September 18, 2012 Here is the code foreach($response['body']->project as $project){ // output only where status is active if ($project->status == 'active'){ $created = $project->{'created-on'}; $catagory = $project->company->name; $project_name = $project->name; $projectId = $project->id; $resmilestones = $bc->getMilestonesForProject($projectId,$filter_type='late',$format=null); foreach($resmilestones['body']->milestone as $milestone){ $late_date = $milestone->deadline; $responsible = $milestone->{'responsible-party-name'}; $milestone_name = $milestone->title; $milestone_data = $late_date . ' , ' . $responsible . ' , ' . $milestone_name . "\n"; } $data_string .= $projectId .',' . $created .',' . $catagory .',' . $project_name .',' . $late_date . ' , ' . $responsible . ' , ' . $milestone_name . "\n"; } } in the part foreach($resmilestones['body']->milestone as $milestone) i only want to get the first element. i don't need the others some items have none and some have 10 but i just need the first one as it is the Milestone that is the most over due the last one that i am getting now is the least overdue. And i apparently have no clue how to go about this. Link to comment https://forums.phpfreaks.com/topic/268516-getting-first-item-of-array/ Share on other sites More sharing options...
Jessica Posted September 18, 2012 Share Posted September 18, 2012 http://php.net/array_shift Link to comment https://forums.phpfreaks.com/topic/268516-getting-first-item-of-array/#findComment-1378953 Share on other sites More sharing options...
sublevel4 Posted September 18, 2012 Author Share Posted September 18, 2012 I don't think i stated my original issue correctly. output of $resmilestone gives me something like: [body] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => array ) [milestone] => Array ( [0] => SimpleXMLElement Object ( [all-day] => true [commented-at] => SimpleXMLElement Object ( [@attributes] => Array ( [nil] => true ) ) [comments-count] => 0 [completed] => false [created-on] => 2012-07-27T14:53:29Z [creator-id] => 9459713 [id] => 33086455 [project-id] => 9115676 [responsible-party-id] => 4538224 [responsible-party-type] => Person [start-at] => SimpleXMLElement Object ( [@attributes] => Array ( [nil] => true ) ) [title] => Creative [wants-notification] => true [type] => Milestone [creator-name] => Lynn [deadline] => 2012-09-06 [responsible-party-name] => Michelle ) [1] => SimpleXMLElement Object ( [all-day] => true [commented-at] => SimpleXMLElement Object ( [@attributes] => Array ( [nil] => true ) ) [comments-count] => 0 [completed] => false [created-on] => 2012-07-27T14:53:46Z [creator-id] => 9459713 [id] => 33086460 [project-id] => 9115676 [responsible-party-id] => 9459713 [responsible-party-type] => Person [start-at] => SimpleXMLElement Object ( [@attributes] => Array ( [nil] => true ) ) [title] => Approval [wants-notification] => true [type] => Milestone [creator-name] => Lynn [deadline] => 2012-09-13 [responsible-party-name] => Lynn ) ) ) [status] => 200 OK [location] => ) I am getting the info in the [1] and i need the one in the [0] I hope this sheads a little bit more light on what i am trying to get. Thanks. Link to comment https://forums.phpfreaks.com/topic/268516-getting-first-item-of-array/#findComment-1378994 Share on other sites More sharing options...
Jessica Posted September 18, 2012 Share Posted September 18, 2012 Instead of using a foreach, use array_shift() Link to comment https://forums.phpfreaks.com/topic/268516-getting-first-item-of-array/#findComment-1378997 Share on other sites More sharing options...
jinijames Posted October 20, 2012 Share Posted October 20, 2012 Getting first item of array- <?php if (!empty ( $an_array ) ) { $min = min ( count ( $an_array ), 5 ); $i = 0; foreach ($value in $an_array) { echo $value; $i++; if ($i == $min) break; } } ?> Link to comment https://forums.phpfreaks.com/topic/268516-getting-first-item-of-array/#findComment-1386560 Share on other sites More sharing options...
Zane Posted October 21, 2012 Share Posted October 21, 2012 And what happens when you simply do this echo "<pre>", print_r($resmilestones['body']->milestone[0]), "</pre>"; Link to comment https://forums.phpfreaks.com/topic/268516-getting-first-item-of-array/#findComment-1386697 Share on other sites More sharing options...
shaddowman Posted October 24, 2012 Share Posted October 24, 2012 I think you need to use the built in PHP function "curent" for this array. <?php $theArray = array("first", "second", "third"); print_r(current($theArray)); ?> Please look at the reference php.net/manual/en/function.current.php Link to comment https://forums.phpfreaks.com/topic/268516-getting-first-item-of-array/#findComment-1387568 Share on other sites More sharing options...
us2rn4m2 Posted November 11, 2012 Share Posted November 11, 2012 Sorry for my English ! <?php $milestone = $yourSimpleXmlElementObject->body->milestone[1]; // Because some keys contains characters like '-' , you need to write keys into brackets {} $created_on = $milestone->{'created-on'}; $title = $milestone->{'title'}; $creator_name = $milestone->{'creator-name'}; Link to comment https://forums.phpfreaks.com/topic/268516-getting-first-item-of-array/#findComment-1391717 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.