-1

The reason why I asked this array or object, is because I used echo is_array($news) ? 'Array' : 'not an Array'; and it showed it's not an array, is it an object then.

I know how to read from a php array. I basically have a formdata like this search array look like this

searcharray = ["yemen", "america", "franc", "germany"];
const json = JSON.stringify({searcharray });
const formData = new FormData(); 
 
formData.append('searcharray', json); 
formData.append('news', 'true'); 

then when I recieve it on php I do this

if (isset($_POST["news"]) )
{
$searcharrayx =  $_POST["searcharray"];
  $object = json_decode(json_encode($searcharrayx), FALSE);
echo is_array($object) ? 'Array' : 'not an Array';


// I tried this didn't work
$firstvalue = reset($object);
echo $firstvalue;

//I tried var dump that didn't work either get an error

//I tried object[0] I just get the first chracter {

Baisically what I want to do, is add this to a text like this 
["yemen", "america", "france", "germany"];
$string = "yemen&america&france&germany"; in php

I'm sorry if this was asked too many times, I checked for the past two hours stackoverflow it solved one problem, it's just because I'm not advanced in php or I'm dumb, I can easily solve this however in js. I'm still searching if I find an answer, I'll update it.

Update I found a solution, which I used before for hours it didn't work now it works, I didn't change anything on the frontend. I just did

I changed only the naming variables.

if (isset($_POST["news"]) )
{
  
$searcharrayx =  $_POST["searcharray"];

$b = $searcharrayx;
//echo $b;
 $a =  json_decode($b, true); //this one now works for some reason
 $ccount = count($a);
 $e = "";
 for($d = 0; $d<=$ccount+-1; $d++) {
     if($d == $ccount +-1){
         $e = $e.$a[$d];
     } else {
       $e=$e.$a[$d]."%20AND%20";
     }
 }
  • Have you tried setting the second argument in **json_decode** to true instead of false? – imvain2 Aug 24 '22 at 16:30
  • 1
    If PHP is receiving JSON, why are you calling `json_encode()` on it before calling `json_decode()`? Have you tried removing the call to `json_encode()`? – kmoser Aug 24 '22 at 19:26
  • Agreed, it makes no sense to encode something as JSON when it's already JSON! Just decode it only – ADyson Aug 24 '22 at 19:52
  • How are you sending the JSON payload? – Progrock Aug 24 '22 at 22:24
  • @Progrock you can see that from the JS code snippet – ADyson Aug 24 '22 at 22:49
  • I solved it now, I tried json_decode($var, true) before for long time, then I made a variable string to learn how to read it and acess it and convert it to array, then I was trying the same thing to _POST then it worked, magic – Basaam Qasem Aug 26 '22 at 09:11

1 Answers1

0

Here is some simplified code, take your stringified json, turn it into a php object, and then join the array and dump it to a text file.

<?php
if(isset($_POST['news'])){
    $submission = $_POST['searcharray'];
    $json       = json_encode($submission);
    if($json !== false) {
        $json_ob = json_decode($submission);
        file_put_contents('data.txt', implode('&', $json_ob->searcharray));
    }
}

?>
<html>
    <head>
        <script>
            searcharray = ["yemen", "america", "franc", "germany"];
            const json = JSON.stringify({searcharray});
            const formData = new FormData(); 
            formData.append('searcharray', json); 
            formData.append('news', 'true');
            var request = new XMLHttpRequest();
            request.open("POST", "");
            request.send(formData);
        </script>
    </head>
    <body>
    <body>    
</html>

Output of the data.txt after submission:

yemen&america&franc&germany

The POST submission looks like this:

{"searcharray":["yemen","america","franc","germany"]}

The first value could be accessed thus:

$json_ob->searcharray[0]

You could de-key the JSON object, just turn it into a list:

JSON.stringify(searcharray);
Progrock
  • 7,373
  • 1
  • 19
  • 25
  • I found a solution, I'll show you the code, I don't know what difference I made I tried this for hours it didn't work now it works somehow. if (isset($_POST["news"]) ) { $searcharrayx = $_POST["searcharray"]; $b = $searcharrayx; //echo $b; $a = json_decode($b, true); $ccount = count($a); $e = ""; for($d = 0; $d<=$ccount+-1; $d++) { if($d == $ccount +-1){ $e = $e.$a[$d]; } else { $e=$e.$a[$d]."%20AND%20"; } } – Basaam Qasem Aug 26 '22 at 09:05