Hoan Thanh
Hoan Thanh

Reputation: 21

Convert string to array php

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.

Upvotes: 1

Views: 115

Answers (5)

Shivrag
Shivrag

Reputation: 11

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);

Upvotes: 0

Liu.handy
Liu.handy

Reputation: 59

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
        )

)

Upvotes: 1

MVG1984
MVG1984

Reputation: 645

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.

Upvotes: 0

Gangani Roshan
Gangani Roshan

Reputation: 583

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.

Upvotes: 1

Arun pandian M
Arun pandian M

Reputation: 882

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"
}]

Upvotes: 0

Related Questions