0

i'm trying to do a thing but i don't know how :) I have a php string in this format:

$test = (1 / 2015-11-2 11:11:11, 2 / 2015-07-07 11:11:11, ......)

I need a function that process that string to get as format:

$testarray = array(
    "id" => "1",
    "date" => "2015-11-2 11:11:11",

    "id" => "2",
    "date" => "2015-07-07 11:11:11",

   .....
    );

I thought these steps:

1) find how many comma are present in $test string using

substr_count($test, ",");

2) take the string before the comma and find in this string value " / "

3) Create the array

Can you help me?

3
  • explode with ',' first then loop that array and explode each element with '/' Commented Jun 13, 2016 at 9:19
  • i did but i obtain three separate array if in $test string i have for ex 3 item..i need an unique array with 3 items Commented Jun 13, 2016 at 9:25
  • You can't do this, one key in one array only. tow id or two date can't be in one array. Commented Jun 13, 2016 at 9:30

3 Answers 3

3

The function you need to use is explode. You can check the documentation here

First, like you said, you need to separate the string into "substrings" using the comma separator:

$firstArr = explode(',', $test);

This will return an array of strings like this:

$firstArr = [ "1 / 2015-11-2 11:11:11", "2 / 2015-07-07 11:11:11",... ]

After that you can use explode one more time on each string that the array above contains:

$result = array();   //The resulting array
foreach($firstArr as $str)
{
    $secondArr = explode('/', $str); // specific delimiter '/'
    $result[] = array(
        'id' => trim($secondArr[0]),//trim removes blank spaces if any
        'date' => trim($secondArr[1])
    );
}

$secondArr will be like this:

$secondArr = ['1', '2015-11-2 11:11:11']

That is why you need to use $secondArr[0] to get the id and $secondArr[1] to get the date

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

Comments

0

Explode is the best way, but as variant you can use regex

(?<=\(|,)\s*(?P<id>\d+)\s+\/\s*(?P<date>[^,\)]+)(?=,|\))

which returns such result

MATCH 1
id  [1-2]   `1`
date    [5-23]  `2015-11-2 11:11:11`
MATCH 2
id  [25-26] `2`
date    [29-48] `2015-07-07 11:11:11`

demo and some explanation

1 Comment

Wow! i0ve never seen regex! how can i implement in my code?
0

The solution using preg_match_all and array_walk functions:

$test = "1 / 2015-11-2 11:11:11, 2 / 2015-07-07 11:11:11";
$result = [];
preg_match_all("/(\d+?) \/ (\d{4}-\d{2}-\d{1,2} \d{2}:\d{2}:\d{2})/", $test, $m, PREG_SET_ORDER);
array_walk($m, function($v, $k) use(&$result){
    $result[] = ['id'=> $v[1], 'date' => $v[2]];
});

print_r($result);

The output:

Array
(
    [0] => Array
        (
            [id] => 1
            [date] => 2015-11-2 11:11:11
        )

    [1] => Array
        (
            [id] => 2
            [date] => 2015-07-07 11:11:11
        )
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.