-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

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
raivis
  • 37
  • 1
  • 2
  • 9
  • What have you tried? -- besides, you've got your answer: `$form->data['Itemid']` or `$form->data->Itemid;` – Elias Van Ootegem Aug 13 '12 at 15:10
  • possible duplicate of [how to get single value from php array](http://stackoverflow.com/questions/4383914/how-to-get-single-value-from-php-array) – jeroen Aug 13 '12 at 15:10
  • if ($key == 'Itemid') { // do stuff? } – wesside Aug 13 '12 at 15:10
  • 1
    @EliasVanOotegem: `$form->data->Itemid` is not array syntax. – FtDRbwLXw6 Aug 13 '12 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 – Elias Van Ootegem Aug 13 '12 at 15:14
  • Hi @jeroen I did check this post but I could not get the right option out from it.. – raivis Aug 13 '12 at 15:20
  • @EliasVanOotegem: The question says specifically "I have array [...]". Besides, teaching the OP about `ArrayAccess` when he doesn't even know how basic arrays work is just muddying the waters. – FtDRbwLXw6 Aug 13 '12 at 15:26
  • @drrcknlsn: if the OP doesn't know the basics -as you so nicely put it-, what makes you think that he uses the term `array` correctly? looks like a form object of some framework to me, to be perfectly honest... I just responded to your comment, nothing more. If my assumptions are correct, and the op is using a framework, he should know the `ArrayAccess` ASAP, before he encounters code accessing the data as an array and an object at the same time, which will really muddy the waters – Elias Van Ootegem Aug 13 '12 at 15:38
  • EliasVanOotegem and drrcknlsn - guys, I do modification for Chronoforms component in Joomla environment. Component allows a lot of custom modification and it saves data from the form in $form->data array. I do not know how to handle that kind of array expression (object type) (if I may say so) this is why I came up with the question. Let me know if the solutions provided here may lead to some errors or confusions. Thanks! Brgds! Raivis – raivis Aug 13 '12 at 16:08
  • @raivis: If that's the case, then you should edit your question and include all that info, because it originally sounded like you didn't know how to access basic array elements. – FtDRbwLXw6 Aug 13 '12 at 16:23
  • @EliasVanOotegem: The whole point to [`ArrayAccess`](http://php.net/manual/en/class.arrayaccess.php) is exactly that - array access. You don't implement `ArrayAccess` unless you want the object to be able to be accessed as an array (e.g. `$foo['bar']` syntax). Besides this, `$foo->bar` does not necessarily mean the same thing as `$foo['bar']` on an `ArrayAccess` object. `ArrayAccess::offsetGet('bar')` could be returning `$foo->_quux['bar']`, where `$foo->bar` may be a public property. – FtDRbwLXw6 Aug 13 '12 at 16:28
  • @raivis: put simply: just use the array syntax (`$form->data['Itemid']`) and you'll be fine. Just know that, if ever you see an object being passed to a `foreach` loop, you can read up on the `ArrayAccess` interface to find out what is going on, you might even want to use it yourself some time... @drrcknlsn: Relax m8, I know what the `ArrayAccess` interface is, what it's for and how it can be used. Don't get all worked up over it. Talk about muddying the waters: there's nothing like a senseless off-topic argument to do just that. – Elias Van Ootegem Aug 13 '12 at 18:18

2 Answers2

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'];
}
HappyTimeGopher
  • 1,377
  • 9
  • 14
  • 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 Aug 13 '12 at 15:17
  • 1
    @raivis can you please click the green tick icon to the left of this answer, under the up/down vote controls? Thanks :) – HappyTimeGopher Aug 13 '12 at 21:51
1

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

echo $form->data['Itemid']
FtDRbwLXw6
  • 27,774
  • 13
  • 70
  • 107