从句子中制作数组

时间:2015-08-04 08:02:55

标签: php

我从数据库中得到了这句话

0| Future plan 1| Low 2| Normal 3| High 4| Highest

我需要让它看起来类似于:

[0] => Future plan
[1] => Low
[2] => Normal
[3] => High
[4] => Highest

我无法找到任何类似于此的线程。请帮助。

7 个答案:

答案 0 :(得分:5)

我发现了一些应该为你做的事:preg_split

$string = "0| Future plan 1| Low 2| Normal 3| High 4| Highest";
$array = preg_split("/[0-9]+\|/", $string, -1, PREG_SPLIT_NO_EMPTY);

答案 1 :(得分:2)

@ankhzet指出了这个答案所涵盖的一个缺陷。

<?php
$v              = '0| Future plan 22 2| Normal 123| Low 3| High 4| Highest';
$arraySplit     = preg_split("/([0-9]+)\|/", $v, -1, PREG_SPLIT_DELIM_CAPTURE);
$arrayResult    = array();
for($i = 1; $i < count($arraySplit); $i+=2){
    $arrayResult[$arraySplit[$i]] = $arraySplit[$i+1];
}
pre($arrayResult);
?>

新输出:

Array
(
    [0] => Future plan 22
    [2] => Normal
    [123] => Low
    [3] => High
    [4] => Highest
)

<强> OLD

此方法将保持ID,并且比@ ankhzet的答案更短。 如果您需要,请点击这里:

preg_match_all("/(\d+)\|([^0-9]+)/", $v, $array);
$array = array_combine($array[1], $array[2]);
pre($array);

输出如下:

Array
(
    [0] =>  Future plan 
    [2] =>  Normal 
    [1] =>  Low 
    [3] =>  High 
    [4] =>  Highest
)

仅供参考,pre()只是在print_r()代码中包裹<pre></pre>的函数。

答案 2 :(得分:1)

首先更改数据库句子,然后在您的情况下使用php函数explode ("| " , $string)。此函数可以通过分隔符将字符串分解为数组。

答案 3 :(得分:1)

试试这个:

private List<MyListClass> myList;
private MyListAdapter myListAdapter;

public void onCreate(Bundle savedInstanceState){
    myList = new ArrayList<>();

    //Setting the adapter here means it's only set once
    myListAdapter = new SimpleAdapter(this, myList,
        R.layout.single_post, new String[] { TAG_ID }, new int[]{R.id.ID);

    ListView listView = getListView();
    listView.setAdapter(myListAdapter);

    FetchListTask(new OnTaskCompletedListener(){
        public void onTaskCompleted(List<MyListClass> result){
            myList.addAll(result);
            MyListAdapter.notifyDataSetChanged();
        }
    }).execute();
}

public GetListTask extends AsyncTask<Void, Void, List<MyListClass>>{
    private OnTaskCompletedListener onTaskCompletedListener;    

    public GetListTask(OnTaskCompletedListener onTaskCompletedListener){
        this.onTaskCompletedListener = onTaskCompletedListener;
    }

    protected List<MyListClass> doInBackground(){
        //Your business logic here...
        return list; 
    }

     protected void onPostExecute(List<MyListClass> result) {
        //Callback to notify your activity that the task is done
         OnTaskCompletedListener.onTaskCompleted(result);
     } 


}

public interface OnTaskCompletedListener{
    public void onTaskCompleted(List<MyListClass> result);
}

答案 4 :(得分:1)

使用explode

$words = preg_replace('/[0-9]+/', '', $string);//remove all numbers from string
$array = explode ("|" , $words);
$array2 = array_filter($array)//remove empty elements

答案 5 :(得分:1)

鉴于你的字符串中有|,我做了以下(必须进行额外的检查):

$string = '0| Future plan 1| Low 2| Normal 3| High 4| Highest';
$string = str_replace('|','',$string); //replace |
$length = strlen($string); //get length
for($i=0; $i<=$length-1;$i++)
{
    if(is_numeric($string[$i])) { //get numbers
        $word[$string[$i]] ='';
        $lastIndex = $string[$i]; //save last found
    } else {
        if($string[$i]!=' ') { //if not space append to array element of last number found
            $word[$lastIndex] .= $string[$i];
        }
    }
}
var_dump($word);

array (size=5)
  0 => string 'Futureplan' (length=10)
  1 => string 'Low' (length=3)
  2 => string 'Normal' (length=6)
  3 => string 'High' (length=4)
  4 => string 'Highest' (length=7)

答案 6 :(得分:1)

由@Epodax建议的REGEXP不会保留索引:

$v = '0| Future plan 2| Normal 1| Low 3| High 4| Highest';
$array = preg_split("/[0-9]+\|/", $v, -1, PREG_SPLIT_NO_EMPTY);

dump($array);
// array:5 [▼
//   0 => " Future plan "
//   1 => " Normal "
//   2 => " Low "
//   3 => " High "
//   4 => " Highest"
// ]

如果重要,最好是使用它:

$v = '0| Future plan 2| Normal 1| Low 3| High 4| Highest';
$v .= '0|';
$r = [];
while ($v) {
  if (!preg_match('/(\d+)\|(.+?)(?:(\d+)\|)/', $v, $m, PREG_OFFSET_CAPTURE))
    break;

  $idx = intval($m[1][0]);
  $r[$idx] = trim($m[2][0]);

  if (($offset = $m[3][1]) >= strlen($v))
    break;

  $v = substr($v, $offset);
}

dump($r);
// array:5 [▼
//   0 => "Future plan"
//   2 => "Normal"
//   1 => "Low"
//   3 => "High"
//   4 => "Highest"
// ]