这是我正在建造的Alfred Workflow。这是我的第一个。
我正在创建具有多个返回值的函数。我需要返回一个具有多个值的数组,所以我试图在每个函数中创建一个数组,然后通过索引页面上的关联数组访问返回值。有一个更好的方法吗?我做得对吗?我似乎无法将其输出。
的functions.php
function firstRun()
{
// Check to make sure the data folders exist; otherwise, make them.
check_data_folder( $w->data() );
// Define the settings file path.
$settings_file = $w->data() . "/settings.json";
// Checks to see if the settings file exists and is valid
$check = check_settings( $settings_file );
// Check for user error
if ( $check === 'user' ) {
// Username not set, so, make them set it.
call_set_username();
die();
}
// Check for password error
if ( $check === 'password' ) {
// No password was set, so make them set it.
call_set_password();
}
// ------------------------------------------------------------------
// DID I DO THIS CORRECTLY?
$output =
[
// Read the settings file into an associative array so that we can use it.
'settings' => json_decode( file_get_contents( $settings_file ) , TRUE );
// grab the username
'username' => $settings['username'];
// grab and decrypt the password
'password' => decrypt_string( $settings['password'] );
];
return $ouput;
// ------------------------------------------------------------------
}
/**
* Pulls the Data out of the JSON file and stores it in an array called $gists
*
* @return boolean TRUE
*/
function json_to_array( $data )
{
// ------------------------------------------------------------------
// DID I DO THIS CORRECTLY?
$output =
[
// Cache is still fresh, so why not use it?
'gists' => json_decode( file_get_contents( $data ) , TRUE ),
// use the cache
'usecache' => TRUE
];
return $ouput;
// ------------------------------------------------------------------
}
的index.php
// Run through an initial check to make sure
$firstRun = firstRun();
// 1: The data files and folders exist
// 2: Settings file exists and set it as an associative array
$settings = $firstRun['settings'];
// 3: The User has input their Username
$username = $firstRun['username'];
// 4: The User has input their Password
$password = $firstRun['password'];
// If the data folders & files exist locally
if ( check_data_folder( $w->data() ) ):
$data = $w->data() . "/gist-cache.json";
// And they are not fresh
if (! is_fresh( $data ) ):
// Then Update the local gists
update_local_data();
// Otherwise
else:
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// DID I DO THIS CORRECTLY?
// Store the output into an associative array called $array
$array = json_to_array( $data );
// Then store the gist data into an array called $gists
$gists = $array['gists'];
// And relay whether the local cache was used or not
$usecache = $array['usecache']; // Boolean
// return $array; // (A) Do I need to return this array like this?
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
endif;
endif;
答案 0 :(得分:1)
使用逗号而不是分号进行数组声明。您应该使用array()语法与较低版本的PHP兼容。
$output = array(
// Read the settings file into an associative array so that we can use it.
'settings' => json_decode( file_get_contents( $settings_file ) , TRUE ), // <- Commas not semicolons
// grab the username
'username' => $settings['username'], // <- Commas not semicolons
// grab and decrypt the password
'password' => decrypt_string( $settings['password'] )
);