在多维数组中搜索特定键的值,并返回不同键的值

问题描述:

我是php的新手,并且一直在使用社区和解答来真正帮助我完成一个正在进行的小项目,因此在此先感谢大家的帮助!

I'm new to php and have been using the community and answers here to really help me with a little project I'm working on so thank you all in advance for the help so far!

我要提取格式不正确的文本文件/供稿中包含的大量信息,修剪特殊字符的内容,然后使用 str_replace 来查找其他特定的字符串,并将其替换为逗号(,)或分号(;),以创建可用的文本。然后,我想在此文本中搜索某些关键字,然后在原处返回文本的其他部分。

I am pulling a load of information held in a poorly formatted text file/feed, trimming the contents of special characters and then using str_replace to find other specific strings and replace them with commas(,) or semi-colons(;), in order to create a usable piece of text. I then want to search this text for certain keywords and return other parts of the text in it's place.

到目前为止,我已经设法将文本分解为多维数组,但是现在我不知道如何搜索该数组以便提取出来一条特定的信息。我实质上是在尝试构建一个可搜索的数组,以便在原始Feed更新时从中获取信息。这是当前数组的示例:

So far, I've managed to explode the text into a multidimensional array, but I can't work out how to search this array now, in order to pull out a specific piece of information. I'm essentially trying to build a searchable array that I can pull information from as and when the original feed updates. Here's a sample of the array as it stands at the moment:

Array
(
 [0] => Array
    (
        [0] => 240
        [1] => 1
        [2] => euro
        [3] => 2016-02-19 15:30:00
        [4] => EUR
    )

 [1] => Array
    (
        [0] => 240
        [1] => 3
        [2] => euro2
        [3] => 2016-02-19 15:00:00
        [4] => EUR
    )

 [2] => Array
    (
        [0] => 1890
        [1] => 9
        [2] => uspb
        [3] => 2016-02-17 22:59:00
        [4] => USD
    )
)

基本上,我希望能够写点东西将在此数组中搜索 uspb (数组2,键2),如果找到,则返回另一个键下的值。因此,如果我想要键0,它将返回 1890 。如果在搜索 euro2 时需要键1,它将返回 3。

Essentially, I want to be able to write something that will search this array for say uspb (array 2, key 2) and if it is found, return the value held under another key. So if I want key 0, it will return 1890. If I want key 1 when searching for euro2 it will return "3".

我浏览了很多示例,但现在并没有什么适合我的。也许我看错了方法,使用数组不是正确的方法。任何建议将不胜感激。

I've looked through a ton of examples and nothing really fits what I'm after at the moment. Perhaps I'm looking at this the wrong way and using an array isn't the correct approach. Any advice would be greatly appreciated.

作为参考,这是到目前为止我的代码副本(略作编辑)。

For reference, here's a copy of my code (slight redacted) so far.

<?php 
    $file=file_get_contents("http://www.example.com/feed/");
    $trim=trim($file, "[]");
    $find = array("{\"value\":\"", "\",\"date_utc\":\"", "\",\"currency\":\"");
    $replace = array(",", ",", "");
    $replaced = str_replace($find, $replace, $trim);

$ret = array_map (
  function ($_) {return explode (',', $_);},
  explode (';', $replaced)
);

print_r ($ret);
    ?>


由于数组是多维的,因此必须对其进行迭代以找到所需的值:

As your array is multidimensional - you have to iterate over it to find the value you need:

foreach ($ret as $value) {
    // the index you want to search is always `2`?
    if ($value[2] == 'uspb2') {
        echo $value[0];
        break;
    }
}

并转到函数:

function findMyValue(
    $array, 
    $search_key,
    $search_str, 
    $key
) {
    foreach ($array as $v) {
        if ($v[$search_key] == $search_str) {
            return $v[$key];
        }
    }
    return 'NOT_FOUND';
}

echo findMyValue($ret, 2, 'euro', 1); // outputs 3
echo findMyValue($ret, 2, 'uspb', 0); // outputs 1890

而且注释中已经注意到-它不是格式较差的文本,而是JSON。您可以使用 json_decode 函数从JSON字符串中获取数组:

And as already noticed in comments - it's not a poorly formated text, it's JSON. You can get an array from JSON string simply with json_decode function:

$file=file_get_contents("http://www.example.com/feed/");
$ret = json_decode($file, true);
var_dump($ret);