0
$result = AssetModel::biGetRecords($userId);

The result is array of objects.

like this

array(100) {
[0]=>
object(stdClass)#1120 (3) {
["id"]=>
int(1058064)
["asset_id"]=>
string(16) "12345"
["name"]=>
string(22) "David"
}
[1]=>
object(stdClass)#1116 (3) {
["id"]=>
int(1058088)
["asset_id"]=>
string(16) "34567"
["name"]=>
string(6) "Smith"

so I use

$result = json_decode(json_encode($result), true); 

to transfer array of std objects to array of arrays.

It works fine. But then when new records added in. Suddenly

 $result = json_decode(json_encode($result), true); 

instead of return array of array, it returns empty array now.

My guess is that some new records with some invalid characters that make json_encode returns invalid json string so the next step json_decode would not work?

echo "get results: ";

echo count($result);

$result = json_decode(json_encode($result), true);

echo " count data results again: ";

echo count($result);

the result is

get results: 397320 count data results again: 0

So my questions are

  • $result = json_decode(json_encode($result), true) is not error proof way to transfer array of objects to array of array?
  • if above case is true, what is the easiest way to transfer array of objects to array of arrays?

Thanks!

6
  • I think we need to know more about what AssetModel::biGetRecords() returns and if it has private/protected properties. Can you mockup an example of what one of the object records looks like in your question? eg var_dump(current($result)); Commented Mar 8, 2018 at 1:07
  • 1
    Why do you want to change the objects into arrays? Commented Mar 8, 2018 at 1:11
  • The data will be sent to next function for processing and that function expecting array of arrays. I have been using this approach for a long time without problems. Commented Mar 8, 2018 at 1:14
  • Scuzzy, I gave examples above. It has been working fine. And if I limit my database query to return 1000, 2000 records etc. it is still fine. But if I return the full dataset, then it breaks. So my guess is not the object format issue. it is the data (characters) in some of the newly added records that break the process. Commented Mar 8, 2018 at 1:20
  • Do you have full error reporting turned on? it might be hitting memory constraints. if so, you could batch your conversion process by performing the translation in chunks. Commented Mar 8, 2018 at 1:25

2 Answers 2

1

After your comment about limiting the results, my thought is you're hitting a memory limit on your script. I'd try performing the encode/decode on a per item basis...

This will perform the translation at a per item level.

$mocklist = array_fill( 0, 100, (object) array('foo'=>'foo','bar'=>'bar') );

array_walk( $mocklist, function( &$value ){
  $value = json_decode( json_encode( $value ), true );
});

print_r( $mocklist );

You could also leverage this to locate which $value becomes empty in a crude manual debugging way.

array_walk( $mocklist, function( $value ){
  if( empty( json_decode( json_encode( $value ), true ) )
  {
    print_r( $value );
    exit('Found the empty one!');
  }
});
Sign up to request clarification or add additional context in comments.

4 Comments

I thought that it could be a memory limit issue. But I have another query on different tables, and it has the results (both in individual records and in the numbers of the records) bigger than this one. And it is fine. Plus, memory limit will give me error message.
I could use other approach to solve this particular issue. But I would like to find out if $result = json_decode(json_encode($result), true); is error proof to transfer array of objects to array of arrays. Because I have been using this approach quite a few times. If there are issues, I might need to go back to change the other codes just to avoid future same problems.
Yes. I would use your suggestions to find out when the value becomes empty to narrow down the problem. Thanks.
php.net/manual/en/function.json-last-error-msg.php Thanks. I should check the php manual carefully. Missed this function. I will use this function to find out what is wrong. My guess is utf8 issue. I will keep you informed if it is the issue. Thanks!
0

Suggested by Scuzzy, I should use php.net/manual/en/function.json-last-error-msg.php to report the error.

Yes. there is an error when I do the json_encode.

Malformed UTF-8 characters, possibly incorrectly encoded

I used to run to this UTF-8 malformed issue frequently before, due to two reasons

  • Database tables were not utf8 encoded.
  • data transfer was in xml format instead of json format

But since the database' tables are mostly encoded as utf8 today, and more and more the data transfer in json format rather than xml, I had not run into utf8 characters issue for a long time.

Now go back to my issue, my tables are

ENGINE=InnoDB DEFAULT CHARSET=utf8 

I need to figure out why the query results from these tables still gave me utf8 Malformed problems.

Thanks!

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.