Changing from $jumbeVar to $tidyVar

I have a variable like the above.
The value of the variable $jumbleVar is all text.

I like to make the $jumbleVar to $tidyVar.
The code below doesn’t work correctly, but I hope that it shows what I want.

And my target result is the following.

echo $tidyVar;

I am taking a guess that this line is part of is part of a DB query. If that is the case you should look at the IN function.

I noticed only one of the var’s has a dollar sign. Seems to be a mistake. Either they all are supposed to have it or none most likely.

Best thing to do is tell us what the real problem is instead of asking about your attempted solution to the real problem.

it was typo.
I fixed it as I noticed by your telling.

However it is all same whether it does have dollar sign like the below or doen’t have like the above.

Important thing is they are all same except numbers.

Ok, so tell us about the real problem you are trying to solve with this code.

It looks to me as if the OP wants to sort the OR values in ascending order. I can’t imagine why if it’s being used in a query, but maybe it’s for something else.

One thought might be to explode the string by “OR” and then sort the resulting array, or maybe if the string is being built from an array, sort that array in the first place.

Yes, it can be part of a DB query or inside if-clause.

I sometimes found the variable “$jumbleVar” has wrong number

for example,

When I found “var=17” is wrong, I have to change it like the following.

in order to find the wrong number, my eyes have to scan from the first till found it.
If the variable is not jumbbled, and arranged. I can find the wrong number with easy.


How can I sort the resulting array?

As all the ORs look at the same column you probably should be using IN for this, Eg

... WHERE var IN (3, 8, 17, 25)

As you can see the comma separated list of possible values could easily be made from an array with implode. That array could easily be sorted with sort.

Is it a DB query? or an array?

The above is already arranged. I like to make it arranged from non-arranged one.

That is part of a query.
It looks for rows where the value of “var” is in the list of values in the brackets.

OP, you are still telling us how you are trying to solve a problem. What is the actual problem, the overall task at hand?

You could use array_sort, or there are other sort routines around.

The jumbleVar is sometimes very long, I like to find a wrong number which can be 17, 3 or 25 in order to remove the wrong number.

About the overall task, I put it…

Okay, after I search and test about the array_sort suggested by droopsnoot, I’ll be back.
(It cannot,actually, be short to write about the overall task.)

If I’m understanding, what the problem is was answered to your earlier asking.

While I agree that sorting the numbers would make it easier to manually spot “wrong” numbers, unless this is only to make it easier to debug the script I don’t see this approach as being anything easily maintainable. IMHO it would be much better to tighten up the code and have unit tests keep an eye on it.

As benanamen is implying, the better approach would be to figure out where the “wrong” number is coming from and how it’s getting included with the “right” numbers.

1 Like

I would have to strongly disagree.

We are “maybe” dealing with a DB query or “maybe” dealing with an if clause. OP is talking about “wrong numbers”. What is a “wrong number”? Why is OP “scanning” for them with his eyes? He says variables can be “jumbbled” or “arranged”. What does that mean? OP shows a max of two numbers but says “jumbleVar is sometimes very long”. What is a “jumbleVar”? OP also says “It cannot,actually, be short to write about the overall task.”

So I submit, the OP clearly has not told us what the REAL problem is which is why every response is just guessing at what an answer might be to his attempted solution.

(1) I like to tell about the my story.since peaple don’t like to listen to me around me…
(2) I don’t know I have to tell from where to where.
(3) it is really long for telling the overall story.

I am sorry if you feel not good although you tried to help me but I might not give the real problem.

Sounds Shady to me. I’m out.

I really want talk with you about my coding story since people don’t listen to it around me.
I don’t know I have to tell from where to where.
I will be happy to talk with you about my long story and I like, if you don’t mind, to listen to your story.

I am sorry you are out.

Have a good day, and see you.

The result of the above code is “OR var25”.

I write the following code for changing my original variable “$jumbleVar” into array pattern.

The result of the above is the following,

The above result is the same as the jumble array data.

echo $jumble[0]; is “OR var25”, but echo $jumble2[0] is the following.

How can I make it jumble2[0] is “OR var25” instead of ‘OR var=25", “OR var=8”, “OR var=17”, "OR var=3’.?

I have, I think, to make the variable “$jumbleVar” into an array for sorting it.

Why would you do that, when the explode() function does it for you?

We have established that this is part of a database query.
So as mentioned it would be better to use an IN condition instead of a long chain of OR conditions.
This will also make sorting the data easier.

$numbers = array(8, 3, 25, 17); // Starting with an array of numbers in unsorted order

sort($numbers); // This will sort the number into order

$inlist = implode(', ', $numbers); // Turns the array into a string of CSV

var_dump($inlist); // This will allow you to manually check the numbers on screen

$query = "SELECT * FROM table WHERE var IN ($inlist)"; // Create the query string with the list of values

Just a note of caution here. There may be a potential danger of injection putting the variable into the query if you are not 100% certain all values will be integers. If you do not have absolute control over where these values come from it will need a slightly different approach for the query, but that is going a little off topic for the current problem.