0

I need to sort an exploded string to write into database.

With the code down below it works, but I was not able to detect the summary of arrays outside the foreach loop.

So $terms[0].$terms[1].$terms[2] was not correct.

Cause as example $terms[0].$terms[1].$terms[2].$terms[3] was required for 4 terms in string.

How could that be solved?

$terms = explode(";", "banana;apple;lemon");

sort($terms);

$length = count($terms);

for($x = 0; $x < $length; $x++) {

$terms[$x] = $terms[$x].'; ';
}

$sorted_terms = $terms[0].$terms[1].$terms[2]; // <<< should changed dynamically

$sql = mysqli_query($dbcon,"INSERT INTO mydatabase (terms) VALUES ('$sorted_terms')");
3
  • 2
    you have implode function wich is the opposite of explode. It makes a string out of an array, using the specified glue. And please be careful with sql injection... Commented May 29, 2018 at 10:38
  • Although I don't know your exact use case, storing more than one atomic value (term) in a a field is rarely a good idea as it violates 1NF. Using a separate table for your terms might be the better long term solution and it would make the current problem irrelevant. Commented May 29, 2018 at 11:32
  • thanks Zyigh. Have solved it with $terms = "banana;apple;lemon"; $sorted_terms = explode(";", $terms); sort($sorted_terms, SORT_STRING); $sorted_terms = implode(";", $sorted_terms); Commented May 29, 2018 at 12:03

3 Answers 3

2

Use PHP implode which is an exact opposite of explode and intended for just this sort of thing.

$terms = explode(";", "banana;apple;lemon");
sort($terms);
$sorted_terms = implode(";", $terms);

//$sorted_terms = "apple;banana;lemon";

Edit:

As you're using procedural MySQLi for your data entry you should also use mysqli_real_escape_string to limit SQL injection into your database. I know it may not apply to your current work but it's best practise to get into - short of actually using Prepared Statements.

so:

$sql = mysqli_query($dbcon,"INSERT INTO mydatabase (terms) 
       VALUES ('".mysqli_real_escape_string($dbcon, $sorted_terms)."')");

Using Prepared Statements instead is very highly recommended.

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

Comments

1

Simply use string concatenation. Use $terms1 as string in for loop.

$terms1 = "";
$terms = explode(";", "banana;apple;lemon");
sort($terms);
$length = count($terms);
for($x = 0; $x < $length; $x++) {
$terms1 .= $terms[$x].'; ';
}

//$sorted_terms = $terms[0].$terms[1].$terms[2];
//$sql = mysqli_query($dbcon,"INSERT INTO mydatabase (terms) VALUES ('$sorted_terms')");
$sql = mysqli_query($dbcon,"INSERT INTO mydatabase (terms) VALUES ('$terms1')");

5 Comments

This works; but implode does the same thing with far less code.
Yes Martin sure, but this is reply for his understanding in question asked above "but I was not able to detect the summary of arrays outside the foreach loop."
It works also thanks, but have solved it with implode. Code looks like cleaner :)
@walker2k if you solved it with implode then this answer is not the right answer because this answer doesn't use implode.
@Martin Kuldepp solves it exactly with my code in question. So that answer was for me the correct one. That i've been used implode at the end, doesn't change the content from question.
0

Try $sorted_terms = implode(';', $terms);

http://php.net/manual/en/function.implode.php

3 Comments

Why does the OP want dots/periods instead of the ; as specified in their question?
Why? It's not clear whether OP wants ; or . so I'm going by the given example.
OP states: $terms[$x].'; '; . OP at no point states they want ".". You may be getting confused with the PHP String Concatenation Operator

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.