admin管理员组文章数量:1327660
I have a quick question, I'm developing a wordpress plugin and need some help
I have the following array which I got in php from 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"
];
I need to separate that in as many arrays as the "_x" says, example in this case there are "_1", "_2" and "_3" at the end of each position of the arrays, which means that all the "_1" correspond to 1 record that will be inserted in the database, all those that end in "_2" are another record, etc... (notice that $model2, doesn't have "car_bono2" because I use a "checkbox" for that so if is checked, it will be true, if not, just don't appear)
$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"
];
This is the function I have to save data in 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));
}
I have a quick question, I'm developing a wordpress plugin and need some help
I have the following array which I got in php from 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"
];
I need to separate that in as many arrays as the "_x" says, example in this case there are "_1", "_2" and "_3" at the end of each position of the arrays, which means that all the "_1" correspond to 1 record that will be inserted in the database, all those that end in "_2" are another record, etc... (notice that $model2, doesn't have "car_bono2" because I use a "checkbox" for that so if is checked, it will be true, if not, just don't appear)
$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"
];
This is the function I have to save data in 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));
}
Share
Improve this question
asked Jul 27, 2020 at 3:38
Israel SantiagoIsrael Santiago
112 bronze badges
1
- You may get an answer to your quesiton here, but even though this is on your Wordpress site, this isn't specifically a Wordpress problem. It's only about how to write PHP to process your array, so should be posted on e.g. stackoverflow. – mozboz Commented Jul 27, 2020 at 8:14
1 Answer
Reset to default 0I am new here and I think @mozboz is correct on where to ask this question, but you have could loop through the array and explode the keys and use the last value to create the array you want.
本文标签: phphow to separate an array into different arrays and save them into db
版权声明:本文标题:php - how to separate an array into different arrays and save them into db? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742227624a2436618.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论