【问题标题】:Pushing data into an array that's inside of an array将数据推送到数组内部的数组中
【发布时间】:2018-07-07 09:44:21
【问题描述】:

所以这可能以前被问过,或者我可能只是以一种完全奇怪的方式使用数组。无论如何,我想做的是有一个数组,比方说.. $replacements = array(); 这个数组包括 keysreplacements 的所有数据时间>。不知道如何描述它,但这就是这个想法的来源:click - 现在,假设我已经得到了如上所述的这个数组,我试图在我的函数内部添加到数组,该选项允许您将一个键限制为一组动态页面。这就是我想象的数组的样子:

array() {
    ["key1"] => "result"
    ["key2"] => "result2"
    ["key3 etc"] => "result3 etc"
}

这将是一个没有指定任何页面限制的键数组,这就是我为页面受限键附加其他数组时想象的数组。

array() {
    ["key1"] => "result"
    ["key2"] => "result2"
    ["key3 etc"] => "result3 etc"
    "homepage" = array() {
        ["home_key1"] => "this is a key in the homepage only"
        ["two_page_restricted"] => "this is a replacement in two restricted pages only"
    },
    "newspage" = array() {
        ["two_page_restricted"] => "this is a replacement in two restricted pages only"
    }
}

我不确定到目前为止我所说的是否有任何意义,但我想你明白了。这就是我到目前为止所获得的基本密钥替换:

function addTranslation($key, $replacement, $restricted = null) {
    if($restricted == null)
        array_push($this->translations, $key, $replacement);
    //else 
        //array_push($this->translations, )
    }

最后,我想要完成的是,如果 $restricted 不是 null 然后将其附加到 $this->translations 不会干扰其他键。在此先感谢您的帮助。

编辑: 如果有帮助,这就是我使用该功能的方式:

对于两个页面: $class->addTranslation("{key}", "this is a key", array("homepage", "newspage");

对于任何页面: $class->addTranslation("{key}", "this is a key");

编辑2: 为了说明,这是 PHP 而不是 JavaScript。

【问题讨论】:

  • 我不确定我是否理解所有内容,但使用该系统,如果您有一个名为“主页”的页面限制,您将无法拥有名称为“主页”的密钥所有页面。是这样吗?
  • 我想要一个 key 来限制替换,比如说 homepage。假设我有一个名为{title} 的密钥,其替换值为This is a title.,受限页面为array("homepage", "newspage");。然后我想在纯 HTML 中使用 {title} 并在主页和新闻页面中替换它,但即使在 HTML 中使用了密钥,也没有其他地方。
  • 好的,根据我收集到的信息,为了处理这些翻译,您计划使用一个类,它有一个名为 addTranslations 的方法,并且能够处理一些 HTML。是这样吗?

标签: php arrays dimensions


【解决方案1】:

要回答主要问题,一种可以同时在数组属性的根级别或同一数组的多个子级别处理推送数据的方法可能如下所示:

class EntryHandler
{
    private $entries;

    function addEntry($key, $value, array $subArrayKeys = null) 
    {
        if($subArrayKeys == null)
        {
            $this->entries[$key] = $value;
            return; // Skip the subArrayKeys handling
        }

        foreach($subArrayKeys as $subArrayKey)
        {
            // Initialize the sub array if it does not exist yet
            if(!array_key_exists($subArrayKey, $this->entries))
            {
                $this->entries[$subArrayKey] = [];
            }
            // Add the value
            $this->entries[$subArrayKey][$key] = $value;
        }
    }
}

话虽如此,您已指定此附加操作不应“干扰其他键”。在这种情况下,您描述您期望的数组的方式根本行不通。 使用这种结构,您将无法拥有具有相同值的翻译键和受限页面名称。

我认为这里的正确方法是使用一致的结构,其中多维数组的每个级别都包含相同类型的数据。考虑到您的用例,您可以引入一个“默认”域,除了特定于页面的翻译之外,您还可以将其用作翻译的基础。


这是我的一个项目中的一小节课,据我所知,我根据您的用例对其进行了调整。它使用strtr,正如您为字符串处理部分链接的帖子中所建议的那样。

class Translator
{
    const DEFAULT_DOMAIN = '_default'; // A string that you are forbidden to use as a page specific domain name
    const KEY_PREFIX = '{';
    const KEY_SUFFIX = '}';

    private $translations = [];    

    public function addTranslation($key, $translation, array $domains = null)
    {
        // If no domain is specified, we add the translation to the default domain
        $domains = $domains == null ? [self::DEFAULT_DOMAIN] : $domains;
        foreach($domains as $domain)
        {
            // Initialize the sub array of the domain if it does not exist yet
            if(!array_key_exists($domain, $this->translations))
            {
                $this->translations[$domain] = [];
            }

            $this->translations[$domain][$key] = $translation;
        }
    }

    public function process($str, $domain = null)
    {
        return strtr($str, $this->getReplacePairs($domain));
    }

    private function getReplacePairs($domain = null)
    {
        // If the domain is null, we use the default one, if not we merge the default 
        // translations with the domain specific ones (the latter will override the default one) 
        $replaceArray = $domain == null 
            ? $this->translations[self::DEFAULT_DOMAIN] ?? [] 
            : array_merge($this->translations[self::DEFAULT_DOMAIN] ?? [], $this->translations[$domain] ?? []);

        // Then we add the prefix and suffix for each key
        $replacePairs = [];
        foreach($replaceArray as $baseKey => $translation)
        {
            $replacePairs[$this->generateTranslationKey($baseKey)] = $translation;
        }

        return $replacePairs;
    }

    private function generateTranslationKey($base)
    {
        return self::KEY_PREFIX . $base . self::KEY_SUFFIX;
    }
}

有了这个类,下面的代码

$translator = new Translator();
$translator->addTranslation('title', 'This is the default title');
$translator->addTranslation('title', 'This is the homepage title', ['homepage']);

$testString = '{title} - {homepage}';
echo $translator->process($testString, 'random_domain'); // Outputs "This is the default title - {homepage}"
echo '<hr/>';
echo $translator->process($testString, 'homepage');  // Outputs "This is the homepage title - {homepage}"

会输出:

This is the default title - {homepage}<hr/>This is the homepage title - {homepage}

【讨论】:

  • 啊,是的,这就是我的总结。谢谢。
【解决方案2】:

抱歉应该注意到美元符号

如果你想使用 PHP 从数组中拉出一个字段,你想这样做

$result->fields[fieldname][fieldarray];

Array_push($result);

希望我理解正确

【讨论】:

    【解决方案3】:

    您是否尝试从数组中获取结果,然后将其添加到新数组中。

    应该是这样的:

    var 结果 = firstarray.key1;

    newarray.push(结果);

    【讨论】:

    • 我应该说,这是 PHP 而不是 JavaScript。
    猜你喜欢
    • 2019-01-28
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多