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.

2 Answers2

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);
Robert
  • 5,703
  • 2
  • 31
  • 32
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
)
Nathan
  • 491
  • 4
  • 8