-4

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>
sfjac
  • 7,119
  • 5
  • 45
  • 69
  • 2
    Screenshots aren't a very good way of sharing code. It's better to put it in a code block. – Matthew Daly Nov 02 '17 at 20:17
  • 2
    A *real* array? You will need to suuply more information than this, and documented examples of what you have tried, and researched. And yes, a screenshot of a mess is not really helpful for others to be helpful. – IncredibleHat Nov 02 '17 at 20:18
  • Check this out: https://stackoverflow.com/questions/684553/convert-php-array-string-into-an-array – acaputo Nov 02 '17 at 20:19
  • Maybe this web site can provide you JSON data – YouneL Nov 02 '17 at 20:23
  • This is my code : $array2=$elements->title; $array=str_replace(':','=>',$array2); $arraynospec=htmlspecialchars_decode($array); $json = json_encode($arraynospec); var_dump(json_decode($json)); – Anas Valencia Nov 02 '17 at 20:36

1 Answers1

0

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!

acaputo
  • 190
  • 12
  • firstly , thank you for your reply but this doesn't work for me as you can view in this screen :https://img4.hostingpics.net/pics/171183oop.png . i use var_dump to show data in this example – Anas Valencia Nov 02 '17 at 20:34
  • Did you try unserialize? – acaputo Nov 02 '17 at 20:58
  • when i use unserialize i have this error : unserialize(): Error at offset 0 of 54 bytes – Anas Valencia Nov 02 '17 at 21:08
  • check this out https://stackoverflow.com/questions/10152904/unserialize-function-unserialize-error-at-offset Looks like it's due to special characters – acaputo Nov 02 '17 at 21:13
  • i use base64_encode(serialize($data)); and $array3=unserialize(base64_decode($string)) but this shows me the same result : https://img4.hostingpics.net/pics/171183oop.png – Anas Valencia Nov 02 '17 at 21:28
  • It's because it wasn't serialized with base64_encode. That won't help, so you'll have to escape all the special characters first. – acaputo Nov 02 '17 at 21:38
  • I posted all the code if you want test it to have a better idea where is the problem – Anas Valencia Nov 02 '17 at 21:50
  • So you're trying to string manipulate what is already JSON into some sort of associative array. I'll update my answer with code – acaputo Nov 02 '17 at 21:52
  • the result: https://img4.hostingpics.net/pics/231130DADO.png . Always null – Anas Valencia Nov 02 '17 at 22:16
  • I added htmlspecialchars_decode and this is the result finaly : https://img4.hostingpics.net/pics/865982DADDSQ.png – Anas Valencia Nov 02 '17 at 22:21
  • Could be special characters. Try running your string through a function like in this answer before json_decoding it https://stackoverflow.com/questions/12911536/json-decode-with-special-chars – acaputo Nov 02 '17 at 22:22
  • Great! Glad it ended up working out. So what the site was giving you was actually JSON which is a way of representing objects. You actually made it harder on yourself be str replacing the : to a => :) – acaputo Nov 02 '17 at 22:25
  • htmlspecialchars_decode was the solution, thank you a lot alec !!!! another question if i can: can i split array into 2 array (because as you can see in the picture there is two array one with size 2 and another with size 7) because i should store this data into different table in Mysql database – Anas Valencia Nov 02 '17 at 22:25
  • So if you want the value of, say, "label" of the second array you could do: `$arraynospec[1]['label']` which would give you the value "Authier" OR `$newArray = arraynospec[1];` would give you the second array in $newArray – acaputo Nov 02 '17 at 22:29