Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 数组在json解码后删除父数组_Php_Arrays_Json_Session - Fatal编程技术网

Php 数组在json解码后删除父数组

Php 数组在json解码后删除父数组,php,arrays,json,session,Php,Arrays,Json,Session,我正在开发一个支持系统,用户可以将会话保存在数据库中,公司可以在另一台计算机上打开会话来帮助他 我的问题: Array ( [splashback] => Array ( [dimensions] => Array ( [width] => 1200 [height] => 800

我正在开发一个支持系统,用户可以将会话保存在数据库中,公司可以在另一台计算机上打开会话来帮助他

我的问题:

Array
(
    [splashback] => Array
        (
            [dimensions] => Array
                (
                    [width] => 1200
                    [height] => 800
                )

            [hole] => Array
                (
                    [choice] => false
                )

            [holes] => Array
                (
                    [0] => Array
                        (
                            [type] => Array
                                (
                                    [type] => single
                                    [name] => Single socket
                                )

                            [x] => 120
                            [y] => 300
                        )
                )
        )
)
Array
(
    [0] => Array
        (
            [splashback] => Array
                (
                    [dimensions] => Array
                        (
                            [width] => 1200
                            [height] => 800
                        )

                    [hole] => Array
                        (
                            [choice] => false
                        )

                    [holes] => Array
                        (
                            [0] => Array
                                (
                                    [type] => Array
                                        (
                                            [type] => single
                                            [name] => Single socket
                                        )

                                    [x] => 120
                                    [y] => 300
                                )
                        )
                )
        )
)
问题是,在解码json之后,我的会话数组中有一个额外的级别。(下面是手表阵列)

  • 因此,我需要在之后删除该级别(父数组)
  • 或者我需要以另一种方式保存/导入它
我的功能:

此函数用于保存会话和json_encode,并将其保存在数据库行中。然后返回一个“支持url”,公司可以打开该url来加载会话

function gtp_support_request( $referer ) {
    global $wpdb;

    $url_id         = url_to_postid( $referer );
    $request_id = time();
    $table      = $wpdb->prefix . 'support_requests';

    if( isset( $_SESSION ) ) 
        $config     = json_encode( $_SESSION );
    else 
        $config     = '';   

    $wpdb->insert( 
        $table,
        array(
            'request_id'        => $request_id,
            'config'            => $config,
            'url_id'            => $url_id,
        ),
        array( '%d', '%s', '%d' )
    );
    $request_url = get_permalink( $url_id ) . '?support_id=' . $request_id;
    echo $request_url;
}
function gtp_load_support_request() {
    global $wpdb;

    if( ! empty( $_GET['support_id'] ) ) {
        $request_id     = $_GET['support_id'];
        $table          = $wpdb->prefix . 'support_requests';
        $results        = $wpdb->get_results( "SELECT config FROM $table WHERE request_id = $request_id" );
        $config         = json_decode( $results[0]->config, true );


        $_SESSION[]     = $config;
    }
}
此函数通过URL中的支持id从数据库获取json数据,然后json_解码配置并将其加载到会话中

function gtp_support_request( $referer ) {
    global $wpdb;

    $url_id         = url_to_postid( $referer );
    $request_id = time();
    $table      = $wpdb->prefix . 'support_requests';

    if( isset( $_SESSION ) ) 
        $config     = json_encode( $_SESSION );
    else 
        $config     = '';   

    $wpdb->insert( 
        $table,
        array(
            'request_id'        => $request_id,
            'config'            => $config,
            'url_id'            => $url_id,
        ),
        array( '%d', '%s', '%d' )
    );
    $request_url = get_permalink( $url_id ) . '?support_id=' . $request_id;
    echo $request_url;
}
function gtp_load_support_request() {
    global $wpdb;

    if( ! empty( $_GET['support_id'] ) ) {
        $request_id     = $_GET['support_id'];
        $table          = $wpdb->prefix . 'support_requests';
        $results        = $wpdb->get_results( "SELECT config FROM $table WHERE request_id = $request_id" );
        $config         = json_decode( $results[0]->config, true );


        $_SESSION[]     = $config;
    }
}
之前:

Array
(
    [splashback] => Array
        (
            [dimensions] => Array
                (
                    [width] => 1200
                    [height] => 800
                )

            [hole] => Array
                (
                    [choice] => false
                )

            [holes] => Array
                (
                    [0] => Array
                        (
                            [type] => Array
                                (
                                    [type] => single
                                    [name] => Single socket
                                )

                            [x] => 120
                            [y] => 300
                        )
                )
        )
)
Array
(
    [0] => Array
        (
            [splashback] => Array
                (
                    [dimensions] => Array
                        (
                            [width] => 1200
                            [height] => 800
                        )

                    [hole] => Array
                        (
                            [choice] => false
                        )

                    [holes] => Array
                        (
                            [0] => Array
                                (
                                    [type] => Array
                                        (
                                            [type] => single
                                            [name] => Single socket
                                        )

                                    [x] => 120
                                    [y] => 300
                                )
                        )
                )
        )
)
新阵列:

Array
(
    [splashback] => Array
        (
            [dimensions] => Array
                (
                    [width] => 1200
                    [height] => 800
                )

            [hole] => Array
                (
                    [choice] => false
                )

            [holes] => Array
                (
                    [0] => Array
                        (
                            [type] => Array
                                (
                                    [type] => single
                                    [name] => Single socket
                                )

                            [x] => 120
                            [y] => 300
                        )
                )
        )
)
Array
(
    [0] => Array
        (
            [splashback] => Array
                (
                    [dimensions] => Array
                        (
                            [width] => 1200
                            [height] => 800
                        )

                    [hole] => Array
                        (
                            [choice] => false
                        )

                    [holes] => Array
                        (
                            [0] => Array
                                (
                                    [type] => Array
                                        (
                                            [type] => single
                                            [name] => Single socket
                                        )

                                    [x] => 120
                                    [y] => 300
                                )
                        )
                )
        )
)

恢复时,您可以通过不指定整个结果而是指定其中的第一个元素来解决此问题:

$_SESSION[]     = $config[0];
编辑

json\u decode
返回
\stdClass
的实例,而不是数组。这意味着您需要将其转换为数组,然后才能像数组一样访问密钥

试试这个:

$config = (array)json_decode( $results[0]->config, true );
$_SESSION[] = $config[0];

我需要这样做:

$_SESSION = $config;
而不是:

$_SESSION[] = $config;

是不是在同一个地方转储了
$config
的内容?对不起,我不知道你的确切意思。我是说,你是从哪里转储了你在问题中包含的新数组的?此时,新数组的索引肯定为0;在我的网站的标题中。请尝试在还原它之后立即放置它(
$\u SESSION=$config