Jump to content

Passing multi-dimensionnal array to javascript?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
by0logic

by0logic

    Newbie

  • Members
  • PipPip
  • 19 posts
I've read somewhere that it can't be done but I'm not certain yet.
My initial test returned 'Array' instead of the value.

Any ideas? :)

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
Heh, there has never been a more perfect function for you:
PHP: json_encode - Manual

JSON is "JavaScript Object Notation" in case you do not know already, it is used for the very reason of transfering/serializing data in a portable method.

Some more info on the format: JSON in JavaScript
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
by0logic

by0logic

    Newbie

  • Members
  • PipPip
  • 19 posts
I took a minute and came back to delete my thread, thinking i should experiment some more but you sir have the answer to all my questions.

Another huge tank to you! ^^

Lol, yeah, have a tank.

#4
by0logic

by0logic

    Newbie

  • Members
  • PipPip
  • 19 posts
mmm. so I hate to come running here for help everytime I hit a wall but ... I still do.
so I believe json_encore() to be exactly what I need but it turns out the server is using PHP 5.0.4 with Microsoft-IIS/6.0 and json wasn't implemented yet.

I took a look at possible modules but I think its no help because I don't have any physical and limited virtual access to the server, I simply upload through FTP.

Any tips is, again, very appreciated. Otherwise I'll work around the problem.

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
The comments of the json_encode page on php.net include useful comments, one of them is a custom made function for older PHP that should work instead, maybe try this:
/**
 * Converts an associative array of arbitrary depth and dimension into  JSON representation.
 *
 * NOTE: If you pass in a mixed associative and vector array, it will  prefix each numerical
 * key with "key_". For example array("foo", "bar" => "baz") will be  translated into
 * {'key_0': 'foo', 'bar': 'baz'} but array("foo", "bar") would be  translated into [ 'foo', 'bar' ].
 *
 * @param $array The array to convert.
 * @return mixed The resulting JSON string, or false if the argument was  not an array.
 */
function array_to_json( $array ){

    if( !is_array( $array ) ){
        return false;
    }

    $associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) ));
    if( $associative ){

        $construct = array();
        foreach( $array as $key => $value ){

            // We first copy each key/value  pair into a staging array,
            // formatting each key and value properly as we go.

            // Format the key:
            if( is_numeric($key) ){
                $key = "key_$key";
            }
            $key = "'".addslashes($key)."'";

            // Format the value:
            if( is_array( $value )){
                $value = array_to_json( $value );
            } else if( !is_numeric( $value ) || is_string( $value ) ){
                $value = "'".addslashes($value)."'";
            }

            // Add to staging array:
            $construct[] = "$key: $value";
        }

        // Then we collapse the staging  array into the JSON form:
        $result = "{ " . implode( ", ", $construct ) . " }";

    } else { // If the array is a vector  (not associative):

        $construct = array();
        foreach( $array as $value ){

            // Format the value:
            if( is_array( $value )){
                $value = array_to_json( $value );
            } else if( !is_numeric( $value ) || is_string( $value ) ){
                $value = "'".addslashes($value)."'";
            }

            // Add to staging array:
            $construct[] = $value;
        }

        // Then we collapse the staging  array into the JSON form:
        $result = "[ " . implode( ", ", $construct ) . " ]";
    }

    return $result;
}
There is also a drop in replacement for older PHP 4 in the PEAR::Compat package (requires uploading a few files, there is a working json_encode fuction there):

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.