从数组中的单个多值行中提取一个值

时间:2012-12-29 01:17:32

标签: php regex arrays

  

可能重复:
  In PHP, is there a function that returns an array made up of the value of a key from an array of associative arrays?

多个值存储在数组中的一行中,如下所示:

Array
(
  [data] =>
     Duration: 1 Month
     Name: James Foo
     Category: Info Tech
)

问题:

如何分别从[data]中提取和回显值?即仅显示Category: Info Tech

2 个答案:

答案 0 :(得分:1)

如果你更喜欢正则表达式,你可以这样做:

$array = array('data' =>
'Duration: 1 Month
Name: James Foo
Category: Info Tech');

function get_line($array, $line)
{
    preg_match('/' . preg_quote($line) . ': ([^\n]+)/', $array['data'], $match);
    return $match[1];
}

echo get_line($array, 'Category');

答案 1 :(得分:0)

foreach ($array as $k => $v)
    echo "Category: ".$v['Category']."\n";