Changing from $jumbeVar to $tidyVar


#1

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;


#2

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.


#3

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.


#4

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


#5

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.


#6

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.

.


#7

How can I sort the resulting array?


#8

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.


#9

Is it a DB query? or an array?

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


#10

That is part of a query.
It looks for rows where the value of “var” is in the list of values in the brackets.
https://dev.mysql.com/doc/refman/8.0/en/comparison-operators.html#function_in


#11

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


#12

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


#13

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.)


#14

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.


#15

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.


#16

(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.


#17

Sounds Shady to me. I’m out.


#18

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.


#19

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


#20

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.