David D
David D

Reputation: 1295

Explode semicolon delimited string and iterate every two values

I have an array with a single value collected from database.

Within a while loop I wish to explode this for every two values.

Example:

$data = array('20;40;60;80;100;150;200;300;500;1000');

I want to explode this value and end up with the following loop:

$lowprice = "20";
$highprice = "40";

Upvotes: -1

Views: 3321

Answers (4)

lll
lll

Reputation: 12889

You can use preg_match_all().

Example:

$text = '20;40;60;80;100;150;200;300;500;1000';

preg_match_all("/([^;]+);([^;]+)/", $text, $pairs, PREG_SET_ORDER);

foreach ($pairs as $pair) {
    // ...
    $lowvalue = $pair[1];
    $highvalue = $pair[2];
    // ...
}

If you really must use explode and a while loop, the following will also work:

$text = '20;40;60;80;100;150;200;300;500;1000';

$data = explode(';', $text);

$i = 0;
$count = count($data);

while ($i < $count) {
// ...
    $lowvalue = $data[$i++];
    $highvalue = $data[$i++];
// ...
}

Upvotes: 1

Mark Baker
Mark Baker

Reputation: 212522

If you just want the first and second values as your low/high:

$data = array('20;40;60;80;100;150;200;300;500;1000');
list($lowprice,$highprice) = explode(';',current($data));

echo '$lowprice=',$lowprice,PHP_EOL;
echo '$highprice=',$highprice,PHP_EOL;

If you want an array of lows and highs:

$data = array('20;40;60;80;100;150;200;300;500;1000');
$lowprices = $highprices = array();
$data = explode(';',current($data));
$dataCount = count($data);
var_dump($data);
for ($i=0; $i < $dataCount; $i += 2) {
    $lowprices[] = $data[$i];
    $highprices[] = $data[$i+1];
}
echo '$lowprices=';
var_dump($lowprices);
echo '$highprices=';
var_dump($highprices);

EDIT

Why not start out with a proper array of values in the first place: it would simplify this a lot

$data = array(20,40,60,80,100,150,200,300,500,1000);
list($lowprice,$highprice) = $data;

echo '$lowprice=',$lowprice,PHP_EOL;
echo '$highprice=',$highprice,PHP_EOL;

or

$data = array(20,40,60,80,100,150,200,300,500,1000);
$lowprices = $highprices = array();
$dataCount = count($data);
var_dump($data);
for ($i=0; $i < $dataCount; $i += 2) {
    $lowprices[] = $data[$i];
    $highprices[] = $data[$i+1];
}
echo '$lowprices=';
var_dump($lowprices);
echo '$highprices=';
var_dump($highprices);

Upvotes: 1

Vyktor
Vyktor

Reputation: 21007

You may use loop like this one (using explode() and array_shift()):

$data = explode( ';', '20;40;60;80;100;150;200;300;500;1000');
while( count( $data) > 1){
    $lowprice = array_shift( $data);
    $highprice = array_shift( $data);
}

Or use for loop (should be more effective than calling count() in every iteration):

$count = count( $data);
for( $i = 0; $i < $count; $i+=2){}

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270765

Explode initially on the ;. Then in a for loop incrementing by 2, you can assign the current and next values to a sub-array:

$initarray = explode(";", "20;40;60;80;100;150;200;300;500;1000");
$num = count($initarray);

// Main array to hold subarrays
$outputarray = array();
for ($i=0; $i<$num; $i=$i+2) {
  // Add the current pair ($i and $i+1) to a sub-array
  $outputarray[] = array("lowprice"=>$initarray[$i], "highprice"=>$initarray[$i+1]);
}

Outputs:

Array
(
    [0] => Array
        (
            [lowprice] => 20
            [highprice] => 40
        )

    [1] => Array
        (
            [lowprice] => 60
            [highprice] => 80
        )

    [2] => Array
        (
            [lowprice] => 100
            [highprice] => 150
        )

    [3] => Array
        (
            [lowprice] => 200
            [highprice] => 300
        )

    [4] => Array
        (
            [lowprice] => 500
            [highprice] => 1000
        )

)

Upvotes: 0

Related Questions