0

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)); } 
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.com.
    – mozboz
    CommentedJul 27, 2020 at 8:14

1 Answer 1

0

I 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.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.