php - Change array element (e.g. 4-10) into pipe separated list (e.g. 4|5|6|7|8|9|10) -
i have tried last 2 days searching on google , in forums, can't seem find answer remotely helps me problem .
i have stock feed .csv
file need change values of shoe sizes work woocommerce. shoe sizes different on each row.
the sizes in csv listed this: 4-10, 5-12, 3-9
etc. 1 set of numbers per row 4-10
. have inputed file array in php script.
so each shoe have array this:
array ( [0] => 4578 [1] => kors [2] => red [3] => wedge [4] => 4-10 )
i need take last value e.g. 4-10
, change them this: 4|5|6|7|8|9|10
.
so need take first number in element , increment 1 , separate pipe character " | "until reaches value of last number. need replace 4-10
in element 4|5|6|7|8|9|10
.
this should work you:
(here first last element of array , explode()
-
delimiter. after create array range()
use $start
, $end
variable. @ end save element implode()
'ing it.)
<?php $arr = [4578, "kors", "red", "wedge", "4-10"]; list($start, $end) = explode("-", $arr[count($arr)-1]); $arr[count($arr)-1] = implode("|", range($start, $end)); print_r($arr); ?>
output:
array ( [0] => 4578 [1] => kors [2] => red [3] => wedge [4] => 4|5|6|7|8|9|10 )
Comments
Post a Comment