Remove data from an array and insert into the table

Asked

Viewed 819 times

1

I include in a array, user informed data. But, I use only one array to store data from two fields, where I use "P"(And the entered data) for the first field and "S"(And the data entered) for the second field. Thus the data is out of order on array. Example:

array_dados=[P01000213, P154878946, S797465464,P454464654,S48897874, ...]

So I’d like to store in table do banco the information of array .. But, "P" and "S" are different fields in table and I only use the letters before the numbers to know which fields they will go to!

My doubt ... " How to remove these letters (P and S) and store the data in its proper fields in the table?"

Table I need to store the fields:

CREATE TABLE sai_info_dados
(
  seq_info integer NOT NULL DEFAULT nextval('sam_cad_info'::regclass),
  tx_num_prim character varying(12) NOT NULL,
  tx_num_secu character varying(15) NOT NULL
)
  • Why don’t you make your job easier and create the multidimensional array? Sort of: $array_dados = array(array('P' => '01000213', 'S' => ''),array('P' => '154878946', 'S' => '797465464'),array('P' => '454464654', 'S' => '48897874')); thus you guarantee data parity.

2 answers

2

To separate the items, you can do something like this:

$tabela_p = array();
$tabela_s = array();

for each ( $array_dados as $item ) {
   if( strtoupper( $item{0} ) == 'P' ) {
       $tabela_p[] = substr( $item, 1 );
   } else {
       $tabela_s[] = substr( $item, 1 );
   }
}

After that loop, you will have two separate arrays with the respective items, and without the letters.

As you have not posted the desired insertion criterion, here are some possibilities:

  • You can use a foreach in each of the $tabelas;

  • you can change the insertion in the arrays by the respective INSERTs;

  • If the result is symmetric, (one P for each S), you can insert in the same table row with a loop:

    for( $i = 0; $i < count( $tabela_p ); $i ++ ) {
       // 'INSERT INTO ... VALUES( '.$tabela_a[$i].', '.$tabela_s[$i].' )'
    }
    

    (adjustment as the actual case)

  • @ Bacco thank you so much for the information! your explanation helped me on the basis of being able to separate the fields for insertion!

2

Use replace() to remove the first character of $item this way you take the 'clean' value to write to the bank($valor). Then to check which field to save to use $item[0] which takes the first character.

<?php

$arr = array('P01000213', 'P154878946', 'S797465464','P454464654','S48897874');

foreach($arr as $item){
    $valor = substr($item, 1);

    if($item[0] == 'S'){
        echo 'INSERT INTO tabela (s_campo) .... '. $valor  .'<br>';
    }else if($item[0] == 'P'){
        echo 'INSERT INTO tabela (p_campo) ....'. $valor .'<br>';
    }
}
  • thank you very much for the information, I had knowledge of substr(), but did not know how to use correctly!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.