dousuohe5882 2013-11-09 15:19
浏览 178
已采纳

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 15: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条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 电脑买回,学校的有线网络总掉。
  • ¥20 关于普洛菲斯触摸屏与AB连接地址问题
  • ¥15 vue但是页面显示的数据为空为什么呀,明明在钩子函数中已经成功赋值(相关搜索:输出数据)
  • ¥15 syri可视化不显示插入缺失
  • ¥30 运行软件卡死查看系统日志分析不出来
  • ¥15 C语言代码改正特征选择算法设计,贝叶斯决策,,设计分类器,远程操作代码修正一下
  • ¥15 String 类valuve指向的问题
  • ¥15 在ros2的iron版本进行编译时遇到如下问题
  • ¥18 vs用setup project打包项目实现安装完立即运行
  • ¥15 matlab gui如何修改并保存cell中的文件信息
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部