Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

56
Views
Using JS array values as PHP array values

I cannot find a solution to my question, I read the hints of server-side and client-side issues already.

  1. I have a js script that loads the values of a js array which is stored in localStorage, here is the code:

     <script>
     var myList = JSON.parse(localStorage.getItem('myList', '[]'));
     console.log(myList);
     </script>
    
  2. I get the content of the myList array values stored locally. The values of this array are separated by a comma (,) like this:

     myList ['apple1', 'apple2', 'apple3']
    
  3. I try to use this js array in php.

     <?php
     $test0 = "<script>document.writeln(myList);</script>";
    
  4. I echo the $test0, it works, I get the apple1,apple2,apple3 values:

     echo $test0;
     //apple1,apple2,apple3
    
  5. I change $test0 to a string (anyway it should be a string originally) and then I check whether it´s really a string, it works, it´s a string:

     $test1 = strval($test0);
     echo is.string($test1);
     // 1
    
  6. I explode the string $test1 into the $test2 array and then check whether $test2 is an array, it works, $test2 is an array;

     $test2 = explode(',',$test1,0);
     echo is_array($test2);
     // 1
    
  7. I echo the $test2 array values, it works, I get the values:

     foreach($test2 as $value){
     echo $value;
     }
     ?>  
     //apple1,apple2,apple3
    
  8. But, if I try to use the substr() function to this $value in order to return specific characters of each value of the $value array, I get the substracted string of the $test0 variable (ript>document.writeln(myList);):

     foreach($test2 as $value){
     echo substr($value,3);
     }
     // ript>document.writeln(myList);
    

Why does not work the substr() function in this case?

Thank you for your help!

7 months ago · Juan Pablo Isaza
Answer question
Find remote jobs