Change key [ 0 ] to [ Name ] in php array?
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
add a comment |
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
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
add a comment |
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
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
php arrays string
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
<?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',
)
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Nov 24 '18 at 0:03
NickNick
31.9k121942
31.9k121942
add a comment |
add a comment |
<?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',
)
add a comment |
<?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',
)
add a comment |
<?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',
)
<?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',
)
answered Nov 24 '18 at 0:14
ProgrockProgrock
4,4331921
4,4331921
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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