sohal07
sohal07

Reputation: 482

PHP split the value of a variable to multiple variables


I have a value from mysql which I want to split into multiple input fields in a form.

// This is the value from mysql...
$value = "10 - Coles, 15 - Aldi, 5 - Target, 7.77 - Kmart";

// This is how I want to split it....
$amt_1 = "10";
$desc_1 = "Coles";
$amt_2 = "15";
$desc_2 = "Aldi";
$amt_3 = "5";
$desc_3 = "Target";
$amt_4 = "7.77";
$desc_4 = "Kmart";


//So that it can be displayed in input boxes...
<input type="text" value="<?php echo $amt_1; ?>" />
<input type="text" value="<?php echo $desc_1; ?>" />

<input type="text" value="<?php echo $amt_2; ?>" />
<input type="text" value="<?php echo $desc_2; ?>" />

<input type="text" value="<?php echo $amt_3; ?>" />
<input type="text" value="<?php echo $desc_3; ?>" />

<input type="text" value="<?php echo $amt_4; ?>" />
<input type="text" value="<?php echo $desc_4; ?>" />

Now, there is a condition.

The $value can be empty or it may not enough to be able to split into 4 parts, means it can be $value = "10 - Coles, 15 - Aldi"; to be able to fit into $amt_1, $desc_1 and $amt_2, $desc_2. or any set of 1, 2, 3 or 4 splits.

Upvotes: 1

Views: 177

Answers (3)

Sunil Rajput
Sunil Rajput

Reputation: 980

Use following code

<?php
$value = "10 - Coles, 15 - Aldi, 5 - Target, 7.77 - Kmart";

$arrayValue = explode(', ', $value);
$i=0;
foreach ($arrayValue as $new) {
    $i++;
     $exactValue = explode(' - ',$new);
     echo '<input type="text" name="amt_'.($i).'" value="'.$exactValue[0].'" />';
     echo '<input type="text" name="desc_'.($i).'" value="'.$exactValue[1].'" /><br>';
}
 ?>

Upvotes: 0

Webeng
Webeng

Reputation: 7113

Using explode in an efficient matter inside a foreach loop:

// This is the value from mysql...
$value = "10 - Coles, 15 - Aldi, 5 - Target, 7.77 - Kmart";

$myArray = explode(",", $value);//separating the parts

foreach ($myArray as $data)
{
  $buffer = explode("-", $data);
  $first = trim($buffer[0]);
  $second = trim($buffer[1]);
  if ($first != "")
  {
    echo "<input type='text' value='{$first}' />";
    echo "<input type='text' value='{$second}' />";
  }
}

Upvotes: 0

Alex
Alex

Reputation: 17289

Here is my approach if you want exactly 4 pairs:

$value = "10 - Coles, 15 - Aldi, 5 - Target, 7.77 - Kmart";

$valArr = explode(', ', $value);

for ($i=0; $i<4; $i++) {
  if(isset($valArr[$i])) {
     $currentVal = explode(' - ',$valArr[$i]);
     echo '<input type="text" name="amt_'.($i+1).'" value="'.$currentVal[0].'" />';
     echo '<input type="text" name="desc_'.($i+1).'" value="'.$currentVal[1].'" />';
  } else {
     echo '<input type="text" name="amt_'.($i+1).'" value="NO VALUE" />';
     echo '<input type="text" name="desc_'.($i+1).'" value="NO VALUE" />';
  }
}

But to me more proper approach is:

$value = "10 - Coles, 15 - Aldi, 5 - Target, 7.77 - Kmart";

$valArr = explode(', ', $value);

foreach ($valArr as $pair) {
     $currentVal = explode(' - ',$pair);
     echo '<input type="text" name="amt_'.($i+1).'" value="'.$currentVal[0].'" />';
     echo '<input type="text" name="desc_'.($i+1).'" value="'.$currentVal[1].'" />';
}

Upvotes: 1

Related Questions