数组在AJAX请求中变为多维

时间:2019-02-25 20:55:15

标签: php arrays ajax wordpress

因此,我正在WordPress中创建一个函数,该函数计算并设置用户会话并将其值存储在用户的本地存储中。我可以使用Cookie使其完美运行,并且在本地托管站点时,由于某种原因,当我将其上载到暂存站点时,该站点不起作用。因此,我正在尝试使用另一种方法来实现此功能,并决定改为使用本地存储。

该函数生成的Array值有问题,我几乎整天都在尝试调试问题。它正在生成多维而不是单个维。

这是我的功能代码:

 Notification notification = new Notification.Builder(getApplicationContext())
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setContentTitle("Track title")
                .setContentText("Artist - Album")
                .setOngoing(true)

                .addAction(R.drawable.ic_add_black_24dp, "fwd", pi)
                .addAction(R.drawable.ic_android_black_24dp, "fwd", pi)
                .addAction(R.drawable.ic_archive_black_24dp, "fwd", pi)
                .addAction(R.drawable.ic_arrow_back_black_24dp, "fwd", pi)
                .addAction(R.drawable.ic_aspect_ratio_black_24dp, "fwd", pi)
                .addAction(R.drawable.ic_fast_forward_black_24dp, "fwd", pi)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.vectors_525058875))
                .setAutoCancel(false)


                .build();
        notificationManager.notify(300, notification);

以下是此函数生成的屏幕截图

Array

这是一个示例JSON

function monitor_post_views() {
  $page_id = 'page' . $_POST['page_id'];
  $timestamp = time();  
  // 30 minutes timeout
  $timeout = 1800;
  // Serves as my guide for debugging, will not include in the final code
  $message = '';


  if ( ! empty($_POST['page_id_array']) ) {
     //Checks if values from local storage exist

     //Gets the stored Array coming from AJAX call
     $page_id_array[] = json_decode(stripslashes($_POST['page_id_array']), true);

     if ( in_array_r($page_id_array, $page_id) ) {
        //Check if current page is found in array

        $message = 'FOUND IN ARRAY CHECKING !!!!';

        $temp= [];

        $page_id_array_temp = array('id' => $page_id, 'expiration' => $timestamp, 'message' => $message);

        $temp = $page_id_array_temp;

        //Pushes the generated array inside the $page_id_array
        array_push($page_id_array, $temp);

        print_r(json_encode($page_id_array));

        foreach ( $page_id_array as $page ) {
            //If page is in array, check if the session is expired, if not, do nothing, if expired, update and then run the view function
        }
    } else {
      // ID Not found in Array, Insert a new entry

      $message = 'ID NOT FOUND IN ARRAY, CREATING ENTRY !!!';

      $temp = [];

      $page_id_array_temp = array('id' => $page_id, 'expiration' => $timestamp, 'message' => $message);

      $temp = $page_id_array_temp;

      //Pushes the generated array inside the $page_id_array
      array_push($page_id_array, $temp);

      print_r(json_encode($page_id_array));

      //Set post view function here base on $_POST['page_id']

    }

  } else {
    //Not found in local storage, need to create one

    $message = 'CREATING A NEW ENTRY !!!!';

    $temp = [];

    $page_id_array = array('id' => $page_id, 'expiration' => $timestamp, 'message' => $message);

    $temp = $page_id_array;

    print_r(json_encode($temp));

    //Set post view function here base on $_POST['page_id']

  }

  wp_die();

}
add_action('wp_ajax_monitor_post_views', 'monitor_post_views');
add_action('wp_ajax_nopriv_monitor_post_views', 'monitor_post_views');

我试图生成一个一维,但最终得到了这个。 有什么想法吗?预先感谢

1 个答案:

答案 0 :(得分:1)

问题是您创建数组的次数过多:

将$ page_id_array和$ page_id_array_temp更改为

 $page_id_array=new \stdClass();//no need to declare as an array

替换

$page_id_array_temp = array('id' => $page_id, 'expiration' => $timestamp, 'message' => $message);

使用

 $page_id_array->id=$page_id;
 $page_id_array->expiration=$timestamp;
 $page_id_array->message=$message;

也改变

$temp = []; 

您可以直接使用它 //无需将$ temp声明为数组

 $temp=$page_id_array;