0

This is for an Alfred Workflow I'm building. It's my first one.

I'm creating functions with multiple return values. I need to return an array with multiple values so I am trying to make an array in each function and then access the return values via an associative array on the index page. Is there a better way to do this? Am I doing it correctly? I can't seem to get it to output.


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;
4

1 回答 1

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'] )
);
于 2014-03-03T01:40:28.890 回答