Populating associative arrays

2019-03-01 13:11发布

问题:

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']
]

回答1:

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.