2014-01-10 20:06


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

var nr = ['',''];
var p = [{"ip":"","parents":["",""},{"ip":"","parents":["",""}];

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 '','' 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);
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • douhuangjian9627 douhuangjian9627 7年前

    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.

    $str = <<<EOF
    var nr = ['',''];
    var p = [{"ip":"","parents":["",""]},{"ip":"","parents":["",""]}];
    $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>";
    echo "</pre>IP list:<pre>";
    echo "</pre>";
    点赞 评论 复制链接分享