-1

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";
2
  • Do you mean you want an array of low and high prices from the loop? or just the first two values in the array as low and high? Commented Feb 9, 2012 at 12:10
  • 1
    You want only the first 2 values..? Please explain more clearly. Commented Feb 9, 2012 at 12:10

4 Answers 4

1

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++];
// ...
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

count($data) will be 1. He is defining an array with a single element. Just remove the array() from your data= lines.
@Leigh - already fixed, with appropriate comments about creating an array in the first place
0

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
        )

)

Comments

0

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){}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.