简体   繁体   中英

How to split array in php with 2 strings?

How to split array with , in 2 parts so that that can be used as 2 strings in php?

I am getting this kind of array you can view image with this list. I want to split this array to post id and title in separate columns.

<li  id="recordsArray_'.$value['id'].','.$value['title'].'"></li>

在此处输入图片说明

If you want to separate array into two string, you can use explode like this short code:

$item = '4,test';
$var = explode(',', $item);
echo $var;

and for loop array, you can use

$array = array(YOUR_ARRAY);
foreach($array as $item){
    $var = explode(',', $item);
    echo $var;
}

Update 1: You can use a simple array like below code:

$array = array(4, 'salar');

and use a code like this:

$array = array(4, 'salar');
foreach ($array as $item) {
    dump(explode(',', $item));
}

Give a try with below code if it works for you

$arr = array('1,test1','2,test2','3,test3');

foreach($arr as $ar){
  $res = explode(',', $ar);
  $id = $res[0];
  $title = $res[1]; 

  echo 'id-'.$id;
  echo ' title-'.$title;
  echo '<br>';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM