Hi can anyone give me an idea on how I can come out with an output like this:

$string = string1-1/string3-3/string3-3/  //this is the original string from DB
    //i want to split the strings and put it in the array, and the array should look like this
Array[0] = String
Array[1] = Int
Array[2] = String
Array[3] = Int
...and so on..

For better understanding of my problem, the string from db that I want to split is a set of strings that I concatenate when the user insert it.
Now, I want to split it up into STRINGS and INT so that I can subtract the INT value to the quantity of the STRING.

What I have done so far is like this:

 $result1 = mysqli_query($connection, "SELECT orderline.product_id, product_name, category_id, orderline.qty_order, accessoryName
                            FROM orderline
                            INNER JOIN products
                            ON products.product_id = orderline.product_id
                            WHERE category_id = 10
                            AND orderline.order_no = $orno ");
                    while($row1 = mysqli_fetch_assoc($result1)) { 
                      $pcname = $row1['product_name']; //get the product name
                      $qty_order1 = $row1['qty_order'];
                      $aName = $row1['accessoryName']; //save accessory name to variable

    $ACname = explode("/", $aName); //ouput array[0]: name1-qty1, array[1]: name2-qty2, array[3]: name3-qty3 
            for ($a = 0; $a < count($ACname); ++$i) { //loop count: array size
                //loop split array[0] to array[n]
                    $ACEname = explode("-", $ACname); //ouput array[0]: name1, array[2]: price1, array[3]: name2, array[4]: price2

In my last line of code, I don't know what to do next.. Anyone have an idea on how I would come up to what I want? Suggestion please. Thanks!

// initialize output array
$outputArray = array();
$tmpArray = explode("/", $aName);
foreach ($tmpArray as $tmpString) { 
    $tmpOneRow = explode("-", $tmpString);
    // add two elements to the output array
    $outputArray[] = $tmpOneRow[0];
    $outputArray[] = $tmpOneRow[1];
}
// check the result
print_r($outputArray);

Beware: no error checking in above example. You might want to check for existence of elements.

just an idea.. u could separate the names from price i.e

    // initialize output array
    $nameArray = array();
    $priceArray = array();

    $tmpArray = explode("/", $aName);
    foreach ($tmpArray as $tmpString) {
    $tmpOneRow = explode("-", $tmpString);
    // add two elements to the output array
    $nameArray[] = $tmpOneRow[0];
    $priceArray[] = $tmpOneRow[1];
    }
    // check the result
   for($i=0; $i<count($nameArray); i++)
   {
     print "Name: ".$$nameArray[$i]." Price: " .$priceArray[$i];
   }

Although all depends on what you want to do. There are many ways of improving this to get what you want

Just another deviation of the two proposed solutions above on a single loop.

$string = 'string1-1/string2-2/string3-3';

$array_string = explode('/',$string);

foreach($array_string as $items){

        $item = explode('-',$items);

        echo 'Item : '. $item[0].'  Price:  '.$item[1].'<br/>';

}

returns

Item : string1 Price: 1
Item : string2 Price: 2
Item : string3 Price: 3

you just need to initialize the array as shown on Broj1 and Hakeemtunde responses.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.