dswsl2016 2017-11-02 20:13
浏览 46
已采纳

如何将String Array转换为Real数组PHP

screenshot

Hello, as you can see in the screenshot, when i extract data from a site with PHP Simple HTML DOM Parser. I have this result , so i would like to convert this data to a real array to have the real control and the opportunity to access with $array['label']

<?php include_once('simple_html_dom.php'); ?>
<!DOCTYPE html>

<html>
<head>

<title> HTML DOM Parser</title>
</head>
<body>
<?php
header('Content-Type: text/html; charset=utf-8');
set_time_limit(0);
$html=file_get_html('https://www.monreseauplus.com/villes/');
$array[]=array();
$array3[]=array();
foreach($html->find('.ul. li.cat-item a') as $elements){
    $array2=$elements->title;
    $array=str_replace(':','=>',$array2);
    $arraynospec=htmlspecialchars_decode($array);
    var_dump($arraynospec);
    }
?>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • dongyu3659 2017-11-02 20:22
    关注

    So it looks like you are trying to string manipulate JSON to try to make some sort of associative array. Replacing the : with => is not the right move here...

        <?php include_once('simple_html_dom.php'); ?>
        <!DOCTYPE html>
    
        <html>
        <head>
    
        <title> HTML DOM Parser</title>
        </head>
        <body>
        <?php
        header('Content-Type: text/html; charset=utf-8');
        set_time_limit(0);
        $html=file_get_html('https://www.monreseauplus.com/villes/');
        $array[]=array();
        $array3[]=array();
        foreach($html->find('.ul. li.cat-item a') as $elements){
            $array2=$elements->title;
            $array=json_decode($array2,true)
            $arraynospec=htmlspecialchars_decode($array);
            var_dump($arraynospec);
        }
        ?>
        </body>
        </html>
    

    Try that

    This is the problem: $array=str_replace(':','=>',$array2); Looks like it was already in JSON!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看