Remove everything before (including special character) in array
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Is it possible to remove everything before special characters including this character in array
?
For example, SUBSTR()
function is used in string
$a= ('1-160');
echo substr($a,strpos($a,'-')+1);
//output is 160
Any function for array like SUBSTR()
? (least priority to preg_replace
).
Here array is structured as following example, every index consists int
value + hyphen(-)
$a= array('1-160','2-250', '3-380');
I need to change removing every values before hyphen and hyphen also
$a= array('160','250', '380');
Actually my requirement is to sum all values after the hyphen(-)
in array. If hyphen(-)
can be removed, it can be done as
echo array_sum($a);
//output is 790
but, because of the special characters, I am generating output as following way.
$total = 0;
foreach($a AS $val){
$b = explode('-',$val);
$total += $b[1];
}
echo $total;
//output is 790
I am searching short and fast method as possible.
php arrays
add a comment |
Is it possible to remove everything before special characters including this character in array
?
For example, SUBSTR()
function is used in string
$a= ('1-160');
echo substr($a,strpos($a,'-')+1);
//output is 160
Any function for array like SUBSTR()
? (least priority to preg_replace
).
Here array is structured as following example, every index consists int
value + hyphen(-)
$a= array('1-160','2-250', '3-380');
I need to change removing every values before hyphen and hyphen also
$a= array('160','250', '380');
Actually my requirement is to sum all values after the hyphen(-)
in array. If hyphen(-)
can be removed, it can be done as
echo array_sum($a);
//output is 790
but, because of the special characters, I am generating output as following way.
$total = 0;
foreach($a AS $val){
$b = explode('-',$val);
$total += $b[1];
}
echo $total;
//output is 790
I am searching short and fast method as possible.
php arrays
What is wrong with your current approach?
– Tim Biegeleisen
Nov 29 '18 at 6:40
@TimBiegeleisen,array_sum
would be faster thanexplode
and adding value, isn't it ? So I wanna know, can I doarray_sum
in my case.
– Dipak
Nov 29 '18 at 6:44
preg_grep and array_sum but I doubt it will be faster
– Andreas
Nov 29 '18 at 6:46
1
How big is your data set? If it isn't very big, there will most likely not be any noticeable performance differences.
– Magnus Eriksson
Nov 29 '18 at 6:50
add a comment |
Is it possible to remove everything before special characters including this character in array
?
For example, SUBSTR()
function is used in string
$a= ('1-160');
echo substr($a,strpos($a,'-')+1);
//output is 160
Any function for array like SUBSTR()
? (least priority to preg_replace
).
Here array is structured as following example, every index consists int
value + hyphen(-)
$a= array('1-160','2-250', '3-380');
I need to change removing every values before hyphen and hyphen also
$a= array('160','250', '380');
Actually my requirement is to sum all values after the hyphen(-)
in array. If hyphen(-)
can be removed, it can be done as
echo array_sum($a);
//output is 790
but, because of the special characters, I am generating output as following way.
$total = 0;
foreach($a AS $val){
$b = explode('-',$val);
$total += $b[1];
}
echo $total;
//output is 790
I am searching short and fast method as possible.
php arrays
Is it possible to remove everything before special characters including this character in array
?
For example, SUBSTR()
function is used in string
$a= ('1-160');
echo substr($a,strpos($a,'-')+1);
//output is 160
Any function for array like SUBSTR()
? (least priority to preg_replace
).
Here array is structured as following example, every index consists int
value + hyphen(-)
$a= array('1-160','2-250', '3-380');
I need to change removing every values before hyphen and hyphen also
$a= array('160','250', '380');
Actually my requirement is to sum all values after the hyphen(-)
in array. If hyphen(-)
can be removed, it can be done as
echo array_sum($a);
//output is 790
but, because of the special characters, I am generating output as following way.
$total = 0;
foreach($a AS $val){
$b = explode('-',$val);
$total += $b[1];
}
echo $total;
//output is 790
I am searching short and fast method as possible.
php arrays
php arrays
asked Nov 29 '18 at 6:36
DipakDipak
525418
525418
What is wrong with your current approach?
– Tim Biegeleisen
Nov 29 '18 at 6:40
@TimBiegeleisen,array_sum
would be faster thanexplode
and adding value, isn't it ? So I wanna know, can I doarray_sum
in my case.
– Dipak
Nov 29 '18 at 6:44
preg_grep and array_sum but I doubt it will be faster
– Andreas
Nov 29 '18 at 6:46
1
How big is your data set? If it isn't very big, there will most likely not be any noticeable performance differences.
– Magnus Eriksson
Nov 29 '18 at 6:50
add a comment |
What is wrong with your current approach?
– Tim Biegeleisen
Nov 29 '18 at 6:40
@TimBiegeleisen,array_sum
would be faster thanexplode
and adding value, isn't it ? So I wanna know, can I doarray_sum
in my case.
– Dipak
Nov 29 '18 at 6:44
preg_grep and array_sum but I doubt it will be faster
– Andreas
Nov 29 '18 at 6:46
1
How big is your data set? If it isn't very big, there will most likely not be any noticeable performance differences.
– Magnus Eriksson
Nov 29 '18 at 6:50
What is wrong with your current approach?
– Tim Biegeleisen
Nov 29 '18 at 6:40
What is wrong with your current approach?
– Tim Biegeleisen
Nov 29 '18 at 6:40
@TimBiegeleisen,
array_sum
would be faster than explode
and adding value, isn't it ? So I wanna know, can I do array_sum
in my case.– Dipak
Nov 29 '18 at 6:44
@TimBiegeleisen,
array_sum
would be faster than explode
and adding value, isn't it ? So I wanna know, can I do array_sum
in my case.– Dipak
Nov 29 '18 at 6:44
preg_grep and array_sum but I doubt it will be faster
– Andreas
Nov 29 '18 at 6:46
preg_grep and array_sum but I doubt it will be faster
– Andreas
Nov 29 '18 at 6:46
1
1
How big is your data set? If it isn't very big, there will most likely not be any noticeable performance differences.
– Magnus Eriksson
Nov 29 '18 at 6:50
How big is your data set? If it isn't very big, there will most likely not be any noticeable performance differences.
– Magnus Eriksson
Nov 29 '18 at 6:50
add a comment |
2 Answers
2
active
oldest
votes
strstr with substr should work just fine
<?php
$a = ['1-160','2-250', '3-380'];
$result = ;
foreach($a as $b) {
$result = substr(strstr($b, '-'), 1); // strstr => get content after needle inclusive needle, substr => remove needle
}
var_dump($result);
var_dump(array_sum($result));
https://3v4l.org/2HsAK
Yeah !! the clean way.
– Dipak
Nov 29 '18 at 6:57
add a comment |
Although you already have the answer, this is for your reference.
array_sum(array_map(function ($item) {return explode('-', $item)[1];}, $a));
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%2f53533164%2fremove-everything-before-including-special-character-in-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
strstr with substr should work just fine
<?php
$a = ['1-160','2-250', '3-380'];
$result = ;
foreach($a as $b) {
$result = substr(strstr($b, '-'), 1); // strstr => get content after needle inclusive needle, substr => remove needle
}
var_dump($result);
var_dump(array_sum($result));
https://3v4l.org/2HsAK
Yeah !! the clean way.
– Dipak
Nov 29 '18 at 6:57
add a comment |
strstr with substr should work just fine
<?php
$a = ['1-160','2-250', '3-380'];
$result = ;
foreach($a as $b) {
$result = substr(strstr($b, '-'), 1); // strstr => get content after needle inclusive needle, substr => remove needle
}
var_dump($result);
var_dump(array_sum($result));
https://3v4l.org/2HsAK
Yeah !! the clean way.
– Dipak
Nov 29 '18 at 6:57
add a comment |
strstr with substr should work just fine
<?php
$a = ['1-160','2-250', '3-380'];
$result = ;
foreach($a as $b) {
$result = substr(strstr($b, '-'), 1); // strstr => get content after needle inclusive needle, substr => remove needle
}
var_dump($result);
var_dump(array_sum($result));
https://3v4l.org/2HsAK
strstr with substr should work just fine
<?php
$a = ['1-160','2-250', '3-380'];
$result = ;
foreach($a as $b) {
$result = substr(strstr($b, '-'), 1); // strstr => get content after needle inclusive needle, substr => remove needle
}
var_dump($result);
var_dump(array_sum($result));
https://3v4l.org/2HsAK
answered Nov 29 '18 at 6:52
XatenevXatenev
5,83521235
5,83521235
Yeah !! the clean way.
– Dipak
Nov 29 '18 at 6:57
add a comment |
Yeah !! the clean way.
– Dipak
Nov 29 '18 at 6:57
Yeah !! the clean way.
– Dipak
Nov 29 '18 at 6:57
Yeah !! the clean way.
– Dipak
Nov 29 '18 at 6:57
add a comment |
Although you already have the answer, this is for your reference.
array_sum(array_map(function ($item) {return explode('-', $item)[1];}, $a));
add a comment |
Although you already have the answer, this is for your reference.
array_sum(array_map(function ($item) {return explode('-', $item)[1];}, $a));
add a comment |
Although you already have the answer, this is for your reference.
array_sum(array_map(function ($item) {return explode('-', $item)[1];}, $a));
Although you already have the answer, this is for your reference.
array_sum(array_map(function ($item) {return explode('-', $item)[1];}, $a));
answered Nov 29 '18 at 6:59
LunaLuna
326111
326111
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%2f53533164%2fremove-everything-before-including-special-character-in-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
What is wrong with your current approach?
– Tim Biegeleisen
Nov 29 '18 at 6:40
@TimBiegeleisen,
array_sum
would be faster thanexplode
and adding value, isn't it ? So I wanna know, can I doarray_sum
in my case.– Dipak
Nov 29 '18 at 6:44
preg_grep and array_sum but I doubt it will be faster
– Andreas
Nov 29 '18 at 6:46
1
How big is your data set? If it isn't very big, there will most likely not be any noticeable performance differences.
– Magnus Eriksson
Nov 29 '18 at 6:50