i have following array:
$phones = array( [0] => apple iphone 4 [1] => apple iphone 5 [2] => samsung galaxy s6 ) what i'd split array up, separating brand model of phone, in end can build array give me this:
$phone_array = array ( 'apple' => array ( 'iphone 4', 'iphone 5' ), 'samsung' => array ( 'galaxy s6', ) ) so far, have following unfinished code:
$brand_dictionary = "/(samsung|apple|htc|sony|nokia)/i"; foreach($phones $p) { if(stripos($p,$brand_dictionary)) { pr($p); die(); } } but isn't working correctly @ all.
try -
$phones = array( 'apple iphone 4', 'apple iphone 5', 'samsung galaxy s6' ); $new = array(); foreach($phones $phone) { $temp = explode(' ', $phone); $key = array_shift($temp); $new[$key][] = implode(' ', $temp); } var_dump($new); output
array(2) { ["apple"]=> array(2) { [0]=> string(8) "iphone 4" [1]=> string(8) "iphone 5" } ["samsung"]=> array(1) { [0]=> string(9) "galaxy s6" } }
Comments
Post a Comment