将两个数组合并为一个在php中不断给我错误

Posted

技术标签:

【中文标题】将两个数组合并为一个在php中不断给我错误【英文标题】:Merge two arrays into one continually gives me error in php 【发布时间】:2021-11-15 02:29:06 【问题描述】:

对于一个基于 wordpress 的房地产广告客户网站,我开发了一个通过电子邮件向订阅客户发送广告的系统。

为了避免多次发送相同的广告,我创建了一个标记系统,突出显示广告是否已经发送。

要注册发送的广告,我使用 ID 并在特定列中注册使用复选框选择的广告的 ID,数据通过 javascript 函数传输。

这里是jquery部分代码:

    var checkboxVals = $('.listing_multi_id');       // This for id of post(advert) checked
    var email_to = $('#lead_email').val();          // This tell me the email to send. 
    var lead_id = $('#lead_id').val();              // This tell me the col ID in DB of my client
    var already_ID = $('#annunci_inviati').val();   //This tells me which ids are already present in the database row as sent

    var vals = $('.listing_multi_id:checked').map(function() return this.value;).get().join(',') // This create array of ids of post(advert) checked


   // And all information is sent through json post 
     $.ajax(
                    type: 'POST',
                    dataType: 'json',
                    url: ajaxurl,
                    data: 
                        'action': 'houzez_match_listing_email',
                        'ids': vals,
                        'email_to': email_to,
                        'id_lead': lead_id,
                        'already_inviati': already_ID,
                    ,''''

这里是php部分代码:

            // I have omitted all the information that does not concern the question, the function I am using is much more complex and structured than the one you see

      $listing_ids = sanitize_text_field($_REQUEST['ids']); // Array with the ids of post

      $already_inviati1 = sanitize_text_field($_REQUEST['already_inviati']); // Array with the ids of post already sent 


    // Here transform the array in array with INT ids
    $strings_array = explode(',', $already_inviati1);
    
    foreach ($strings_array as $each_number=>$id) 
        $already_inviati2[] = $id;
     
    // up to here

   // Here check if the is the first sent or not

     if (isset($already_inviati1)) 
        $merged = array_merge($already_inviati2,$listing_ids);

        $annunci_inviati = maybe_serialize($merged);
     else
    $annunci_inviati = maybe_serialize($listing_ids);

    // Here save the value in database
    
    $id_cliente = $_POST['id_lead']; 

        $data_table= $wpdb->prefix . 'crm_enquiries';
        $data = array(
            'annunci_inviati' => $annunci_inviati 
        );

        $format = array(
            '%s'
        );

        $where = array(
            'lead_id' => $id_cliente
        );

        $where_format = array(
            '%d'
        );

       $wpdb->update( $data_table, $data, $where, $format, $where_format ); 

       // FINE SCRIPT

如您所见,如果是第一次发送邮件,则脚本有效,因此如果该列为空。

在第二次发送时,它出错了,数组被合并,但是当我去检查它的值时,它变成了这样:

// I use a hidden input in html to check ids already sent

<input type="hidden" id="annunci_inviati" value="array (
 0 => 0, // Old IDs are lost by becoming numbers
 1 => 1, // Old IDs are lost by becoming number
 2 => 2, // Old IDs are lost by becoming number
 3 => 0, // Old IDs are lost by becoming number
 4 => 53161, // the new id selected
 5 => 53153, // the new id selected
 6 => 53144, // the new id selected
 )">

我该如何解决?

【问题讨论】:

在您的 PHP 中,$listing_ids 似乎是一个字符串而不是一个数组。 【参考方案1】:

您正在尝试将字符串与数组合并。请尝试以下方法,以确保输入值正确转换为数组。

/**
 * @var array Array of listing ID's.
 */
$listing_ids = !empty( $_REQUEST[ 'ids' ] ) ? array_map( 'intval', explode( ',', $_REQUEST[ 'ids' ] ) ) : [];

/**
 * @var array Array of ID's for post already sent.
 */
$already_sent = !empty( $_REQUEST[ 'already_inviati' ] ) ? array_map( 'intval', explode( ',', $_REQUEST[ 'already_inviati' ] ) ) : [];

/**
 * @var array Merge the listing ID's and already sent ID's for storing.
 */
$merged = array_unique( array_merge( $listing_ids, $already_sent ) );

/**
 * @var int|null The client record to update.
 */
$id_cliente = !empty( $_POST[ 'id_lead' ] ) ? (int)$_POST[ 'id_lead' ] : null; 

if ( $id_cliente ) 
    $data_table = $wpdb->prefix . 'crm_enquiries';

    $wpdb->update( $data_table, [
        'annunci_inviati' => maybe_serialize( $merged )
    ], [
        'lead_id' => $id_cliente
    ], [
        '%s'
    ], [
        '%d'
    ] ); 

【讨论】:

我尝试了您的解决方案,但它总是给我错误“array_merge (): Expected parameter 1 as array, null given” 参数 1 是 $ Listing_ids =!空($_POST ['ids'])? array_map('intval', explode(',', $_POST ['ids'])): [];

以上是关于将两个数组合并为一个在php中不断给我错误的主要内容,如果未能解决你的问题,请参考以下文章

Java中如何把两个数组合并为一个

如何将两个有序数组合并为一个有序数组,用函数做,一个循环搞定?

JAVA怎么合并两个数组

如何将两个数组合并为一个相应的2d数组?

将两个数组合并为一个 Laravel PHP [重复]

怎么用c语言将两个数组并起来?