dopod0901 2014-05-28 11:25
浏览 30
已采纳

php字典数组

I have multiple functions inside my php that look like this

function linkExtractor($html){ 
    $doc = new DOMDocument(); 
    $last = libxml_use_internal_errors(TRUE); 
    $doc->loadHTML($html); 
    libxml_use_internal_errors($last); 
    $xp = new DOMXPath($doc); 

    $result = array(); 
    foreach ($xp->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' infaa ')]") as $node) 
        $result[] = trim($node->textContent); 
    return $result;     
} 

And I am using this to turn them into json:

echo json_encode(array("info" => linkExtractor($html),
"dates" => linkExtractor2($html),
"names" => linkExtractor3($html),
"images" => linkExtractor4($html),
"genres" => linkExtractor5($html)
));

But this is returning the json like this:

{
"name":["melter",...],
"date":["05/24/14",...],
"image":["pictu.jpg",...],
"genre":["art",...],
"info":["Lorem ipsum",...]
}

I am looking to batch them so the first of each result is put into a curly brackets like this:

[ 
{ 
"name": "melter", 
"date": "05/24/14", 
"image": "pictu.jpg",
"genre": "art",
"info": "Lorem ipsum"
},
...
]

How can I do this?


HTML snippet:

<table width="703" border="0" align="center">
<tr>
<th width="697" scope="col">
<div id='gopro-hero-3'>
<a  href="Bits&Bobs/gopro-hero-3.html"><img src="pictu.jpg" alt="" width="700" height="525" class="images  gopro-hero-31" /></a>
</div>
</th>
</tr>
<tr>
<td valign="top" class="type" align="centre">
<table width="100%" border="0" align="right">
<tr>
<th width="48%" class="type genre" scope="col">art</th>
<th width="3%" class="" scope="col"> </th>
<th width="49%" class="date" scope="col">05/24/14</th>
</tr>
</table>
</td>
</tr>
<tr>
<td align="left">
<table width="100%" border="0">
<tr>
<th class="name" align="left" scope="col"><a  class="gopro-hero-31" href="Bits&Bobs/gopro-hero-3.html">melter</a>
</th>
</tr>
<tr>
  <th class="infaa" align="left" scope="col">Lorem ipsum</th>
</tr>
</table>
  • 写回答

2条回答 默认 最新

  • duanjue2560 2014-05-28 12:21
    关注

    I guess the arrays returned by the linkextractor functions are the same length.

    $arr = array();
    
    $info = linkExtractor($html);
    $dates = linkExtractor2($html);
    $names = linkExtractor3($html);
    $images = linkExtractor4($html);
    $genres = linkExtractor5($html);
    
    for ($i=0; $i<count($info); $i++) {
        $arr[] = array("info" => $info[$i], "date" => $dates[$i], "name" => $names[$i], "image" => $images[$i], "genre" => $genres[$i]);
    }
    
    echo json_encode($arr);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错