doutuo3935 2014-01-10 20:06
浏览 52
已采纳

如果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]);
                }
            }
        }
  • 写回答

1条回答 默认 最新

  • douhuangjian9627 2014-01-10 20:16
    关注

    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>";
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题