如何从php中的数组中获取特定数据

时间:2013-09-06 15:17:33

标签: php arrays

我希望从数组中检索特定数据,例如

$array[] = "test type: human; Including all the test; number of participants 2 persons + additional  $50";

我只考虑以下类型的信息:

输出:

array[1] test type: human; 
array[2] 2

我应该只能在输出中显示这些信息。每种情况的测试类型都会有所不同,其他订单的参与者数量会有所不同。 我想从给定的数组中只获得上述基本信息。我研究了通过半冒号爆炸给定的数组,然后使用子字符串来获得所需的结果。但寻找任何有效的方法来检索数据。 任何帮助或建议真的很感激。

4 个答案:

答案 0 :(得分:5)

你似乎误解了数组与字符串。

阵列
http://php.net/manual/en/language.types.array.php

字符串(在变量中)
http://www.php.net/manual/en/language.variables.basics.php

你可以使用你目前拥有的变量和字符串方法,但诚实地说,你通过在单个变量/字符串中使用有效的不同键和值来为自己设置一个痛苦的世界。这就是阵列的用途!

字符串是单个实体,可能是许多字符或单词等,但是当你想要一件事时应该使用它。 $ TheName =“Bob”;
数组是指您想要一次性存储,操作和使用多个字符串或数据集。即加载数据库结果。

因此,在我看来你需要使用一个数组。要使您的代码成为数组,您可以执行以下操作:

$array = array(
               "test type" => "human", 
               "number of participants" => "2 persons", 
                "additional" => "$50"
               );

数组的KEY是“”中的第一个文本,然后使用=>为该KEY分配VALUE,然后引号中的下一个文本为VALUE。用逗号分隔每个KEY和VALUE对,数组中的最后一个KEY和VALUE对不需要逗号。

以上是手动创建的,但是您可以在数据库查询的循环中创建它。用上面的数据库列名替换手动分配的KEY名称,VALUE可能是字段的数据。

至于你的问题“从数组中获取特定数据”,然后循环数组,回显或将每个KEY的VALUE分配给变量。
例如

foreach ($array as $key => $value)
  {
    echo "The ."$key." is ".$value;
  }

您真的需要阅读一些基础知识的教程。这是一个Q& A网站,虽然我们会帮助您解决问题,但在提出问题之前,您需要了解编码内容的基础知识。
如上所述,答案不是问题的答案,而是可以在Stack Exchange网站,论坛和教程中找到的指南或教程:)

答案 1 :(得分:2)

你误解了PHP中stringsarrays之间的区别。区分两者是批判。我假设您的数据是字符串格式。

抢救字符串操作!如果您的数据没有可识别的模式或格式,则难以提取所需信息。如果您的字符串始终test type:开头且后面的值始终以分号结尾,您可能会执行以下操作(不理想,但很简单) :

$str = 'test type: human; Including all the test; number of participants 2 persons + additional  $50';
$testType = substr($str, 11, strpos($str, ';')-11); // 11 == len('test type: ')

同样,如果您的字符串在所需数据之前始终包含文本number of participants,并且在该数据之后紧​​跟空格,则可以使用:

$start = strpos($str, 'number of participants') + 23;
$numPart = substr($str, $start, strpos($str, ' ', $start+1) - $start);

我为了简洁而对索引进行了硬编码,但它们很容易被字符串搜索功能(尤其是正则表达式)取代。

资源:

答案 2 :(得分:2)

我有两种可能的方式来获得你的结果,以确保有很多其他的,但它不是最简单的。

如果您的STRING永远不会改变,您可以使用选项2,否则选项1

<?php

$array[] = "test type: human; Including all the test; number of participants 2 persons + additional  $50";

// option 1
if (preg_match_all('#test type:(.*);#Usi', $array[0], $a)) {
    echo 'test type --> '.trim(implode($a[1]));
}

if (preg_match_all('#participants(.*)persons#Usi', $array[0], $a)) {
    echo '<br />participants --> '.trim(implode($a[1]));
}
// end option 1    

    //just to show string as real array
    $arr = explode(' ', $array[0]);
    echo '<pre>';
        var_dump($arr);
    echo '</pre>';

    echo '<br /><br /><br />';
    // end show

//option 2 , only works if the array, see var_dump from above is always like that
echo 'test type --> '.substr($arr[2], 0, -1);
echo '<br />participants --> '.$arr[10];
// end option 2

?>

答案 3 :(得分:1)

为什么不将数据拆分成单独的块并将其放入多维数组中,如下所示:

$array[] = explode('; ', "test type: human; Including all the test; number of participants 2 persons + additional  $50");

这会给你:

Array
(
    [0] => Array
        (
            [0] => test type: human
            [1] => Including all the test
            [2] => number of participants 2 persons + additional  $50
        )

)

如果初始字符串的每个部分都有一个键和一个冒号(即“键:一些细节”),它将使它更容易使用,如下所示:

$initial_data = explode('; ', "test type: human; Including: all the test; number of participants: 2 persons + additional  $50");
$clean_up = array();
foreach ($initial_data as $data) {
  $clean = explode(': ', $data);
  $clean_up[$clean[0]] = $clean[1];
}
$array[] = $clean_up;

这会使你的数组​​看起来像这样:

Array
(
    [0] => Array
        (
            [test type] => human
            [Including] => all the test
            [number of participants] => 2 persons + additional  $50
        )

)