Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multidimensional array keys #254

Closed
ophian opened this issue Jul 15, 2016 · 5 comments
Closed

multidimensional array keys #254

ophian opened this issue Jul 15, 2016 · 5 comments

Comments

@ophian
Copy link

ophian commented Jul 15, 2016

3.1.30-dev/80

I have just found a strange sudden foreach issue. This definitely worked before, but I don't know which version broke this behaviour, or if I myself did something strange causing this.

<pre>{$templates|print_r}</pre> is a key named array which includes another key named subarray.

$templates = array (
    [foo] => Array
        (
            [info] => Array
                (
                    [name] => foo
                    [author] => bar
                    [date] => 2016-07-12
                )

            [preview] => templates/foo/preview.png
        )
        . . .

When I do this:

        {foreach $templates as $template => $info}
            <pre>key:{$template} - {$info@key}:{$info|print_r}</pre>
        {/foreach}

{$template} is empty and the $info@key is the $template key. Before (with 3.1.30-dev/66) it was $template == foo and $info@key == foo ( strange though it was'nt 'info' for the 2cd)

key:foo - foo:Array
(
    [info] => Array
                  ....

Normally we do this with it:

        {foreach $templates as $template => $info}
            {templateBlock template=$template info=$info key=$info@key}
        {/foreach}

Again {$template} is empty, $info is the sub array as ever, {$key} is the $template name key.

@uwetews Can you please have a look to it and confirm it working or buggy, or Smarty behaviour having changed... Thanks!
Edit: I just checked with an older 3.1.30-dev/66 which worked ok. So it may be a behaviour change in @key chaining or a bug. If being a behaviour change, I could use $key instead of $template though. In this case it should be noted somewhere, since it breaks custom k=>v usage. But I think it is a bug and somehow related to 0fcac38 or so. [Tested with PHP 5.6.23]

@ophian
Copy link
Author

ophian commented Jul 17, 2016

Ok let me resume. I think its not a bug and we just made it wrong. Its either AS $name, or AS $k => $v. Accessing @key is different, while you have to make youself clear about the internal structure of results.
We better go this way, which is simpler and more clear

        {foreach $templates as $template}
            {templateBlock template=$template key=$template@key}
        {/foreach}

I'll close this for now. Sorry for the hassle.

But there is one little question left. We don't need this in this case, but, is there a way to get the subarray key name (which is 'info' here, but could be something dynamically set) by the foreach command and loop only? {$template.info@key} or such does not work.

@ophian ophian closed this as completed Jul 17, 2016
uwetews added a commit that referenced this issue Jul 18, 2016
@uwetews
Copy link
Contributor

uwetews commented Jul 18, 2016

Indeed it was a bug that when a key variable and item@key both have been used the key variable was not updated. this is now fixed.

$key and $item@key have the same meaning

So I don't understand why item@key should show 'info'?

@ophian
Copy link
Author

ophian commented Jul 18, 2016

OK thanks. That indeed returns to the behaviour before.

So I don't understand why item@key should show 'info'?

It should not and is not what I meant. Its ok for the main arrays key, but my question after all was about the inner subarrays key name. If there is a way to get this with foreach only... eg item.item@key or something.

@uwetews
Copy link
Contributor

uwetews commented Jul 18, 2016

I still have problems to understand.
if item contains the inner subarray iit can be an array with several keys.
as long as we do not enter a foreach on the subarray there is no current key of it.

@ophian
Copy link
Author

ophian commented Jul 19, 2016

Yeah, forget it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants