如何将一个数组分成不同的数组并保存到数据库中?

时间:2020-07-27 作者:Israel Santiago

我有一个简短的问题,我正在开发一个wordpress插件,需要一些帮助

我在php中从ajax获得了以下数组:

$array = [
    "car_model_1=modelo 1",
    "car_price_1=123",
    "car_brand_1=Chrysler",
    "car_year_1=2020",
    "car_type_1=Auto",
    "car_basefee_1=1234",
    "car_basemodel_1=0.543",
    "car_rcusa_1=2143",
    "car_bono_1=true",
    "car_model_2=modelo%202",
    "car_price_2=3453",
    "car_brand_2=Chrysler",
    "car_year_2=2020",
    "car_type_2=Auto",
    "car_basefee_2=435",
    "car_basemodel_2=0.643",
    "car_rcusa_2=2534",
    "car_model_3=modelo%203",
    "car_price_3=4355",
    "car_brand_3=Chrysler",
    "car_year_3=2020",
    "car_type_3=Auto",
    "car_basefee_3=3454",
    "car_basemodel_3=0.5643",
    "car_rcusa_3=2345",
    "car_bono_3=true"
];
我需要在尽可能多的数组中分离它_x“;例如,本例中有_1“"E_2英寸;和_3英寸;在数组的每个位置的末尾,这意味着所有_1英寸;对应于将插入数据库中的1条记录,所有这些记录都以_2英寸;是另一个记录等。。。(请注意,$model2没有“car\\u bono2”,因为我使用了一个“checkbox”,所以如果选中,则为true,如果不选中,则不显示)

$model1 = [
    "car_model_1=modelo 1",
    "car_price_1=123",
    "car_brand_1=Chrysler",
    "car_year_1=2020",
    "car_type_1=Auto",
    "car_basefee_1=1234",
    "car_basemodel_1=0.543",
    "car_rcusa_1=2143",
    "car_bono_1=true",
];

$model2 = [
    "car_model_2=modelo%202",
    "car_price_2=3453",
    "car_brand_2=Chrysler",
    "car_year_2=2020",
    "car_type_2=Auto",
    "car_basefee_2=435",
    "car_basemodel_2=0.643",
    "car_rcusa_2=2534",
];

$model3 = [
    "car_model_3=modelo%203",
    "car_price_3=4355",
    "car_brand_3=Chrysler",
    "car_year_3=2020",
    "car_type_3=Auto",
    "car_basefee_3=3454",
    "car_basemodel_3=0.5643",
    "car_rcusa_3=2345",
    "car_bono_3=true"
];
这是我必须在php中保存数据的函数:

    /* sending data to custom table */
    if (cotizador_query_select($post_id) == null) {
        $wpdb->insert($table_name, array(
            \'post_id\' => $post_id,
            \'car_model\' => $car_model,
            \'car_price\' => $car_price,
            \'car_brand\' => $car_brand,
            \'car_year\' => $car_year,
            \'car_type\' => $car_type,
            \'car_basefee\' => $car_basefee,
            \'car_basemodel\' => $car_basemodel,
            \'car_rcusa\' => $car_rcusa,
            \'car_bono\' => $car_bono,
        ));
    } else {
        $wpdb->update($table_name, array(
            \'car_model\' => $car_model,
            \'car_price\' => $car_price,
            \'car_brand\' => $car_brand,
            \'car_year\' => $car_year,
            \'car_type\' => $car_type,
            \'car_basefee\' => $car_basefee,
            \'car_basemodel\' => $car_basemodel,
            \'car_rcusa\' => $car_rcusa,
            \'car_bono\' => $car_bono,
        ), array(\'post_id\' => $post_id));
    }

1 个回复
SO网友:mmeehan

我是新来的,我认为@mozboz在哪里问这个问题是正确的,但是你可以循环数组并分解键,然后使用最后一个值创建你想要的数组。