SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 27
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Convert array string into an array

    Hi - how do I convert an array string into an array (i.e. the following values into an array). It may already look like an array but PHP is seeing it as a string (just need to convert it please).

    PHP Code:
    Array
    (
        [
    plaintext] => This is an example CV.
        [
    filename] => 1345550846.doc


  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    1. Find the contents of the array(s). (Hint: preg_match_all)
    2. Foreach of those contents, explode on line breaks.
    3. Foreach line, explode on =>.
    4. Trim the first component. Left trim the second, right trim the last.
    5. the key is defined as the first element of the components, from index 1 to -1 (hint: substr). The value is defined as the remaining elements imploded on => (in case there is a => in the value).
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there no easier way to convert it straight to an array?

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    What exactly is the output you want to achieve, as you already have an array...

    Can you pass the variable to var_dump() so we can see how PHP is identifying the data

  5. #5
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    What exactly is the output you want to achieve, as you already have an array...

    Can you pass the variable to var_dump() so we can see how PHP is identifying the data
    PHP Code:
    array(2) {
      [
    "plaintext"]=>
      
    string(23"This is an example CV.
    "
      
    ["filename"]=>
      
    string(14"1345555572.doc"


  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Okay, is that the var_dump of the expected output or of the data you have that you want to convert? As if that is the data you want to convert, you already have a PHP array, if it is the expected output, can you provide the var_dump of the data you want to convert as well?

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    From what i read, cp, the input is a multi-line sting, in the format shown in the OP.

    Take that STRING. (not an array object, an actual string. As if you'd screen-scraped the output of a print_r. and convert it into it's array equivilant.

    If i've missed something, let me know.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Okay, I'll try playing around with a few regular expressions when I get home, that could hopefully simplify this process. As if you can write a regex that matches the key and the value of said key, you can then loop through the matches to create your associative array.

  9. #9
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    hmm, true... something like...

    Array {.*?(\[(.*?)\] => (.*?)\n)*} ?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  10. #10
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    hmm, true... something like...

    Array {.*?(\[(.*?)\] => (.*?)\n)*} ?
    I came up with something similar
    Code:
    /\[([a-zA-Z0-9]+)\] => (.*?)(?:\r\n|\r|\n)/
    When used in preg_match_all, the output should be
    Code:
    array (
      0 => 
      array (
        0 => '[plaintext] => This is an example CV. 
    ',
        1 => '[filename] => 1345550846.doc 
    ',
      ),
      1 => 
      array (
        0 => 'plaintext',
        1 => 'filename',
      ),
      2 => 
      array (
        0 => 'This is an example CV. ',
        1 => '1345550846.doc ',
      ),
    )

  11. #11
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    I came up with something similar
    Code:
    /\[([a-zA-Z0-9]+)\] => (.*?)(?:\r\n|\r|\n)/
    I tried this code but it still doesn't seem to be working (returns blank array).

    Here is an example of the code i'm using:

    PHP Code:
    $array_string 'array(2) {
      ["plaintext"]=>
      string(23) "This is an example CV.
    "
      ["filename"]=>
      string(14) "1345727271.doc"
    }'
    ;

    preg_match_all("/\[([a-zA-Z0-9]+)\] => (.*?)(?:\r\n|\r|\n)/"$array_string$matches);

    print_r($matches); 

  12. #12
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Your updated code has quotes around the array keys, I didn't see that in the original text you posted. In fact, that is entirely different than the original text you posted, so which text do you have to convert?

  13. #13
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The text is the same as post #5 http://www.sitepoint.com/forums/show...=1#post5172303

    Only the .doc name has changed slightly.

  14. #14
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Just where are you getting this "array string"...and how do you know it is not actually an array? And just maybe, you can help yourself instead of being spoon fed like...[omitted]. Most of the code to convert the "array string" with regex is provided. Now help yourself for a change and make the necessary edits to solve the issue on Post #11.

    Yes I know I could be nicer...
    But those that don't help themselves just aggravate me to no end.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  15. #15
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm getting it from another server, I'm returning it as a string (using print_r) as it's not possible to return as an array to the browser.

  16. #16
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    why not use json_encode and then json_decode on the receiving server?

  17. #17
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes that's probably a better solution actually.

    So on the other server i use echo json_encode($array);

    Then how would I convert back to an array? Just json_decode($string)?

  18. #18
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The output of that is:

    stdClass Object ( [plaintext] => This is an example CV. [filename] => 1345730220.doc )

  19. #19
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Zaggs View Post
    Yes that's probably a better solution actually.

    So on the other server i use echo json_encode($array);

    Then how would I convert back to an array? Just json_decode($string)?
    right and you would feed that straight to json_decode() which will give you your array back.

  20. #20
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but it's outputting the following:

    PHP Code:
    stdClass Object
    (
        [
    plaintext] => This is an example CV.
        [
    filename] => 1345730220.doc


  21. #21
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Help me understand what you mean it is outputting the following?

    PHP Code:
    <?php
    $myArray 
    = array('plaintext' => 'This is an example CV.''filename' => '1345730220.doc');
    $jsonString json_encode($myArray);
    $decodedArray json_decode($jsonStringtrue); // Note: I pass true for the $assoc optional parameter

    var_dump($myArray$decodedArray); // Should be nearly identical
    echo $decodedArray['plaintext'];
    ?>
    Last edited by cpradio; Aug 23, 2012 at 07:24. Reason: Updated to set the assoc option to true

  22. #22
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Your output to json_encode should look like:
    Code:
    {"plaintext":"This is an example CV.","filename":"1345730220.doc"}

  23. #23
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Your output to json_encode should look like:
    Code:
    {"plaintext":"This is an example CV.","filename":"1345730220.doc"}
    Maybe it's because I'm fetching the output from the remote server using CURL? I think it's seeing it as a string?

  24. #24
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    It is a string, are you setting the optional parameter in json_decode for $assoc to true?
    PHP Code:
    $decodedArray json_decode($jsonStringtrue); // Note: I pass true for the $assoc optional parameter 

  25. #25
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, should I be?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •