Click here to Skip to main content
14,391,734 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello everybody, actually i've got the following data from android application as string and i converted it to array and now I want to insert it in MySql table using php my data is as the following:

Name=Steve:Phone=+0154155423,Name=Jack:Phone=+013935175713,Name=Sarah:Phone=012376479150

but i want to split that data into two variables like "$name,$phone" to insert it to the database

What I have tried:

if(isset($_POST['data'])){
$value = $_POST['data']; //i got the data here
$values = explode(',',$value); //expolde it 

foreach($values as $item) {

    $item = str_replace('[', '', str_replace(']', '', $item)); //removed spaces
    $item = str_replace(' ','',$item);

//after this i dont know how to split it to insert them in the database

    $sql = "INSERT INTO users (name, number) VALUES ('$name','$number')";
 mysqli_query($link, $sql);


      }


So how can I split my above array into two variables to insert them to the mysql database.

Thanks in advance.
Posted
Updated 16-Jan-17 0:32am
Rate this:
Please Sign up or sign in to vote.

Solution 1

So your problem is how to get to the innest part of the respective data items, that can be achieved by exploding the inner items using different delimiters, see example:
<?php 
$value = "Name=Steve:Phone=+0154155423,Name=Jack:Phone=+013935175713,Name=Sarah:Phone=012376479150"; //expolde it 
$values = explode(',',$value);
    
print_r($values);

echo '<br>';

foreach($values as $item){
    
    $pairs = explode(':',$item);
    
    print_r($pairs);
	
	echo '<br>';
    
    $namePair = explode('=',$pairs[0]);
                 
    $nameValue = $namePair[1];
                 
    print_r($nameValue);
	
	echo '<br>';
    
    $phonePair = explode('=',$pairs[1]);
    
    $phoneValue = $phonePair[1];
                  
    print_r($phoneValue);
	
	echo '<br>';
}
    
?>
and the outcome:
Array ( [0] => Name=Steve:Phone=+0154155423 [1] => Name=Jack:Phone=+013935175713 [2] => Name=Sarah:Phone=012376479150 ) 
Array ( [0] => Name=Steve [1] => Phone=+0154155423 ) 
Steve
+0154155423
Array ( [0] => Name=Jack [1] => Phone=+013935175713 ) 
Jack
+013935175713
Array ( [0] => Name=Sarah [1] => Phone=012376479150 ) 
Sarah
012376479150
Study the code to figure how to pick out the respective name and phone values for insertion to mysql.
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 2

Try this pure MySQL solution:

SELECT
  TRIM(LEADING 'Name=' FROM SUBSTRING_INDEX(NamePhonePair, ":", 1))   AS Name,
  TRIM(LEADING 'Phone=' FROM SUBSTRING_INDEX(NamePhonePair, ":", -1)) AS Phone
FROM
  (SELECT
     SUBSTRING_INDEX(NamePhonePairs, ',', 1) AS NamePhonePair
   FROM
     (SELECT
        TRIM(LEADING ',' FROM TRIM(LEADING SUBSTRING_INDEX(DATA, ',', Indx - 1) FROM DATA)) AS NamePhonePairs
      FROM
        (SELECT
           *
         FROM
           (SELECT
              "Name=Steve:Phone=+0154155423,Name=Jack:Phone=+013935175713,Name=Sarah:Phone=012376479150"
              AS DATA
           ) AS t1
           CROSS JOIN
           (SELECT
                         1 AS Indx
                       UNION ALL
                       SELECT
                         2
                       UNION ALL
                       SELECT
                         3
                       #UNION ALL
                       #SELECT
                       #  4
                       # ...
           )
           AS counter)
        AS t2)
     AS t3)
  AS t4
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100