'How to post array with PHP

i have an array in php

loadHTMLFile(index.html):

<body>
  <a href="https://google1.co.il">google</a>
  <img src="https://www.leoweekly.com/wp-content/uploads/2018/01/Modern_DSA.jpg" alt="">
  <a href="https://google2.co.il">google</a>
  <a href="https://google3.co.il">google</a>
</body>

index.php

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    Name: <input type="text" name="fname">
    <input type="submit">
</form>

<?php
$html = 'index.html';
$dom = new DOMDocument();
$dom->loadHTMLFile($html);
$array = array();

foreach ($dom->getElementsByTagName('a') as $link) {
    array_push($array, $link->attributes->getNamedItem("href")->value);
}
foreach ($dom->getElementsByTagName('img') as $img) {
    array_push($array, $img->attributes->getNamedItem("src")->value);
}
?>

array:

Array ( [0] => https://google1.co.il 
        [1] => https://google2.co.il 
        [2] => https://google3.co.il 
        [3] => https://www.leoweekly.com/wp-content/uploads/2018/01/Modern_DSA.jpg 
)

i would like to know how can i make post method with the best solution

  1. with loop that send each index per request

or

2.all of them at the same time

*note: Actually it's a mission that i need to run this on google snippet, it's need to work on Google chrome from in snippets this is mean i need to make this from server only(without access to client), there is way to make it from server ?



Solution 1:[1]

You can use json_encode() for store array in json format and for get data use json_decode.    
<?php
    $html = 'index.html';
    $dom = new DOMDocument();
    $dom->loadHTMLFile($html);
    $array = array();
    
    foreach ($dom->getElementsByTagName('a') as $link) {
        array_push($array, $link->attributes->getNamedItem("href")->value);
    }
    foreach ($dom->getElementsByTagName('img') as $img) {
        array_push($array, $img->attributes->getNamedItem("src")->value);
    }
    $json_array = json_encode($array)
    ?>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1