1

我需要以下结构的数组:

Array
(
[www.example.com] => Array
    (
        [0] => http://www.example.com/
        [1] => http://www.example.com/something.html
        [2] => http://www.example.com/anything.html
    )

[www.beispiel.com] => Array
    (
        [0] => http://www.beispiel.com/product-services/
        [1] => http://www.beispiel.com/non-disclosure-agreement/
    )
 )

作为输入数组,我有以下两个

Array ( [0] => http://www.example.com/
        [1] => http://www.example.com/something.html
        [2] => http://www.example.com/anything.html
        [3] => http://www.beispiel.com/product-services/
        [4] => http://www.beispiel.com/non-disclosure-agreement/
      ) 

Array ( [0] => www.example.com [1] => www.beispiel.com ) 

此刻我有这样的事情:

Array
(
[0] => Array
    (
        [www.example.com] => http://www.example.com/
    )

[1] => Array
    (
        [www.example.com] => http://www.example.com/something.html
    )

[2] => Array
    (
        [www.example.com] => http://www.example.com/anything.html
    )

[3] => Array
    (
        [www.beispiel.com] => http://www.beispiel.com/product-services/
    )

[4] => Array
    (
        [www.beispiel.com] => http://www.beispiel.com/non-disclosure-agreement/
    )

)

这是可能的还是我错过了一些关于 php 的信息(例如 key 不能是解析的 url(主机))?

4

2 回答 2

1

尝试:

$arr1 = array('http://www.example.com/','http://www.example.com/something.html','http://www.beispiel.com/product-services/' ...); // 1st input array
$arr2 = array('www.example.com', 'www.beispiel.com'); // 2nd input array
$arr1_val = array_values($arr1);
foreach($arr2 as $v) {
 foreach($arr1 as $m) {       
   if(strpos($m, $v) !== FALSE)
    $new_arr[$v][] = $m;
  }      
}

演示

于 2012-12-06T17:46:26.287 回答
0

这不可能。与变量一样,相同的命名规则适用于数组键。

于 2012-12-06T17:41:29.120 回答