how can i convert the following string into an array?

$str = "Array ( [0] => asd [1] => frd [2] => cfc [3] => ask )";

i tried eval() but couldnt do it. The string could be any string representation of an array such as print_r, var_dump or var_export - thanksπŸ˜•

    I dont understand.

    If you have an array $arr you can get that print out by using print_r($arr)
    But what is the use of converting the printed output back to an array you already have?
    The result wil be the same as $arr ....

    But if you mean another way to check your array contents
    this is one way

    <?php
    
    $myarray = array( 'asd', 'frd', 'cfc', 'ask' );
    
    foreach( $myarray AS $key => $value ){
    
    echo $key. ' is ' .$value. '<br />';
    
    }
    
    ?>

      the $arr is out of scope and only its output is available in string format. i need to create the array from this string representation. any ideas?

        liveaday wrote:

        the $arr is out of scope and only its output is available in string format. i need to create the array from this string representation. any ideas?

        Those 3 different funtions you mention: print_r, var_dump, var_export
        may have different syntax.
        But if we take the string you show above,
        it has certain pattern to look for in that string.

        Each key-value has this pattern, between the colons:
        :[3] => ask :

        Using [man]preg_match_all[/man]
        it is very easy to get the key (3) and value (ask)
        and then we use this for the 'matches'
        $key = 3;
        $value = 'ask';

        $newarray[$key] = $value;

        The preg_match_all will return 4 matches of that pattern.
        And this will make the new array have 4 key-value pairs
        exactly same as the original.

          i m not sure how that would work since i never used regular expressions. I was hoping that since this the string is a valid array definition there would be a function create the array or a function to reverse the output of var_export for example

            I am not very good with regex myself.
            This is why I left it to someebody else to show what to use πŸ˜‰

            But you know there is a way in php to convert array to string
            and back again string to array.
            This is same way as is used when $SESSION array is saved in the session file.
            This is how values of $
            SESSION can be remembered from script to script for a user.

            The stored session array is written to a file, in format of a string.
            The string is read from the file and converted back to an array called $_SESSION.

            The 2 functions I am talking about are:
            [man]serialize[/man]
            [man]unserialize[/man]

            <?php
            
            $myarray = array(
            'name' => 'henry',
            'age' => 22,
            'location' => 'england'
            );
            
            $string = serialize( $myarray );
            echo $string;
            echo '<br>';
            
            $newarray = unserialize( $string );
            print_r( $newarray );
            
            ?>

            The variable to serialize need not be an array.
            It can be any variable, like
            $x = 1234; $x = false; $x = 'Hello world';

            Regards

              the problem is that i need to do this from 2 different servers. a script on server A will set the array and then i need to read it in another script on server B. Basically i wish to have one server do all the processing and setting of variables (caching)and then have other servers just read/access those variables and display differently. My issue is that i cant communicate data from server to another.

              1. set arrays into cache files

              2. get file contents based on file creation time

              if i do #1 in server A and #2 in server B then i cant get the file creation time in server B and thus need to do #2 in server A also. But then how do i get the variables back to B?

              below is what I have and works fine when working in one server. BUT how do i pass the $results_ to a different server??

              if ( file_exists($file) && time() < filemtime($file) + $interval ) {
              $results = unserialize(file_get_contents($file));
              } else {
              ...processing
              $results
              = array('aa','ss','ddd');
              $fp = fopen($file, 'w+') or die("Could not open $file.");
              fwrite($fp, serialize($results_));
              fclose($fp);

              }

                Um, the code you've got writes a file with the serialised value of $results_. Send that file to the other server and have the other server unserialise it. You already have code to do that right there.

                  this is how I think it can be done
                  if following Weedpacket hint and I got this right:

                  A. sender server
                  1. check local file, if needs to be updated
                  2a. if so, create a new serialized version of file and save
                  2b. send a copy of this file to server B, using
                  $fp = fopen( URL_TO_B+dir+file, 'w+');fwrite;fclose

                  B. receiver server
                  1. read and unserilize file
                  2. use values

                    i guess that would work but it means duplicating the file to another server. and in the case i wanted 10 servers to access that data it would mean saving the file 10 times...

                      10 servers

                      the question is what should trigger setting of variables and caching
                      and what should trigger the transfer
                      one of the calling servers or the main server

                      php normally need a user/visitor to request a php page

                        Write a Reply...