-9

I have array that I can display by code:

foreach($form->data as $key => $value){
echo 'Key is '.$key.' and Value is '.$value.'<br />';
}

And I get following display:

Key is language and Value is lv-LV
Key is Itemid and Value is 114
Key is option and Value is com_content
Key is pumpis_1 and Value is 1
Key is lietussargs and Value is 2

But I need to display only value of [Itemid] which in this case is 114 How can I do it? Thanks! Raivis

12
  • What have you tried? -- besides, you've got your answer: $form->data['Itemid'] or $form->data->Itemid; Commented Aug 13, 2012 at 15:10
  • possible duplicate of how to get single value from php array Commented Aug 13, 2012 at 15:10
  • if ($key == 'Itemid') { // do stuff? } Commented Aug 13, 2012 at 15:10
  • 1
    @EliasVanOotegem: $form->data->Itemid is not array syntax. Commented Aug 13, 2012 at 15:11
  • 2
    @drrcknlsn: I know, but since the op is clearly using an object, and the results look like what a query returns (or a parsed xml doc, or...), it could well be an object that implements the ArrayAccess interface, in which case $var->data->colName would be valid Commented Aug 13, 2012 at 15:14

2 Answers 2

5
echo $form->data['Itemid'];

Or if you mean inside the foreach loop (because you've got other stuff to do there), then use this:

foreach($form->data as $key => $value) {
    if( $key === 'Itemid' )
        echo $form->data['Itemid'];
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @HappyTimeGopher ! Previously I tried various versions of foreach loop but somehow I didn't get to this option.. Now this one works! Thanks to all responses!!
@raivis can you please click the green tick icon to the left of this answer, under the up/down vote controls? Thanks :)
1

You should read up on PHP arrays. The syntax is this:

echo $form->data['Itemid']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.