Andy Andy - 5 years ago 183
PHP Question

Search multidimensional array for value in certain key, return value of different key

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!

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.

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
)
)


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);
?>

Answer Source

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;
    }
}

And moving to function:

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

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download