How to format the string to certain length and show it new line in an array using PHP

81 views Asked by At

i have an array like this,

 Array
        (
            [0] => 1    CS pizza                      Rs. 400

            [1] => 1    Pepperoni Pizza               Rs. 300

            [2] => 1    Creamy Tomato Soup            Rs. 100

            [3] => 1    Veg Triple Singapore
Noodles     Rs. 200

            [4] => 1    Mix Triple Noodles            Rs. 210

            [5] => 1    Veg Triple Schezwan
Noodles     Rs. 180

            [6] => 1    Veg Triple Singapore
Noodles     Rs. 200

            [7] => 1    New POS Item                  Rs. 100

        )

i want to format the data to like this using php ,

 Array
        (
            [0] => 1    CS pizza                      Rs. 400

            [1] => 1    Pepperoni Pizza               Rs. 300

            [2] => 1    Creamy Tomato Soup            Rs. 100

            [3] => 1    Veg Triple Singapore
                        Noodles                       Rs. 200

            [4] => 1    Mix Triple Noodles            Rs. 210

            [5] => 1    Veg Triple Schezwan
                        Noodles                       Rs. 180

            [6] => 1    Veg Triple Singapore
                        Noodles                       Rs. 200

            [7] => 1    New POS Item                  Rs. 100

        )

i am using this code to do it like this , but i am unable to this,

$value['quantity']."    ".wordwrap(str_pad($value['itemname'],25), 25,"\n")."     "."Rs. ".$value['basePrice']."\n"

How to proceed , can anyone help on this.

1

There are 1 answers

0
jazs ghosh On

ok here i got my answer,

$cnt = 0;
foreach ($order_data['results']['itemDet'] as $key => $value) {
    $first25 = substr($value['itemname'], 0, 20);
    $theRest = substr($value['itemname'], 20);
    if(strlen($value['itemname']) < 20 ){
        $final_thrd_array[$cnt] = $value['quantity']."    ".str_pad($first25,25)."     "."Rs. ".$value['basePrice']."\n";
    }else{
        $final_thrd[0] = $value['quantity']."    ".str_pad($first25,25)."     "."Rs. ".$value['basePrice']."\n";
        $final_thrd[1] = "  "."    ".str_pad($theRest,25)."     "."\n";
        $final_thrd_array[$cnt] = $final_thrd[0];
        $final_thrd_array[++$cnt] = $final_thrd[1];
    }
    $cnt ++;
 }                                  
using this code i can able to form my format,
                                      
Array
    (
        [0] => 1    CS pizza                      Rs. 400

        [1] => 1    Pepperoni Pizza               Rs. 300

        [2] => 1    Creamy Tomato Soup            Rs. 100

        [3] => 1    Veg Triple Singapore          Rs. 200

        [4] =>        Noodles                      

        [5] => 1    Mix Triple Noodles            Rs. 210

        [6] => 1    Veg Triple Schezwan           Rs. 180

        [7] =>       Noodles                       

        [8] => 1    Veg Triple Singapore          Rs. 200

        [9] =>        Noodles                      

        [10] => 1    New POS Item                  Rs. 100

    )