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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效