Change key [ 0 ] to [ Name ] in php array?












-2















I have array:



[0] => Height: 3/16 
[1] => Color: Standard Red
[2] => Material: Die-cut, pressure-sensitive paper


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 
[Color] => Standard Red
[Material] => Die-cut, pressure-sensitive paper


I tried to solve this with array_values function but i didnt make it, can someone help me?










share|improve this question























  • Is the value of the keys (0, 1, 2) a string? "Height: 3/16" for example?

    – Nick Dawes
    Nov 23 '18 at 23:31











  • Please add what you tried. Also how is the first array generated, probably easier to fix it at that point.

    – user3783243
    Nov 23 '18 at 23:49
















-2















I have array:



[0] => Height: 3/16 
[1] => Color: Standard Red
[2] => Material: Die-cut, pressure-sensitive paper


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 
[Color] => Standard Red
[Material] => Die-cut, pressure-sensitive paper


I tried to solve this with array_values function but i didnt make it, can someone help me?










share|improve this question























  • Is the value of the keys (0, 1, 2) a string? "Height: 3/16" for example?

    – Nick Dawes
    Nov 23 '18 at 23:31











  • Please add what you tried. Also how is the first array generated, probably easier to fix it at that point.

    – user3783243
    Nov 23 '18 at 23:49














-2












-2








-2








I have array:



[0] => Height: 3/16 
[1] => Color: Standard Red
[2] => Material: Die-cut, pressure-sensitive paper


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 
[Color] => Standard Red
[Material] => Die-cut, pressure-sensitive paper


I tried to solve this with array_values function but i didnt make it, can someone help me?










share|improve this question














I have array:



[0] => Height: 3/16 
[1] => Color: Standard Red
[2] => Material: Die-cut, pressure-sensitive paper


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 
[Color] => Standard Red
[Material] => Die-cut, pressure-sensitive paper


I tried to solve this with array_values function but i didnt make it, can someone help me?







php arrays string






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 23:16







user9819807




















  • Is the value of the keys (0, 1, 2) a string? "Height: 3/16" for example?

    – Nick Dawes
    Nov 23 '18 at 23:31











  • Please add what you tried. Also how is the first array generated, probably easier to fix it at that point.

    – user3783243
    Nov 23 '18 at 23:49



















  • Is the value of the keys (0, 1, 2) a string? "Height: 3/16" for example?

    – Nick Dawes
    Nov 23 '18 at 23:31











  • Please add what you tried. Also how is the first array generated, probably easier to fix it at that point.

    – user3783243
    Nov 23 '18 at 23:49

















Is the value of the keys (0, 1, 2) a string? "Height: 3/16" for example?

– Nick Dawes
Nov 23 '18 at 23:31





Is the value of the keys (0, 1, 2) a string? "Height: 3/16" for example?

– Nick Dawes
Nov 23 '18 at 23:31













Please add what you tried. Also how is the first array generated, probably easier to fix it at that point.

– user3783243
Nov 23 '18 at 23:49





Please add what you tried. Also how is the first array generated, probably easier to fix it at that point.

– user3783243
Nov 23 '18 at 23:49












2 Answers
2






active

oldest

votes


















0














You can achieve this using array_reduce to process your array. Inside the function we use preg_match to split the string into its component parts (note you could probably just use explode(':', $v) and trim instead).



$array = array('Height: 3/16',
'Color: Standard Red',
'Material: Die-cut, pressure-sensitive paper');
$new_array = array_reduce($array,
function ($c, $v) { preg_match('/^([^:]+):s+(.*)$/', $v, $m);
return array_merge($c, array($m[1] => $m[2]));
},
);
print_r($new_array);


Output:



Array (
[Height] => 3/16
[Color] => Standard Red
[Material] => Die-cut, pressure-sensitive paper
)


Demo on 3v4l.org






share|improve this answer































    0














    <?php
    $input =
    [
    'Height: 3/16',
    'Color: Standard Red',
    'Material: Die-cut, pressure-sensitive paper'
    ];

    foreach($input as $v) {
    list($key, $val) = explode(':', $v, 2);
    $output[$key] = trim($val);
    }

    var_export($output);


    Output:



    array (
    'Height' => '3/16',
    'Color' => 'Standard Red',
    'Material' => 'Die-cut, pressure-sensitive paper',
    )





    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%2f53453789%2fchange-key-0-to-name-in-php-array%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









      0














      You can achieve this using array_reduce to process your array. Inside the function we use preg_match to split the string into its component parts (note you could probably just use explode(':', $v) and trim instead).



      $array = array('Height: 3/16',
      'Color: Standard Red',
      'Material: Die-cut, pressure-sensitive paper');
      $new_array = array_reduce($array,
      function ($c, $v) { preg_match('/^([^:]+):s+(.*)$/', $v, $m);
      return array_merge($c, array($m[1] => $m[2]));
      },
      );
      print_r($new_array);


      Output:



      Array (
      [Height] => 3/16
      [Color] => Standard Red
      [Material] => Die-cut, pressure-sensitive paper
      )


      Demo on 3v4l.org






      share|improve this answer




























        0














        You can achieve this using array_reduce to process your array. Inside the function we use preg_match to split the string into its component parts (note you could probably just use explode(':', $v) and trim instead).



        $array = array('Height: 3/16',
        'Color: Standard Red',
        'Material: Die-cut, pressure-sensitive paper');
        $new_array = array_reduce($array,
        function ($c, $v) { preg_match('/^([^:]+):s+(.*)$/', $v, $m);
        return array_merge($c, array($m[1] => $m[2]));
        },
        );
        print_r($new_array);


        Output:



        Array (
        [Height] => 3/16
        [Color] => Standard Red
        [Material] => Die-cut, pressure-sensitive paper
        )


        Demo on 3v4l.org






        share|improve this answer


























          0












          0








          0







          You can achieve this using array_reduce to process your array. Inside the function we use preg_match to split the string into its component parts (note you could probably just use explode(':', $v) and trim instead).



          $array = array('Height: 3/16',
          'Color: Standard Red',
          'Material: Die-cut, pressure-sensitive paper');
          $new_array = array_reduce($array,
          function ($c, $v) { preg_match('/^([^:]+):s+(.*)$/', $v, $m);
          return array_merge($c, array($m[1] => $m[2]));
          },
          );
          print_r($new_array);


          Output:



          Array (
          [Height] => 3/16
          [Color] => Standard Red
          [Material] => Die-cut, pressure-sensitive paper
          )


          Demo on 3v4l.org






          share|improve this answer













          You can achieve this using array_reduce to process your array. Inside the function we use preg_match to split the string into its component parts (note you could probably just use explode(':', $v) and trim instead).



          $array = array('Height: 3/16',
          'Color: Standard Red',
          'Material: Die-cut, pressure-sensitive paper');
          $new_array = array_reduce($array,
          function ($c, $v) { preg_match('/^([^:]+):s+(.*)$/', $v, $m);
          return array_merge($c, array($m[1] => $m[2]));
          },
          );
          print_r($new_array);


          Output:



          Array (
          [Height] => 3/16
          [Color] => Standard Red
          [Material] => Die-cut, pressure-sensitive paper
          )


          Demo on 3v4l.org







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 0:03









          NickNick

          31.9k121942




          31.9k121942

























              0














              <?php
              $input =
              [
              'Height: 3/16',
              'Color: Standard Red',
              'Material: Die-cut, pressure-sensitive paper'
              ];

              foreach($input as $v) {
              list($key, $val) = explode(':', $v, 2);
              $output[$key] = trim($val);
              }

              var_export($output);


              Output:



              array (
              'Height' => '3/16',
              'Color' => 'Standard Red',
              'Material' => 'Die-cut, pressure-sensitive paper',
              )





              share|improve this answer




























                0














                <?php
                $input =
                [
                'Height: 3/16',
                'Color: Standard Red',
                'Material: Die-cut, pressure-sensitive paper'
                ];

                foreach($input as $v) {
                list($key, $val) = explode(':', $v, 2);
                $output[$key] = trim($val);
                }

                var_export($output);


                Output:



                array (
                'Height' => '3/16',
                'Color' => 'Standard Red',
                'Material' => 'Die-cut, pressure-sensitive paper',
                )





                share|improve this answer


























                  0












                  0








                  0







                  <?php
                  $input =
                  [
                  'Height: 3/16',
                  'Color: Standard Red',
                  'Material: Die-cut, pressure-sensitive paper'
                  ];

                  foreach($input as $v) {
                  list($key, $val) = explode(':', $v, 2);
                  $output[$key] = trim($val);
                  }

                  var_export($output);


                  Output:



                  array (
                  'Height' => '3/16',
                  'Color' => 'Standard Red',
                  'Material' => 'Die-cut, pressure-sensitive paper',
                  )





                  share|improve this answer













                  <?php
                  $input =
                  [
                  'Height: 3/16',
                  'Color: Standard Red',
                  'Material: Die-cut, pressure-sensitive paper'
                  ];

                  foreach($input as $v) {
                  list($key, $val) = explode(':', $v, 2);
                  $output[$key] = trim($val);
                  }

                  var_export($output);


                  Output:



                  array (
                  'Height' => '3/16',
                  'Color' => 'Standard Red',
                  'Material' => 'Die-cut, pressure-sensitive paper',
                  )






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 24 '18 at 0:14









                  ProgrockProgrock

                  4,4331921




                  4,4331921






























                      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%2f53453789%2fchange-key-0-to-name-in-php-array%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

                      Ottavio Pratesi

                      Tricia Helfer

                      15 giugno