php - How do I get rid of the delimiters? -
case 1: have string "57_5". how take out underscore(_) supposedly (i think) acts delimiter may have 57 , 5? 2 values should integers not string!!! need in php
case 2: have array ("57_8", "45_7", "24_3"). need obtain each individual element in array, take out underscore convert them integers, in total should have 8 distinct values. how can achieved in php? should have 57, 8, 45, 7, 24, 3 remember end result should not array , values should integers.
as written in comment, 1 variant, "implode explode intval array_map things":
$integers = array_map('intval', explode('_', implode('_', (array) $arrayorstring)));
- casting/converting array
- http://php.net/implode
- http://php.net/explode
- http://php.net/intval
- http://php.net/array_map
thanks casting, handles both cases @ time normalizing input.
code-example:
<?php /** * how rid of delimiters? * @link http://stackoverflow.com/q/18484012/367456 */ $cases = [ 1 => "57_5", 2 => array ("57_8", "45_7", "24_3"), ]; foreach ($cases $num => $case) { $integers = array_map('intval', explode('_', implode('_', (array) $case))); echo 'case #', $num, ': ', var_dump($integers), "\n"; }
program output:
case #1: array(2) { [0]=> int(57) [1]=> int(5) } case #2: array(6) { [0]=> int(57) [1]=> int(8) [2]=> int(45) [3]=> int(7) [4]=> int(24) [5]=> int(3) }
see online demo.
Comments
Post a Comment