-3

I have the following string:

$string=John Smith - Clearner - 1234$Bob Dillan - IT Man - 2453242$Sarah Clifford - Admin - 32423423

I need to split each persons name, occupation, registration number into 3 seperate columns into MySQL, each person is seperated by '$'

Desired result:

Col1 | Col2 | Col3

John Smith | Clearner | 1234

Bob Dillan | IT Man | 2453242

Sarah Clifford | Admin | 32423423

Now I believe we need to explode the string twice by '$' and ' - '

I've managed to split '$' by doing the following:

$peeps = explode("$", $string);
foreach($peeps as $peep) {
$persons .= $peep;
}
$pnames = explode(' - ',$persons);

BUT I'm a complete newbie when it comes to exploding and the foreach loops

Any help would be amazing

4
  • Do you have any code that you can share of what you've tried so far? Commented May 15, 2017 at 8:16
  • You need to brake your string and after that | for diffrence Commented May 15, 2017 at 8:19
  • Possible duplicate of Double explode an array Commented May 15, 2017 at 8:20
  • thank you for the link, tried to apply the code but i cant get it work...could you possibly draft a working code?? Commented May 15, 2017 at 8:59

3 Answers 3

0

Given your string, you can modify it in a simple way with array_map

$string="John Smith - Clearner - 1234$Bob Dillan - IT Man - 2453242$Sarah Clifford - Admin - 32423423"

If foreach is too difficult to grasp, as you have to modify the array you are traversing, try the functional approach, maybe it will result simpler to you.

explode, as you already know create an array splitting the input string with a separator:

$partial= explode("$", $string);

will result in

$partial= [ 
  "John Smith - Clearner - 1234",
  "Bob Dillan - IT Man - 245324",
  "Sarah Clifford - Admin - 32423423"
];

you can apply the same transformation to every element in an array with array_map. The transformation is encoded into an helper function:

function detail_splitter($string) {
    return explode(" - ", $string)
}

that can be leveraged as

$result =array_map(detail_splitter, $partial);

and the result will be:

$result = [ 
  ["John Smith", "Clearner", "1234"],
  ["Bob Dillan", "IT Man", "245324"],
  ["Sarah Clifford", "Admin", "32423423"]
];

which is an array of array. Every element of the array is processed by the helper function and splitted into a three elements array. The result is used to create a new array with the processed values ready to be further processed.

Obviously you can combine the steps into a single statement:

$result = array_map( 
    function ($p) { return explode(" - ", $p); },
    explode("$", $string);
);

Since now you do not have to transform the array further, but perform a task for every array element you can revert to foreach.

Note that I changed the parameter name from $string to $p to avoid to mess with the scope (for php it will just be fine, human programmers sometimes are not at ease with shadowing variable names).

There is a function, array_walk to perform a task on every element of an array but i think this task would be easier using a simple foreach.

You can address the different persons in a such way:

// $dbc is your db connection
$sql = "INSERT INTO yourtable VALUES (?,?,?);
foreach($result as $person) {
    $stmt = $dbc->prepare($sql);
    $stmt->bind_param('s', $person[0]);
    $stmt->bind_param('s', $person[1]);
    $stmt->bind_param('s', $person[2]);
    $stmt->execute();
} 

I made some assumption with your sql statement but the mysql use it out of the scope of this question, I guess.

Sign up to request clarification or add additional context in comments.

1 Comment

This did the job! thank you very much for taking the time to helpout! everything went to plan! I to use prepared statements so this slotted in perfectly!
0

Try

`$person_array = array(); $i=0;

$peeps = explode("$", $string);

foreach ($peeps as $peep) {
$persons = explode("-", $string);

foreach ($persons as $person) {

$person[$i]['name'] = $person[0]; 
$person[$i]['occupation'] = $person[1]; 
$person[$i]['number'] = $person[2]; 
$i++;

}   // inner each loop

}   // Outer each loop`

Comments

-2

I would do this in MySQL

UPDATE [table]
SET
 [col1] = SPLIT_STR([col], '$', 1),
 [col2] = SPLIT_STR([col], '$', 2),
 [col3] = SPLIT_STR([col], '$', 3);

1 Comment

It is not the require behaviour, that is just the first part of the procedure, and can be misleading, expecially for a newbie as stated in the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.