1

I have JSON response that looks like this

(
  [0] => stdClass Object

     (
       [default] => false
       [loc] => http://somethingirrelevant.lol
       [temp] => '100'
     )

)

What i am looking to accomplish change the url in [LOC] to https

I tried using :

$array = preg_replace('http','https' $array);

but that completly breaks array!

0

2 Answers 2

1

You have an array of an object. Array key 0 is an object with a loc property, and you can use str_replace() here:

  $array[0]->loc = str_replace('http://', 'https://', $array[0]->loc);
//$array[0]->loc = preg_replace('#http://#', 'https://', $array[0]->loc);

If you decode as an array:

  $array = json_decode($json, true);

Then:

  $array[0]['loc'] = str_replace('http://', 'https://', $array[0]['loc']);
Sign up to request clarification or add additional context in comments.

3 Comments

Not just that, regex must be enclosed between delimiters ;)
I would add you should use wordboundary to avoid replacing https with httpss
@Toto Thanks! Last time I post when I'm trying to leave for lunch.
0

You can't simply call the Array, what you can do is iterate throught it and replace every value with the key loc:

foreach($array AS $key=>$value) {
    if(isset($value['LOC'])) {
         $array[$key]['LOC'] = preg_replace('http','https', $array);
    }
}

You can cast your array into a regular array instead of an object :

if(!is_array($array)) $array = (array)array;

1 Comment

actually, the syntax error was in the original code i copied, But i should have noticed it. Thanks.

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.