For example I have an array
$array[] = ['name - ic'];
The result I want is
$new_Array[]=['name'];
How do I remove the string that start from the -
since the name and ic will be different for everyone? Anyone can help?
For example I have an array
$array[] = ['name - ic'];
The result I want is
$new_Array[]=['name'];
How do I remove the string that start from the -
since the name and ic will be different for everyone? Anyone can help?
Using explode
method you can split string into array.
PHP
<?php
$array[] = ['name - ic'];
$array[] = ['name - bc - de'];
$new_Array = array();
foreach($array as $key=>$values){
foreach($values as $k=>$val){
$str = explode("-",$val);
$new_Array[$key][$k] = trim($str[0]);
}
}
?>
OUTPUT
Array
(
[0] => Array
(
[0] => name
)
[1] => Array
(
[0] => name
)
)
You can use explode function for the same.
$array = array('name - ic','abc - xyz','pqr-stu');
$newArray = array();
foreach($array as $obj):
$temp = explode('-',$obj);
$newArray[] = trim($temp[0]);
endforeach;
print_r($newArray);
Result
Array ( [0] => name [1] => abc [2] => pqr )
Let me know if it not works.