array_merge() vs. CMap::mergeArray(), json_encode() vs. CJSON::encode()

What are the benefits of using Yii’s version instead of PHP’s built-in version?

I believe the purpose of the static function CMap::mergeArray() is to enable you to merge CMap objects rather than PHP arrays. Try using PHP’s built-in version with CMap objects and you should get an error. However, if you use CMap::mergeArray() with CMap objects it should return a CMap object.

CMap::mergeArray() works different than PHP’s version. Just try to merge config files with one or another.

CJSON::encode() allows to encode Active Records and can work when there is no json_encode() available.

CMap::mergeArray() can also merge multi dimensional arrays in a useful way. Like samdark already mentioned that’s perfect to merge complex configuration arrays.

array_merge_recursive() is similar. I think I can see the significant difference. mergeArray() overrides a key of the same name if the corresponding values for the identical keys are not both arrays, otherwise it merges the arrays recursively. array_merge_recursive() either turns the values of identical keys into an array with those values or, if the values are arrays, it recursively merges those arrays - either way identical keys will always merge into an array, which would not be good for merging config arrays.

Thanks Yoda, good roundup.

Mike, I am hoping you might shed some light on this problem.

I need to call an API that returns JSON.

It limits to 100 records each time.

$params = "start_rec = 0, num_rec = 100";

So I call GET

$jsonObject = GET($url, $params);

if the response->count >100 I need to

$start_rec=100

call again

Not sure how to handle the json and merging the data.

Any suggestions?

Thanks very much !

HI All,

CMap::mergeArray() works like array_replace_recursive() in php

not like a array_merge_recursive();

thank you.

Thanks a lot! I know it exactly now!

Thanks For The Info. :)