Click here to Skip to main content
15,894,720 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 15-Jan-17 23:32pm

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.
 
Share this answer
 
v2
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900