Jump to content

Archived

This topic is now archived and is closed to further replies.

sublevel4

getting first item of array

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Instead of using a foreach, use array_shift()

Share this post


Link to post
Share on other sites

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;

 

}

 

}

 

?>

Share this post


Link to post
Share on other sites

And what happens when you simply do this

echo "<pre>", print_r($resmilestones['body']->milestone[0]), "</pre>";

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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'};

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.