1

I have a string like this:

{ArrivalTime:300, ProductID:198, ArrivalDate:21/07/2017}, {ArrivalTime:582, ProductID:397, ArrivalDate:22/07/2017}

I used json_decode() but it's not working;

I need return array like this:

Array
(
    [0] => Array
        (
            [ArrivalTime] => 300
            [ProductID] => 198
            [ArrivalDate] => 21/07/2017
        )

    [1] => Array
        (
            [ArrivalTime] => 582
            [ProductID] => 397
            [ArrivalDate] => 21/07/2017
        )

)

Can you help me to do that? thanks.

4
  • 5
    Because this string is not a valid json. Commented Jul 21, 2017 at 6:42
  • It's not the correct JSON format. That's why json_decode() function is not working. Commented Jul 21, 2017 at 6:42
  • json_decode() doesn't work because your data is not valid json. Maybe you can solve the issue at the source of your data. Commented Jul 21, 2017 at 6:44
  • Possible duplicate of PHP reading invalid json with json_decode(); Commented Jul 21, 2017 at 7:45

5 Answers 5

1

You can try

str_split — Convert a string to an array

Syntax:

array str_split ( string $string [, int $split_length = 1 ] )

Example:

<?php

$str = "Hello Friend";

$arr1 = str_split($str);

print_r($arr1);

?>

Edit:

Output:

Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>
    [6] => F
    [7] => r
    [8] => i
    [9] => e
    [10] => n
    [11] => d
)

Replace string name and array name your own. Thank You.

Sign up to request clarification or add additional context in comments.

3 Comments

did you test your code? It will not work as he wants. He have to check why his json string not formatted correctly.
Yes dude You can try it in simple way. M answer only how to convert string to array.
Actually, he is not asking this! @GanganiRoshan
1

Run follow code to analyse it:

function strAnalyse($str){
        preg_match_all('/(?<={)[^{}]*(?=})/',$str,$match);
        $result = array();
        foreach($match[0] as $item){
            $one =array();
            $tmp = explode(',',$item);
            foreach($tmp as $kv){
                $a = explode(':',$kv);
                $one[trim($a[0])] = trim($a[1]);
            }
            array_push($result,$one);
        }
        echo '<pre>';
        print_r($result);
}

result:

Array
(
    [0] => Array
        (
            [ArrivalTime] => 300
            [ProductID] => 198
            [ArrivalDate] => 21/07/2017
        )

    [1] => Array
        (
            [ArrivalTime] => 582
            [ProductID] => 397
            [ArrivalDate] => 22/07/2017
        )

)

2 Comments

1 for the Answer and we don't know that json is correct anyway your code is fine
That json is not valid,because it lack of key's and value's quote.
0

the json is invalid even if we add "[]" (array brackets)

  • value should be in the any one of the format :

    string, number,object,array,true,false,null

in your case there is a date in the value field change it to string by enclosing ""(double quotes)

  • the key in your json is a string for json key the string should be enclosed by ""(double quotes)

" chars "

offical documentation : http://www.json.org/

the correct json format for your json is

   [{
    "ArrivalTime": 300,
    "ProductID": 198,
    "ArrivalDate": "21 / 07 / 2017"
}, {
    "ArrivalTime": 582,
    "ProductID": 397,
    "ArrivalDate": "22 / 07 / 2017"
}]

Comments

0

You need to wrap your "json" string with [] and quotes, of course (or check, why it not wrapped earlier):

[{"ArrivalTime": 300,"ProductID": 198,"ArrivalDate": "21 / 07 / 2017"}, {"ArrivalTime": 582,"ProductID": 397,"ArrivalDate": "22 / 07 / 2017"}]

Then it will be correct json format and json_decode() create correct array.

3 Comments

Is this valid json?
If with [ and ] - yes. Then it will be array.
Your right, in my example a forgot quotes, and i've checked string with them :)
0

That is not a valid json format. But if you have such kinda string try this code:

$string = "{ArrivalTime:300, ProductID:198, ArrivalDate:21/07/2017}, {ArrivalTime:582, ProductID:397, ArrivalDate:22/07/2017}";


$key = $value = $resultArray= array();

$arrayJson = explode('}, ', $string);



foreach ($arrayJson as $arrayJsonRep) {

    $repJson = str_replace('{', '', $arrayJsonRep);
    $repJson = str_replace('}', '', $repJson);
    $repJsonExp = explode(', ', $repJson);

    foreach ($repJsonExp as $x) {

        $repJsonExpfirst=  explode(':', $x); 
        array_push($key, $repJsonExpfirst[0]);
        array_push($value, $repJsonExpfirst[1]);
    }

    $finalArray = array_combine($key, $value);
    array_push($resultArray, $finalArray);
}



print_r($resultArray);

Comments

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.