填充关联数组 [英] Populating associative arrays

查看:152
本文介绍了填充关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时有什么错 $数据[$关键] [$ VAL] = []; ,因为在本地主机上它的作品不错,但在服务器上它不(字preSS显示空白页,我注意到,它打破​​只在该行)

  $个月=阵列('sijecanj'=>'Siječanj','veljaca'=>'Veljača','ozujak'=>'Ožujak','travanj = GT;'Travanj'...);
$份=阵列('Plodovi','Korijen','高良','Sjeme');$数据=阵列();
的foreach($个月$关键=> $ VAL){
    $数据[$关键] [$ VAL] = [];
    的foreach($部分为$一部分){
        如果(has_term($一部分,$键)){
            array_push($数据[$关键] [$ VAL],$部分);
        }
    }

}

我所试图做的是有每个月的数组,如果它与特定部分的一些价值观,同时有关的键值对了好几个月。 (我需要密钥塞从字preSS数据库和价值获取的数据将被呼应'),这样,最终我得到这样

  $数据= [
    Siječanj'=> ['Plodovi','Korijen'],
    Kolovoz'=> ['高良','Sjeme']
]


解决方案

这无关你的文本编辑器。它与你的PHP版本做。你的开发环境中运行PHP 5.4+和生产环境中运行PHP 5.3或以上这确实的的支持短数组语法(即 [] )这是在PHP 5.4引入的。

所以

  $数据[$关键] [$ VAL] = [];

要成为

  $数据[$关键] [$ VAL] =阵列();

成为向后兼容。

Is there something wrong with $data[$key][$val] = [];, because on localhost it works nice but on server it doesnt (Wordpress shows blank page, and I noticed that it breaks just on that line)

$months = array('sijecanj' => 'Siječanj', 'veljaca' => 'Veljača', 'ozujak' => 'Ožujak', 'travanj' => 'Travanj'...);
$parts = array('Plodovi' ,'Korijen', 'Kora', 'Sjeme'); 

$data = array();
foreach($months as $key => $val) {
    $data[$key][$val] = [];
    foreach($parts as $part) {
        if( has_term( $part, $key ) ) {
            array_push($data[$key][$val], $part);
        }   
    }

}

What I am trying to do is to have an array for each month if it has some values with specific parts, while also pertaining key-value pairs for months. (I need key as slug for fetching data from Wordpress database and value will be echoed'), so that in the end I get something like this

$data = [
    'Siječanj' => ['Plodovi', 'Korijen'],
    'Kolovoz' => ['Kora', 'Sjeme']
]

解决方案

This has nothing to do with your text editor. It has to do with your PHP versions. Your development environment is running PHP 5.4+ and your production environment is running PHP 5.3 or older which does not support short array syntax (i.e. []) which was introduced in PHP 5.4.

So

$data[$key][$val] = [];

needs to become

$data[$key][$val] = array();

to become backwards compatible.

这篇关于填充关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆