put string in array, split by every second line












1















I want to convert my string into array, Im giving you examples of what i want and what Ive tried. My string looks like this:



Height:
3/16
Color:
Standard Red
Material:
Die-cut, pressure-sensitive paper
Package Quantity:
1000/Pkg
Reusable:
Yes
Size:
3/16 H x 1/4 W


I want it to convert it in array to look like this:



Array
(
[0] => Height: 3/16
[1] => Color: Standard Red
[2] => Material: Die-cut, pressure-sensitive paper
[3] => Package Quantity: 1000/Pkg
[4] => Reusable: Yes
[5] => Size: 3/16 H x 1/4 W
)


I tried with this:



$array = explode("n", $string);


But Ive got this for the result:



Array
(
[0] => Height:
[1] => 3/16
[2] => Color:
[3] => Standard Red
[4] => Material:
[5] => Die-cut, pressure-sensitive paper
[6] => Package Quantity:
[7] => 1000/Pkg
[8] => Reusable:
[9] => Yes
[10] => Size:
[11] => 3/16 H x 1/4 W
[12] =>
)









share|improve this question



























    1















    I want to convert my string into array, Im giving you examples of what i want and what Ive tried. My string looks like this:



    Height:
    3/16
    Color:
    Standard Red
    Material:
    Die-cut, pressure-sensitive paper
    Package Quantity:
    1000/Pkg
    Reusable:
    Yes
    Size:
    3/16 H x 1/4 W


    I want it to convert it in array to look like this:



    Array
    (
    [0] => Height: 3/16
    [1] => Color: Standard Red
    [2] => Material: Die-cut, pressure-sensitive paper
    [3] => Package Quantity: 1000/Pkg
    [4] => Reusable: Yes
    [5] => Size: 3/16 H x 1/4 W
    )


    I tried with this:



    $array = explode("n", $string);


    But Ive got this for the result:



    Array
    (
    [0] => Height:
    [1] => 3/16
    [2] => Color:
    [3] => Standard Red
    [4] => Material:
    [5] => Die-cut, pressure-sensitive paper
    [6] => Package Quantity:
    [7] => 1000/Pkg
    [8] => Reusable:
    [9] => Yes
    [10] => Size:
    [11] => 3/16 H x 1/4 W
    [12] =>
    )









    share|improve this question

























      1












      1








      1








      I want to convert my string into array, Im giving you examples of what i want and what Ive tried. My string looks like this:



      Height:
      3/16
      Color:
      Standard Red
      Material:
      Die-cut, pressure-sensitive paper
      Package Quantity:
      1000/Pkg
      Reusable:
      Yes
      Size:
      3/16 H x 1/4 W


      I want it to convert it in array to look like this:



      Array
      (
      [0] => Height: 3/16
      [1] => Color: Standard Red
      [2] => Material: Die-cut, pressure-sensitive paper
      [3] => Package Quantity: 1000/Pkg
      [4] => Reusable: Yes
      [5] => Size: 3/16 H x 1/4 W
      )


      I tried with this:



      $array = explode("n", $string);


      But Ive got this for the result:



      Array
      (
      [0] => Height:
      [1] => 3/16
      [2] => Color:
      [3] => Standard Red
      [4] => Material:
      [5] => Die-cut, pressure-sensitive paper
      [6] => Package Quantity:
      [7] => 1000/Pkg
      [8] => Reusable:
      [9] => Yes
      [10] => Size:
      [11] => 3/16 H x 1/4 W
      [12] =>
      )









      share|improve this question














      I want to convert my string into array, Im giving you examples of what i want and what Ive tried. My string looks like this:



      Height:
      3/16
      Color:
      Standard Red
      Material:
      Die-cut, pressure-sensitive paper
      Package Quantity:
      1000/Pkg
      Reusable:
      Yes
      Size:
      3/16 H x 1/4 W


      I want it to convert it in array to look like this:



      Array
      (
      [0] => Height: 3/16
      [1] => Color: Standard Red
      [2] => Material: Die-cut, pressure-sensitive paper
      [3] => Package Quantity: 1000/Pkg
      [4] => Reusable: Yes
      [5] => Size: 3/16 H x 1/4 W
      )


      I tried with this:



      $array = explode("n", $string);


      But Ive got this for the result:



      Array
      (
      [0] => Height:
      [1] => 3/16
      [2] => Color:
      [3] => Standard Red
      [4] => Material:
      [5] => Die-cut, pressure-sensitive paper
      [6] => Package Quantity:
      [7] => 1000/Pkg
      [8] => Reusable:
      [9] => Yes
      [10] => Size:
      [11] => 3/16 H x 1/4 W
      [12] =>
      )






      php arrays string






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 17:36







      user9819807































          2 Answers
          2






          active

          oldest

          votes


















          1














          You're on the right track by separating the string into lines. You then need to group the array into pairs of lines, and implode them together. Here I'm using array_map to modify each of the pairs at once, but you could also do this for a simple for-loop if it's clearer.



          $lines = explode("n", trim($string));

          $combined = array_map(
          function($line) { return implode(' ', $line); },
          array_chunk($lines, 2)
          );


          $combined should now match the output in your question. See https://3v4l.org/ZdgWS for a full example.






          share|improve this answer


























          • Great this is what i want. Thanks. This is my result, how to remove last element if is empty? [0] => Height: 3/16 [1] => Color: Standard Red [2] => Material: Die-cut, pressure-sensitive paper [3] => Package Quantity: 1000/Pkg [4] => Reusable: Yes [5] => Size: 3/16 H x 1/4 W [6] =>

            – user9819807
            Nov 23 '18 at 17:49











          • The easiest way would be to just trim the string before exploding - I've updated the first line in my answer

            – iainn
            Nov 23 '18 at 17:50













          • thanks.........

            – user9819807
            Nov 23 '18 at 17:52











          • I want to remove these number keys(0,1,2) And replace them with string before two dots in array elements values. So the final result will be: [Height] => 3/16 How i can do that

            – user9819807
            Nov 23 '18 at 18:38











          • @doki look at my answer please I think it will be good solution for you

            – buildok
            Nov 23 '18 at 19:15



















          0














          You can use something like this:



          preg_match_all('/(?<key>.+):n(?<value>.+)/', $s, $a);
          $result = array_combine($a['key'], $a['value']);

          print_r($result);


          output:



          Array
          (
          [Height] => 3/16
          [Color] => Standard Red
          [Material] => Die-cut, pressure-sensitive paper
          [Package Quantity] => 1000/Pkg
          [Reusable] => Yes
          [Size] => 3/16 H x 1/4 W
          )


          So, you need only these 2 functions to use:
          array_combine()
          preg_match_all()






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450937%2fput-string-in-array-split-by-every-second-line%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown
























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            You're on the right track by separating the string into lines. You then need to group the array into pairs of lines, and implode them together. Here I'm using array_map to modify each of the pairs at once, but you could also do this for a simple for-loop if it's clearer.



            $lines = explode("n", trim($string));

            $combined = array_map(
            function($line) { return implode(' ', $line); },
            array_chunk($lines, 2)
            );


            $combined should now match the output in your question. See https://3v4l.org/ZdgWS for a full example.






            share|improve this answer


























            • Great this is what i want. Thanks. This is my result, how to remove last element if is empty? [0] => Height: 3/16 [1] => Color: Standard Red [2] => Material: Die-cut, pressure-sensitive paper [3] => Package Quantity: 1000/Pkg [4] => Reusable: Yes [5] => Size: 3/16 H x 1/4 W [6] =>

              – user9819807
              Nov 23 '18 at 17:49











            • The easiest way would be to just trim the string before exploding - I've updated the first line in my answer

              – iainn
              Nov 23 '18 at 17:50













            • thanks.........

              – user9819807
              Nov 23 '18 at 17:52











            • I want to remove these number keys(0,1,2) And replace them with string before two dots in array elements values. So the final result will be: [Height] => 3/16 How i can do that

              – user9819807
              Nov 23 '18 at 18:38











            • @doki look at my answer please I think it will be good solution for you

              – buildok
              Nov 23 '18 at 19:15
















            1














            You're on the right track by separating the string into lines. You then need to group the array into pairs of lines, and implode them together. Here I'm using array_map to modify each of the pairs at once, but you could also do this for a simple for-loop if it's clearer.



            $lines = explode("n", trim($string));

            $combined = array_map(
            function($line) { return implode(' ', $line); },
            array_chunk($lines, 2)
            );


            $combined should now match the output in your question. See https://3v4l.org/ZdgWS for a full example.






            share|improve this answer


























            • Great this is what i want. Thanks. This is my result, how to remove last element if is empty? [0] => Height: 3/16 [1] => Color: Standard Red [2] => Material: Die-cut, pressure-sensitive paper [3] => Package Quantity: 1000/Pkg [4] => Reusable: Yes [5] => Size: 3/16 H x 1/4 W [6] =>

              – user9819807
              Nov 23 '18 at 17:49











            • The easiest way would be to just trim the string before exploding - I've updated the first line in my answer

              – iainn
              Nov 23 '18 at 17:50













            • thanks.........

              – user9819807
              Nov 23 '18 at 17:52











            • I want to remove these number keys(0,1,2) And replace them with string before two dots in array elements values. So the final result will be: [Height] => 3/16 How i can do that

              – user9819807
              Nov 23 '18 at 18:38











            • @doki look at my answer please I think it will be good solution for you

              – buildok
              Nov 23 '18 at 19:15














            1












            1








            1







            You're on the right track by separating the string into lines. You then need to group the array into pairs of lines, and implode them together. Here I'm using array_map to modify each of the pairs at once, but you could also do this for a simple for-loop if it's clearer.



            $lines = explode("n", trim($string));

            $combined = array_map(
            function($line) { return implode(' ', $line); },
            array_chunk($lines, 2)
            );


            $combined should now match the output in your question. See https://3v4l.org/ZdgWS for a full example.






            share|improve this answer















            You're on the right track by separating the string into lines. You then need to group the array into pairs of lines, and implode them together. Here I'm using array_map to modify each of the pairs at once, but you could also do this for a simple for-loop if it's clearer.



            $lines = explode("n", trim($string));

            $combined = array_map(
            function($line) { return implode(' ', $line); },
            array_chunk($lines, 2)
            );


            $combined should now match the output in your question. See https://3v4l.org/ZdgWS for a full example.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 23 '18 at 17:50

























            answered Nov 23 '18 at 17:46









            iainniainn

            12.2k81930




            12.2k81930













            • Great this is what i want. Thanks. This is my result, how to remove last element if is empty? [0] => Height: 3/16 [1] => Color: Standard Red [2] => Material: Die-cut, pressure-sensitive paper [3] => Package Quantity: 1000/Pkg [4] => Reusable: Yes [5] => Size: 3/16 H x 1/4 W [6] =>

              – user9819807
              Nov 23 '18 at 17:49











            • The easiest way would be to just trim the string before exploding - I've updated the first line in my answer

              – iainn
              Nov 23 '18 at 17:50













            • thanks.........

              – user9819807
              Nov 23 '18 at 17:52











            • I want to remove these number keys(0,1,2) And replace them with string before two dots in array elements values. So the final result will be: [Height] => 3/16 How i can do that

              – user9819807
              Nov 23 '18 at 18:38











            • @doki look at my answer please I think it will be good solution for you

              – buildok
              Nov 23 '18 at 19:15



















            • Great this is what i want. Thanks. This is my result, how to remove last element if is empty? [0] => Height: 3/16 [1] => Color: Standard Red [2] => Material: Die-cut, pressure-sensitive paper [3] => Package Quantity: 1000/Pkg [4] => Reusable: Yes [5] => Size: 3/16 H x 1/4 W [6] =>

              – user9819807
              Nov 23 '18 at 17:49











            • The easiest way would be to just trim the string before exploding - I've updated the first line in my answer

              – iainn
              Nov 23 '18 at 17:50













            • thanks.........

              – user9819807
              Nov 23 '18 at 17:52











            • I want to remove these number keys(0,1,2) And replace them with string before two dots in array elements values. So the final result will be: [Height] => 3/16 How i can do that

              – user9819807
              Nov 23 '18 at 18:38











            • @doki look at my answer please I think it will be good solution for you

              – buildok
              Nov 23 '18 at 19:15

















            Great this is what i want. Thanks. This is my result, how to remove last element if is empty? [0] => Height: 3/16 [1] => Color: Standard Red [2] => Material: Die-cut, pressure-sensitive paper [3] => Package Quantity: 1000/Pkg [4] => Reusable: Yes [5] => Size: 3/16 H x 1/4 W [6] =>

            – user9819807
            Nov 23 '18 at 17:49





            Great this is what i want. Thanks. This is my result, how to remove last element if is empty? [0] => Height: 3/16 [1] => Color: Standard Red [2] => Material: Die-cut, pressure-sensitive paper [3] => Package Quantity: 1000/Pkg [4] => Reusable: Yes [5] => Size: 3/16 H x 1/4 W [6] =>

            – user9819807
            Nov 23 '18 at 17:49













            The easiest way would be to just trim the string before exploding - I've updated the first line in my answer

            – iainn
            Nov 23 '18 at 17:50







            The easiest way would be to just trim the string before exploding - I've updated the first line in my answer

            – iainn
            Nov 23 '18 at 17:50















            thanks.........

            – user9819807
            Nov 23 '18 at 17:52





            thanks.........

            – user9819807
            Nov 23 '18 at 17:52













            I want to remove these number keys(0,1,2) And replace them with string before two dots in array elements values. So the final result will be: [Height] => 3/16 How i can do that

            – user9819807
            Nov 23 '18 at 18:38





            I want to remove these number keys(0,1,2) And replace them with string before two dots in array elements values. So the final result will be: [Height] => 3/16 How i can do that

            – user9819807
            Nov 23 '18 at 18:38













            @doki look at my answer please I think it will be good solution for you

            – buildok
            Nov 23 '18 at 19:15





            @doki look at my answer please I think it will be good solution for you

            – buildok
            Nov 23 '18 at 19:15













            0














            You can use something like this:



            preg_match_all('/(?<key>.+):n(?<value>.+)/', $s, $a);
            $result = array_combine($a['key'], $a['value']);

            print_r($result);


            output:



            Array
            (
            [Height] => 3/16
            [Color] => Standard Red
            [Material] => Die-cut, pressure-sensitive paper
            [Package Quantity] => 1000/Pkg
            [Reusable] => Yes
            [Size] => 3/16 H x 1/4 W
            )


            So, you need only these 2 functions to use:
            array_combine()
            preg_match_all()






            share|improve this answer






























              0














              You can use something like this:



              preg_match_all('/(?<key>.+):n(?<value>.+)/', $s, $a);
              $result = array_combine($a['key'], $a['value']);

              print_r($result);


              output:



              Array
              (
              [Height] => 3/16
              [Color] => Standard Red
              [Material] => Die-cut, pressure-sensitive paper
              [Package Quantity] => 1000/Pkg
              [Reusable] => Yes
              [Size] => 3/16 H x 1/4 W
              )


              So, you need only these 2 functions to use:
              array_combine()
              preg_match_all()






              share|improve this answer




























                0












                0








                0







                You can use something like this:



                preg_match_all('/(?<key>.+):n(?<value>.+)/', $s, $a);
                $result = array_combine($a['key'], $a['value']);

                print_r($result);


                output:



                Array
                (
                [Height] => 3/16
                [Color] => Standard Red
                [Material] => Die-cut, pressure-sensitive paper
                [Package Quantity] => 1000/Pkg
                [Reusable] => Yes
                [Size] => 3/16 H x 1/4 W
                )


                So, you need only these 2 functions to use:
                array_combine()
                preg_match_all()






                share|improve this answer















                You can use something like this:



                preg_match_all('/(?<key>.+):n(?<value>.+)/', $s, $a);
                $result = array_combine($a['key'], $a['value']);

                print_r($result);


                output:



                Array
                (
                [Height] => 3/16
                [Color] => Standard Red
                [Material] => Die-cut, pressure-sensitive paper
                [Package Quantity] => 1000/Pkg
                [Reusable] => Yes
                [Size] => 3/16 H x 1/4 W
                )


                So, you need only these 2 functions to use:
                array_combine()
                preg_match_all()







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 23 '18 at 19:18

























                answered Nov 23 '18 at 19:06









                buildokbuildok

                73747




                73747






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450937%2fput-string-in-array-split-by-every-second-line%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Tonle Sap (See)

                    I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

                    Guatemaltekische Davis-Cup-Mannschaft