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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?