Populating associative arrays

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);
        }   
    }

}

Read More

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

Related posts

Leave a Reply

1 comment

  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.