如果PHP具有来自另一个数组的特定字符串,则获取数组值

I have multiple files that I am trying to consolidate into 1 single file. Each file has something similar to this:

var nr = ['1.02.3.1','1.02.3.4'];
var p = [{"ip":"1.02.3.1","parents":["1.02.3.0","1.1.2.2"},{"ip":"1.04.1.1","parents":["1.1.30.0","1.15.2.2"}];

Keep in mind, I am trying to parsing the information as text for now.

Different files have different IPs, (IPs being the values within the lists. I consolidate 2 files at a time. For the nr array, it's easy. All I did was strip the unnecessary data to leave me with '1.02.3.1','1.02.3.4' then I exploded the string into an array. I merge the array with the other file's array, take out duplicates, implode the data, add the "var nr =[" and "];"

I am having trouble with the next part. I want to cross check "p" with "nr". With the consolidated "nr" array. If any of the values of "nr" match the ip of "p, then save write the ip to a new array called "nrp" and save the list of parents from that ip to the consolidated "p" array. Also, I want to delete the ip and parent ips that are not listed by "nr".

For example, I will represent ip as letters

File A:

var nr = ['A','B','C'];
var p = [{"ip":"A","parents":["J","K","L"]},{"ip":"B","parents":["J","K","L"]},{"ip":"C","parents":["K","L","M"]} ];

File B:

var nr = ['A','C','D'];
var p = [{"ip":"A","parents":["J","K","L"]},{"ip":"D","parents":["N","O","P"]},{"ip":"C","parents":["K","L","M"]} ];

File consolidate:

var nr = ['A','B','C','D'];
var p = [{"ip":"A","parents":["J","K","L"]},{"ip":"B","parents":["J","K","L"]},{"ip":"D","parents":["N","O","P"]},{"ip":"C","parents":["K","L","M"]}];
var nrp = ['J','K','L','N','O','P','M'];

My approach so far: This is after I have the data in arrays.

$consolidatedNR = ['A','B','C','D'];
$consolidatedP = [{"ip":"A","parents":["J","K","L"]},{"ip":"B","parents":["J","K","L"]},{"ip":"D","parents":["N","O","P"]},{"ip":"C","parents":["K","L","M"]}];

foreach($consolidatedP as $key5 => &$string5){
            foreach($consolidatedNR as $key2 => &$ip){
                if($string5 contains "'ip':$ip"){
                    array_push($content4, $ip);
                }
                else{
                    unset($consolidatedP[$key5]);
                }
            }
        }

The arrays that you have are acceptable JSON formats. You can remove the JavaScript assignments from the string, explode the string into two lines and then use json_decode() to convert the lines into PHP arrays.

This should work for you:

Edit: I've edited and tested this code.

<?php

$str = <<<EOF
var nr = ['1.02.3.1','1.02.3.4'];
var p = [{"ip":"1.02.3.1","parents":["1.02.3.0","1.1.2.2"]},{"ip":"1.04.1.1","parents":["1.1.30.0","1.15.2.2"]}];
EOF;

$str = str_replace(array("var nr = ", "var p = ", ";"), "", $str);
$str = str_replace("'", "\"", $str);
$str = explode("
", $str);

$parents_list = array();
$ip_list = array();

$nr = json_decode($str[0], true);
$p = json_decode($str[1], true);
echo "<pre>";
foreach($p as $p_list) {
    if(in_array($p_list['ip'], $nr)) {
        array_push($ip_list, $p_list['ip']);
        $parents_list = array_merge($p_list['parents'], $parents_list);
    }
}

echo "Parents list:<pre>";
print_r($parents_list);
echo "</pre>IP list:<pre>";
print_r($ip_list);
echo "</pre>";

?>