3

Can anyone please help me for converting the string into an Array or JSON as well? Please take look on the text sample below;

{
    "account_id": "dfdfdf",
    "email": "[email protected]",
    "id": "dfdfdf",
    "name": "Gmail Team",
    "object": "contact",
    "phone_numbers": []
},
{
    "account_id": "dfdf",
    "email": "[email protected]",
    "id": "dfdf",
    "name": "Ab",
    "object": "contact",
    "phone_numbers": []
},
{
    "account_id": "dfdf",
    "email": "[email protected]",
    "id": "dfdfdf",
    "name": "xyz",
    "object": "contact",
    "phone_numbers": []
},

I have tried

preg_match_all("/\{([^\)]*)\},/", $stream[0], $aMatches);

But it doesn't return anything. I also have tried json_decode, json_encode but could not find any success on it.

Thanks

4
  • 1
    Take a look at json_decode() Commented Mar 8, 2016 at 15:12
  • @fusion3k Already tried. This is string and it results in null Commented Mar 8, 2016 at 15:47
  • 1
    Please provide a link to real string. With your example, below provided answer is fine. Commented Mar 8, 2016 at 15:49
  • thanks for the support @fusion3k . I agree, the string given is not the same as what he is seeing if the problem persists. Commented Mar 8, 2016 at 15:50

1 Answer 1

12

The goal is to turn it into appropriate JSON format so that you can use json_decode. Ill break it down in steps:

  1. remove all \n characters:

    $string = str_replace('\n', '', $string);
    
  2. remove last comma

    $string = rtrim($string, ',');
    
  3. add brackets

    $string = "[" . trim($string) . "]";
    
  4. turn it into PHP array:

    $json = json_decode($string, true);
    

Result:

$string = ''; //your string
$string = str_replace('\n', '', $string);
$string = rtrim($string, ',');
$string = "[" . trim($string) . "]";
$json = json_decode($string, true);
var_dump($json);

Output:

array (size=3)
  0 => 
    array (size=6)
      'account_id' => string '43z95ujithllc32fn02u8ynef' (length=25)
      'email' => string '[email protected]' (length=23)
      'id' => string '955xl0q3h9qe0sc11so8cojo2' (length=25)
      'name' => string 'Gmail Team' (length=10)
      'object' => string 'contact' (length=7)
      'phone_numbers' => 
        array (size=0)
          empty
  1 => 
    array (size=6)
      'account_id' => string '43z95ujithllc32fn02u8ynef' (length=25)
      'email' => string '[email protected]' (length=21)
      'id' => string '3u4e6i9ka3e7ad4km90nip73u' (length=25)
      'name' => string 'Test Account 1' (length=14)
      'object' => string 'contact' (length=7)
      'phone_numbers' => 
        array (size=0)
          empty
  2 => 
    array (size=6)
      'account_id' => string '43z95ujithllc32fn02u8ynef' (length=25)
      'email' => string '[email protected]' (length=20)
      'id' => string 'bt3lphmp0g14y82zelpcf0w0r' (length=25)
      'name' => string 'Test Account' (length=12)
      'object' => string 'contact' (length=7)
      'phone_numbers' => 
        array (size=0)
          empty
Sign up to request clarification or add additional context in comments.

5 Comments

then you are not providing all the string in your original post.
how is your string formed? Where are you getting the string from?
It is string response from a third party API Nylas. I am posting exactly what I am having. I want array where data between { and }, can be get as internal array
I see that you updated your string but that is very different than your original string. Before you had \n characters... what happened to them?
in that case, do the same as my answer, but dont follow step 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.