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))); 

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

Popular posts from this blog

design - Custom Styling Qt Quick Controls -

Unable to remove the www from url on https using .htaccess -