duan01203 2017-10-24 06:02
浏览 129
已采纳

在提交表单之前重新排序数组索引

<input type="hidden" name="gallery[0]" value="gallery-image-1500382700392.jpeg">
<input type="hidden" name="gallery[1]" value="gallery-image-1500382700392.jpeg">
<input type="hidden" name="gallery[2]" value="gallery-image-1500382700392.jpeg">

This is the form elements.In my admin panel i have the facility to delete images in the art gallery.If I am deleting name="gallery[2]" element, data saved to database is

["gallery-image-1500382700392.jpeg", "gallery-image-1500382700392.jpeg"]

and the format is json array

But if Iam deleting name="gallery[1]" element, data saved to database is of json object format.

{"0": "gallery-image-1500295044382.jpeg","2": "gallery-image-1500295044382.jpeg"}

I think this is because array indexing is not there [0,1] so treated as just key-value pair

I there any way to re-order this indexing?

  • 写回答

1条回答 默认 最新

  • dongzu0742 2017-10-24 06:22
    关注

    You can use array_values to reset the keys of an array and Return all the values of an array.

    Here is example,

    <?php
    $a = array(
    3 => 11,
    1 => 22,
    2 => 33,
    );
    $a[0] = 44;
    
    print_r( array_values( $a ));
    ==>
    Array(
      [0] => 11
      [1] => 22
      [2] => 33
      [3] => 44
    )
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?