dousuohe5882 2013-11-09 23:19
浏览 177
已采纳

PHP - 如何将数组从(键,值)转换为(键,值,值)?

I have a table in a HTML file, using my CODE below I have managed to get all values from this table as a Array.

The problem is my CODE makes Array as: (Key -> Value) (Key -> Value). But the structure of my data is: (Key -> Value, Value) (Key -> Value, Value)

Here is Content of: myHTMLfile.html

<td height="22" align="center" bgcolor="#FFFFFF">1 Hour </td>
<td align="center" bgcolor="#FFFFFF">400 USD</td>
<td align="center" bgcolor="#FFFFFF">450 USD</td>
<td height="22" align="center" bgcolor="#FFFFFF">2 Hours</td>
<td align="center" bgcolor="#FFFFFF">500 USD</td>
<td align="center" bgcolor="#FFFFFF">600 USD</td>
<td height="22" align="center" bgcolor="#FFFFFF">3 Hours </td>
<td align="center" bgcolor="#FFFFFF">600 USD</td>
<td align="center" bgcolor="#FFFFFF">700 USD</td>

Im using this CODE to get content and make this Array:

$file = ("myHTMLfile.html");
$searchfor = 'align="center" bgcolor="#FFFFFF"';                     

header('Content-Type: text/html');   
$html = file_get_contents($file);

$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*\$/m";
if(preg_match_all($pattern, $html, $matches)); 

function remap_alternating(array $values) {
    $remapped = array();
    for($i = 0; $i < count($values) - 1; $i += 2) {

$remapped[strip_tags(trim($values[$i]))] = strip_tags(trim($values[$i + 1]));
    }
    return $remapped;
}
$mapped = remap_alternating($matches[0]);
// RAM 
$keys = array_map("trim", array_map("strip_tags", array_keys($mapped)));
$values = array_map("trim", array_map("strip_tags", array_values($mapped)));
$mapped = array_combine($keys, $values); 

Below you can see the results I get from var_dump($mapped);

array(4) {
  ["1 Hour"]=>
  string(7) "400 USD"
  ["450 USD"]=>
  string(7) "2 Hours"
  ["500 USD"]=>
  string(7) "600 USD"
  ["3 Hours"]=>
  string(7) "600 USD"
}

I want this Array results to look like:

array(3) {
  ["1 Hour"]=>
  string(16) "400 USD, 450 USD"
  ["2 Hours"]=>
  string(16) "500 USD, 600 USD"
  ["3 Hours"]=>
  string(7) "600 USD"
}

My question is: What is the correct PHP programming CODE to get the results as I want?

UPDATED BELOW

I found a example and it looks like its what I need. But how can I use it with my CODE above?

Example:

$get = "first=value1&second=value2&third=value3";

print_r(array_map2("explode","=",explode("&",$get)));

would print out:

Array
(
    [0] => Array
        (
            [0] => first
            [1] => value1
        )

    [1] => Array
        (
            [0] => second
            [1] => value2
        )

    [2] => Array
        (
            [0] => third
            [1] => value3
        )
)
  • 写回答

2条回答 默认 最新

  • dongqianzong4275 2013-11-09 23:58
    关注

    try this:

    function remap_alternating(array $values) {
        $remapped = array();
        for($i = 0; $i < count($values) - 1; $i += 3) {
        $remapped[strip_tags(trim($values[$i]))] = array(strip_tags(trim($values[$i + 1])), strip_tags(trim($values[$i + 2])));
        }
        return $remapped;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算