Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Get array contents with no key

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
TirednBurntOut
Smarty Rookie


Joined: 21 Nov 2014
Posts: 24

PostPosted: Sun Mar 15, 2015 8:41 pm    Post subject: Get array contents with no key Reply with quote

Have inherited some software that gives me an array with one item. A live @var_dump looks something like this:

array(1) {[697]=>string(42) "category name goes here"}

The array name is Category. So if I do Category[0], or Category[1], I get nada, zip. I do get the category name wanted if I do Category[697].

Unfortunately, I dont really know what that 697 will be during the live execution. I do know that the 697 is an index key to identify the category in a table. I dont need the key, I just need to retrieve the category name out of the array. Also, I am very curious as to how the array index got to be 697. I would understand a text "697" as an index name.

To summarize the problem - the index number is not starting at 0 as usual. The index number is being set programmatically, so I will never know what it is. In the example I gave, it happened to be 697. But I would still like to retrieve the contents of the single array element, which will be a category name.


Last edited by TirednBurntOut on Mon Mar 16, 2015 12:34 am; edited 1 time in total
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun Mar 15, 2015 9:47 pm    Post subject: Reply with quote

http://php.net/array-shift
Back to top
View user's profile Send private message
TirednBurntOut
Smarty Rookie


Joined: 21 Nov 2014
Posts: 24

PostPosted: Sun Mar 15, 2015 10:39 pm    Post subject: Reply with quote

AnrDaemon wrote:
http://php.net/array-shift


Thanks for the input. Upon first consideration, I dont know if that helps, so I will have to think about it. At the point I have this array, I am in the template, so PHP is not readily available, as I understand it.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Sun Mar 15, 2015 11:49 pm    Post subject: Reply with quote

PHP functions are available in templates as long as they are not disabled by Security.

AnrDaemon wrote:
http://php.net/array-shift

array_shift() is a bad solution as it removes elements from the array.

use http://php.net/manual/en/function.reset.php

Example
Code:
{$first = reset($foo)}
Back to top
View user's profile Send private message
TirednBurntOut
Smarty Rookie


Joined: 21 Nov 2014
Posts: 24

PostPosted: Mon Mar 16, 2015 12:29 am    Post subject: Reply with quote

[quote="U.Tews"]PHP functions are available in templates as long as they are not disabled by Security.

Thats news to me...thanks.
Back to top
View user's profile Send private message
TirednBurntOut
Smarty Rookie


Joined: 21 Nov 2014
Posts: 24

PostPosted: Mon Mar 16, 2015 12:31 am    Post subject: Reply with quote

use http://php.net/manual/en/function.reset.php

Example
Code:
{$first = reset($foo)}
[/quote]

That still does not give me contents of the array. It only had one element to begin with. The problem is, the index number is not starting at 0 as usual. The index number is being set programmatically, so I will never know what it is. In the example I gave, it happened to be 697.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Mar 16, 2015 12:45 am    Post subject: Reply with quote

U.Tews wrote:
AnrDaemon wrote:
http://php.net/array-shift

array_shift() is a bad solution as it removes elements from the array.

use http://php.net/manual/en/function.reset.php

Example
Code:
{$first = reset($foo)}

How'd reset help? He don't know, what the array index. And he know that there's only one element in the array.
I see no problem in retrieving the only elemnt from the array.
And preferable do it before assigning the variable to template.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Mon Mar 16, 2015 1:14 am    Post subject: Reply with quote

TirednBurntOut wrote:
use http://php.net/manual/en/function.reset.php

Example
Code:
{$first = reset($foo)}


That still does not give me contents of the array. It only had one element to begin with. The problem is, the index number is not starting at 0 as usual. The index number is being set programmatically, so I will never know what it is. In the example I gave, it happened to be 697.



reset() does return the content of the first arrayelement independent from it's index.
Code:
{$first = reset($foo)}

Template variable $first will contain the content of the first element of array $foo.
In your case "category name goes here"
Back to top
View user's profile Send private message
TirednBurntOut
Smarty Rookie


Joined: 21 Nov 2014
Posts: 24

PostPosted: Mon Mar 16, 2015 1:17 am    Post subject: Reply with quote

AnrDaemon wrote:

I see no problem in retrieving the only elemnt from the array.
And preferable do it before assigning the variable to template.


See, its not a perfect world. I am trying to mod some world-class lousy spaghetti code - SmartJobBoard (spit) - it has a horrendous schema that I dont want to mess with. (wanna talk about H.A.T.I.N.G) Here I have the information, I can see it in a var_dump. I just need to access it. Somehow, they've bastardized the use of the array index, which should simply be zero. If it was zero, no problem.
Back to top
View user's profile Send private message
TirednBurntOut
Smarty Rookie


Joined: 21 Nov 2014
Posts: 24

PostPosted: Mon Mar 16, 2015 1:34 am    Post subject: Reply with quote

U.Tews wrote:


reset() does return the content of the first arrayelement independent from it's index.
Code:
{$first = reset($foo)}

Template variable $first will contain the content of the first element of array $foo.
In your case "category name goes here"


Hmmm...not working for me...here is the code out of the template
{$first = reset($listing.Category)}
{$first} {* doesnt display anything *}

Perhaps because Category is nested?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Mon Mar 16, 2015 1:52 am    Post subject: Reply with quote

TirednBurntOut wrote:


Hmmm...not working for me...here is the code out of the template
{$first = reset($listing.Category)}
{$first} {* doesnt display anything *}

Perhaps because Category is nested?


If $listing.Category is an array it does work.

Test
Code:
{$test['Category'][697] = "category name goes here"}
{$first = reset($test.Category)}
{$first}

Returns expected result
Back to top
View user's profile Send private message
TirednBurntOut
Smarty Rookie


Joined: 21 Nov 2014
Posts: 24

PostPosted: Mon Mar 16, 2015 2:13 am    Post subject: Reply with quote

U.Tews wrote:

If $listing.Category is an array it does work.

Test
Code:
{$test['Category'][697] = "category name goes here"}
{$first = reset($test.Category)}
{$first}

Returns expected result


Tried it, no go.
Got a fatal syntax error: unrecognized tag: $test['Category'][697] = "category name goes here"

This system is Smarty 2.6.14

Would that make a difference?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Mon Mar 16, 2015 3:54 am    Post subject: Reply with quote

Okay Smarty2 does not accept most of the examples above.
you must use reset as modifier.

Code:
{assign var=first value=$listing.Category|@reset}
{$first}

or

{$listing.Category|@reset}
Back to top
View user's profile Send private message
TirednBurntOut
Smarty Rookie


Joined: 21 Nov 2014
Posts: 24

PostPosted: Mon Mar 16, 2015 1:37 pm    Post subject: Reply with quote

U.Tews wrote:
Okay Smarty2 does not accept most of the examples above.
you must use reset as modifier.

Code:
{assign var=first value=$listing.Category|@reset}
{$first}





Worked! Thanks much...
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP