PHP - split and combine strings

Hi,
What is the best way to combine the following values separated by a comma using a loop to check if a values exist?


from
       
       
       $itemPrice.1 = "40.00";
       $itemPrice.2 = "42.50";
       $itemPrice.3 = "56.40";
       $itemPrice.4 = "";
       $itemPrice.5 = "";
       $itemPrice.6 = "";
       $itemPrice.7 = "";
       $itemPrice.8 = "";
       $itemPrice.9 = "";
       
to  
       
       
       $itemPrice =  "40.00,42.50,56.40";
     


and a way to achieve the reverse,

from
       
       $itemPrice =  "3.20,2.50,6.40,5.80,17.60";
       
to      
       
       $itemPrice.1 = "3.20";
       $itemPrice.2 = "2.50";
       $itemPrice.3 = "6.40";
       $itemPrice.4 = "5.80";
       $itemPrice.5 = "17.60";
       $itemPrice.6 = "";
       $itemPrice.7 = "";
       $itemPrice.8 = "";
       $itemPrice.9 = "";

Thanks
sabecsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian TaoSenior Business Solutions ConsultantCommented:

1.

If you can make them array elements rather than those not-so-common names, things will be much easier. You can then use implode and explode functions to do what you want to achieve.

2.

Are there always 9 items? or you can have any number of them?
0
sabecsAuthor Commented:
Yes, there won't be more than 9, are you saying I should make them as below?        
        $itemPrice[1] = "3.20";
        $itemPrice[2] = "2.50";
        $itemPrice[3] = "6.40";
        $itemPrice[4]= "5.80";
        $itemPrice[5]= "17.60";
        $itemPrice[6]= "";
        $itemPrice[7]= "";
0
Brian TaoSenior Business Solutions ConsultantCommented:
Correct.  Please see the sample code below:
function get_string_from_price($array_price){
  $ret = implode(",", $array_price);
  $ret = rtrim($ret, ",");
  return $ret;
}

function get_price_from_string($string_price){
  $tmp1 = array(1 => "", "", "", "", "", "", "", "", ""); // placeholders for 9 items, you don't need this, if you're going to discard the empty ones.
                                                          // the "1 =>" is to force the index to start at 1 to conform to your code, please note that in PHP array index starts at 0
  $tmp2 = explode(",", $string_price);  // create the array from the string
  array_unshift($tmp2, "");             // PHP array index starts at 0, so we need to unshift it with 1 element in the head
  $ret = array_replace($tmp1, array_slice($tmp2, 1, null, true)); // get the array elements starting from index 1 and replace the corresponding elements in the placeholder array $tmp1
  return $ret;
}

echo "<pre>\n";

$itemPrice_array1 = array(1 => "40.00", "42.50", "56.40", "", "", "", "", "", ""); // if you use array properly, I don't think you need the empty ones.
$itemPrice_string1 = get_string_from_price($itemPrice_array1);
echo "\$itemPrice_string1 is \"$itemPrice_string1\"\n";
/* output is:
$itemPrice_string1 is "40.00,42.50,56.40"
*/

$itemPrice_string2 = "3.20,2.50,6.40,5.80,17.60";
$itemPrice_array2 = get_price_from_string($itemPrice_string2);
echo "\n\$itemPrice_array2 is:\n";
print_r($itemPrice_array2);
/* output is:
$itemPrice_array2 is:
Array
(
    [1] => 3.20
    [2] => 2.50
    [3] => 6.40
    [4] => 5.80
    [5] => 17.60
    [6] => 
    [7] => 
    [8] => 
    [9] => 
)*/

echo "</pre>";

Open in new window

0
Ray PaseurCommented:
Arrays and strings are traditionally numbered starting with zero, not one.  If you're the only person who will ever see this code, you may be able to get away with numbered indexes that do not observe the standard practices, but if anyone else is going to see it, they are sure to be confused.

Typically arrays are processed with iterators.  These are programming concepts like while(), foreach(), etc., that allow you to think of the array as a single variable with any number of data elements, without having to impose artificial restrictions like "never more than 9."

Here is how I might do it.  Beware of loosely typed comparisons to NULL; if you want to preserve positions that contain zero values, you may need to rethink the design of some other parts of your application!

<?php // demo/temp_sabecs.php
error_reporting(E_ALL);

// COLLECT THE ITEM PRICES INTO AN ARRAY
$itemPrice[] = "3.20";
$itemPrice[] = "2.50";
$itemPrice[] = "6.40";
$itemPrice[] = "5.80";
$itemPrice[] = "17.60";

// NOT SURE WHY THESE NEED TO BE HERE?
$itemPrice[] = "";
$itemPrice[] = "";

// DISPOSE OF THE EMPTY POSITIONS
foreach ($itemPrice as $key => $val)
{
    if ($val == NULL) unset($itemPrice[$key]);
}

// COLLAPSE THE ARRAY INTO COMMA-SEPARATED VALUES
$str = implode(',', $itemPrice);
var_dump($str);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.