0

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?

ron
  • 175
  • 3
  • 14

2 Answers2

1

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
        )

)
Jaydeep Mor
  • 1,690
  • 3
  • 21
  • 39
1

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.

Alex Mac
  • 2,970
  • 1
  • 22
  • 39