从php中的数组获取值并爆炸

时间:2013-08-30 10:52:09

标签: php

我的数组中的值为

$itsthere = (item1-0-100, item2-0-50, item3-0-70, item4-0-50, item5-0-100);

如果用户输入值item3,则必须获得数组中存在的70。我尝试了很多使用爆炸但它没有显示正确的价值。任何人都可以帮助我。

3 个答案:

答案 0 :(得分:5)

尝试:

$itsthere = array(
  'item1-0-100',
  'item2-0-50',
  'item3-0-70',
  'item4-0-50',
  'item5-0-100'
);

$search = 'item3';
$output = '';

foreach ( $itsthere as $value ) {
  if ( strpos($search . '-', $value) === 0 ) {
    $output = explode('-', $value)[2];
    break;
  }
}

答案 1 :(得分:1)

当你说item3是你引用第三个位置或item3作为数组键?我认为您可以创建一个关联数组并将项目名称设为关键

$isthere = array('item1' => '0-100', 'item2' => '0-50' ,'item3' => '0-70', ....,);
echo $isthere['item3']; // This would give you the value.

如果您只想知道此键是否在数组中,请使用array_key_exists。

答案 2 :(得分:-1)

试试这个:

    $item = 'item3';
    $itsthere = array('item1-0-100', 'item2-0-50', 'item3-0-70', 'item4-0-50', 'item5-0-100');

    foreach($itsthere as $there)
    {
        $exp = explode('-',$there);
        if($exp[0] == 'item3') {
            echo $val = $exp[2];
        };
    }
相关问题