TaG
TaG

Reputation: 13

PHP String to array - Stuck with my string

This is my first question, i have been able to solve many issues by using your forum, but I am coming for your help because i do not know how to solve my issue. I hope i will be understandable.

I want to convert a string to an array of array, but all my readings dit not help me to find a solution.

I have a string in this format

$string = "records[0].CardName=TEST records[0].CardNo=01234567 records[0].CreateTime=1566835406 records[0].Door=0 records[0].Method=1 records[0].Password= records[0].RecNo=1366 records[0].Status=1 records[0].URL= records[0].UserID=9901 records[1].CardName=TEST records[1].CardNo=01234567 records[1].CreateTime=1566851904 records[1].Door=0 records[1].Method=1 records[1].Password= records[1].RecNo=1368 records[1].Status=1 records[1].URL= records[1].UserID=9901";

So you can see that this is always the same structure and only values are modified, there is more than 2 repetition of this element, but to keep it readable i just put two of them. I want to have an array created when the records[X] changes and then put each element matching records[X] into this array.

I want to convert it as an array which would look like this.

Array
(
    [0] => Array 
        (
            [CardName] =>TEST
            [CardNo] => 01234567
            [CreateTime] => 1566835406
            [Door] => 0
            [Method] => 1
            [Password] => 
            [RecNo] => 1366
            [Status] => 1
        )
    [1] => Array
        (
            [CardName] => TEST
            [CardNo] => 01234567
            [CreateTime] => 1566835406
            [Door] => 0
            [Method] => 1
            [Password] =>
            [RecNo] => 1366
            [Status] => 1
        )
)

The goal is to access the last element of the array and to be precise the value CreateTime like this : end($array)['CreateTime'].

What I have try does not help me and i am stuck with no idea on how to deal with that issue.

$array = preg_split('/\s+/',$string);
if(is_array($array) {
    print_r($array);
}
// Gives me this : 
Array
(
    [0] => records[0].CardName=TEST
    [1] => records[0].CardNo=01234567
    [2] => records[0].CreateTime=1566835406
    [3] => records[0].Door=0
    [4] => records[0].Method=1
    [5] => records[0].Password=
    [6] => records[0].RecNo=1366
    [7] => records[0].Status=1
    [8] => records[1].CardName=TEST
    [9] => records[1].CardNo=01234567
    [10] => records[1].CreateTime=1566835508
    [11] => records[2].Door=0
    [12] => records[3].Method=1
    [13] => records[4].Password=
    [14] => records[5].RecNo=1366
    [15] => records[6].Status=1
)

I have also tried something like that with multiple different delimiter with no success


function multiexplode ($delimiters,$string) {
        $ary = explode($delimiters[0],$string);
        array_shift($delimiters);
        if($delimiters != NULL) {
            foreach($ary as $key => $val) {
                 $ary[$key] = multiexplode($delimiters, $val);
            }
        }
        return  $ary;
    }

    // Example of use
    $string = "records[0].CardName=APKO records[0].CardNo=88043527 records[0].CreateTime=1566835406 records[0].Door=0 records[0].Method=1 records[0].Password= records[0].RecNo=1366 records[0].Status=1 records[0].URL= records[0].UserID=9901 records[1].CardName=APKO records[1].CardNo=88043527 records[1].CreateTime=1566851904 records[1].Door=0 records[1].Method=1 records[1].Password= records[1].RecNo=1368 records[1].Status=1 records[1].URL= records[1].UserID=9901";
    $delimiters = Array('/\s+/',".",'=');

    $res = multiexplode($delimiters,$string);
    print_r($res);

Thanks for your help. I hope there is something that can be done to achieve this.

TaG

Upvotes: 1

Views: 38

Answers (2)

AbraCadaver
AbraCadaver

Reputation: 78994

If there are no spaces other than the delimiter, you can replace a few things and get a valid query string. The replacements will give you a query string similar to this:

records[0]["CardName"]=TEST&records[0]["CardNo"]=01234567

That can be parsed into an array:

parse_str(str_replace([' ','.','='], ['&','["','"]='], $string), $result);

If there are other spaces then even some fancy regex will fail, as there is no way to tell what is a delimiter and what is not.

Upvotes: 0

Nigel Ren
Nigel Ren

Reputation: 57121

This goes along the lines of splitting the text at multiple parts with explode(). Starting using records[ as the delimiter so that this should mean even values with spaces are maintained. Then breaking the key down into the individual components...

$parts = explode("records[", $string);
// Remove empty item of start
array_shift($parts);
$output = [];
foreach ( $parts as $part ) {
    list($key, $value) = explode("=", $part, 2);
    list($id, $field) = explode("].", $key);
    $output[$id][$field] = rtrim($value);
}
print_r($output);

gives...

Array
(
    [0] => Array
        (
            [CardName] => TEST 
            [CardNo] => 01234567 
            [CreateTime] => 1566835406 
            [Door] => 0 
            [Method] => 1 
            [Password] =>  
            [RecNo] => 1366 
            [Status] => 1 
            [URL] =>  
            [UserID] => 9901 
        )

    [1] => Array
        (
            [CardName] => TEST 
            [CardNo] => 01234567 
            [CreateTime] => 1566851904 
            [Door] => 0 
            [Method] => 1 
            [Password] =>  
            [RecNo] => 1368 
            [Status] => 1 
            [URL] =>  
            [UserID] => 9901
        )

)

Upvotes: 1

Related Questions