0

I am having a few issues with an array being brought from a database.
The original array once converted looks like this:

Array
(
    [0] => {"lastTimeSave":"1494000000"
    [1] => "rankexpire":"0"
    [2] => "evocity_rank":"g-vip"
    [3] => "evocity_rankexpire":"0"}
)

I successfully removed some unuseful characters so my final array looks like this:

Array
(
    [0] => lastTimeSave:1494000000
    [1] => rankexpire:0
    [2] => evocity_rank:g-vip
    [3] => evocity_rankexpire:0
)

What I am wanting to do is get everything before the ':' and place it into the key of the array, then remove the ':' so it looks something like this:

Array
(
    ['lastTimeSave'] => 1494000000
    ['rankexpire'] => 0
    ['evocity_rank'] => g-vip
    ['evocity_rankexpire'] => 0
)

I am separating using:

$staffarray = str_replace('"', "", $staffarray);
$staffarray = str_replace('{', "", $staffarray);
$staffarray = str_replace('}', "", $staffarray);

I have already tried multiple things, including:

foreach ($stafftestarray as $key => $value) {
    $substring = substr($value, 0, strpos($value, ';'));
    $subsubstring = str_replace($substring, "", $value);
    $value = $subsubstring;
}

However nothing seems to change it and the output is not being changed, I would really appreciate any help I can get with this problem as I have searched for countless hours how to fix it to no avail.

4
  • Use explode function in php Commented May 7, 2017 at 19:37
  • 4
    It looks like you're trying to manually parse a JSON string. Why not just use json_decode()? Commented May 7, 2017 at 19:38
  • @AlexHowanskyI completely forgot about JSON, thats a lot easier thanks a lot! Commented May 7, 2017 at 19:52
  • @JakeButterfield Just take a look here: stackoverflow.com/q/29308898/3933332 Commented May 7, 2017 at 20:05

2 Answers 2

1

Looks like an exploded json object stored as on array.

Though I'm not sure why your data looks like that (did you explode the string by , ?)

This will do what you want:

$data = json_decode(implode(',', $yourArray), true);
Sign up to request clarification or add additional context in comments.

Comments

0

Don't remove the " 's and use explode to create a new array based on your old array.

$array = array
        (
            0 => "lastTimeSave:1494000000",
            1 => "rankexpire:0",
            2 => "evocity_rank:g-vip",
            3 => "evocity_rankexpire:0"
        );

$new_array = array();

foreach($array as $value) 
{
    $item = explode(":", $value);
    $new_array[$item[0]] = $item[1];
}

print_r($new_array);

Where print_r($new_array) will give:

Array
(
    [lastTimeSave] => 1494000000
    [rankexpire] => 0
    [evocity_rank] => g-vip
    [evocity_rankexpire] => 0
)

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.