0

I'm looking to use Ajax to delete the key [1] from the following array which is stored in a custom user field and then re-save it...

I am trying to do this by using unset($array[$key]) then saving the updated values but without any success I'm afraid.

TYI for your help...

Array (

  [0] => Array (
    [dna_cal_when] => 1576926000
    [dna_cal_position_id] => 
    [dna_cal_candidate_id] => 
  )

  [1] => Array (
    [dna_cal_when] => 1576927800
    [dna_cal_position_id] => 
    [dna_cal_candidate_id] => 
  )

  [2] => Array (
    [dna_cal_when] => 1576929600
    [dna_cal_position_id] => 
    [dna_cal_candidate_id] => 
  )

) 

Here is the function...

add_action( 'wp_ajax_dna_nuke_diary_slots', 'dna_nuke_diary_slots' );

function dna_nuke_diary_slots() {

  $user   = $_POST['user']; 
  $stamp  = array($_POST['stamp']);

  // values for these two variables are retrieved from custom data attribute in following html and ajax call

  $stamps = !empty(get_user_meta( $user, 'dna_cal', true)) ? get_user_meta( $user, 'dna_cal', true) : array();
  $key    = array_search($stamp, array_column($stamps, 'dna_cal_when'));

  unset($stamps[$key]);

  $new_stamps   = $stamps;
  $sort_stamps  = array_values($new_stamps);

  update_user_meta( $user, 'dna_cal', $sort_stamps);

  wp_die();

}

FYI - following is the Ajax call and a sample of the HTML

$('.slot').on('click', 'a.booked', function(e) {
  e.preventDefault();

  var user   = $('#dna_day').attr( 'data-user' );
  var stamp  = $(this).attr( 'data-timestamp' );

  $(this).removeClass('booked').addClass('nuked');

  $.ajax({
    type: 'POST',
    url: ajax_object.ajaxurl,
    data: {
      action: 'dna_nuke_diary_slots',
      user: user,
      stamp: stamp
    }
  });

});


<div id="dna_day" class="dna-day" data-user="256">
  <div id="dna_slots" class="slots">
    <div class="slot"><a class="booked" data-timestamp="1576926000" href="#"></a></div>
    <div class="slot"><a class="booked" data-timestamp="1576927800" href="#"></a></div>
    <div class="slot"><a class="booked" data-timestamp="1576929600" href="#"></a></div>
  </div>
</div>
2
  • 1
    can you do a print_r($stamps); and a print_r($key); and post the results? Both before the unset Commented Dec 30, 2019 at 0:08
  • Thank you Cornel Commented Dec 31, 2019 at 10:40

1 Answer 1

2

Currently you can not unset key because you can not find proper key from array. The issue is that you did $stamp = array($_POST['stamp']); as array which is wrong. you have to remove array() from $stamp, so which look like $stamp = $_POST['stamp'];. then you can search value in column from array and unset it. I have tested below code which is working properly.

add_action( 'wp_ajax_dna_nuke_diary_slots', 'dna_nuke_diary_slots' );

function dna_nuke_diary_slots() {

  $user   = $_POST['user']; 
  $stamp  = $_POST['stamp']; // here you have to remove array();

  // values for these two variables are retrieved from custom data attribute in following html and ajax call
  $stamps = !empty(get_user_meta( $user, 'dna_cal', true)) ? get_user_meta( $user, 'dna_cal', true) : array();

  $key    = array_search($stamp, array_column($stamps, 'dna_cal_when'));

  unset($stamps[$key]);

  $new_stamps   = $stamps;
  $sort_stamps  = array_values($new_stamps);

  update_user_meta( $user, 'dna_cal', $sort_stamps);

  wp_die();
}
1
  • 1
    Winner, winner, chicken dinner! Thank you Chetan :) Commented Dec 31, 2019 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.