0

Please help me to put these words of string into some specific variables.

$remove4 = "First 1st, Second 2nd, Third 3rd, Fourth 4th"; 

i'm using this code, but i can get it right. can somebody tell me what is the problem in my code?

    $str = (explode(",",$remove4));

$check = array();
for($i=0;$i<=$count;$i++){
    $check[] = $str[$i];
}

foreach($check as $value){
    echo $value . "<br>";
    $var=(explode(" ", $value));
        echo $var[0];
        echo $var[1];
}

Goal:

Process1 = 'First';
Process2 = 'Second';
Process3 = 'Third';
Process4 = 'Fourth';

Temp1 = '1st';
Temp2 = '2nd';
Temp3 = '3rd';
Temp4 = '4th';
0

2 Answers 2

1
$remove4 = "First 1st, Second 2nd, Third 3rd, Fourth 4th";

$str = (explode(",",$remove4));

$check = array();


$i=0; foreach ($str as $value) { $i++;

    $explode = (array_values(array_filter(explode(" ",$value))));

    ${'Process'.($i)} = $explode[0];
    ${'Temp'.($i)} = $explode[1];
}

echo $Process1 . "<br /> ";
echo $Process2 . "<br /> ";
echo $Process3 . "<br /> ";
echo $Process4 . "<br /> ";

echo $Temp1 . "<br /> ";
echo $Temp2 . "<br /> ";
echo $Temp3 . "<br /> ";
echo $Temp4 . "<br /> ";

Output :

First
Second
Third
Fourth
1st
2nd
3rd
4th
Sign up to request clarification or add additional context in comments.

3 Comments

Wow, this what i've been looking for all day.. thanks buddy. I owe you a cup of coffee. :D
I wont forget your promise of coffe :D
Mark my word buddy.. :) given an opportunity to visit your beautiful place Turkiye..
1

you can try this

  foreach($check as $key => $value){
   //echo $value . "<br>";
    $var=(explode(" ", $value));
        ${'Process'.($key+1)} = $var[0];
        ${'Temp'.($key+1)} = $var[1];
}

echo $Process1; echo $Temp3;

Comments

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.