LoginSignup
4
4

More than 5 years have passed since last update.

PHP5.4.7でstdClassをArrayに変換した時に詰まったこと

Posted at

タグの環境にてページネーションを実装中に現象。
https://gist.github.com/mitukiii/917075
こちらのロジックを使用させて頂きました。

$array = stdClassToArray($results);    // resultsにはSQLをページネーションした形で取得したstdClass objectが入っている
$page = array(
            'current' => $array['currentPage'],
            'perpage' => $array['perPage'],
            'last' => $array['lastPage'],
            'total' => $array['total']
        );

としたところ

Undefined index: currentPage

なんぞ?
と$arrayの中身のダンプを取ってみたところ
WS000210.JPG

何か化けてる…?
取りあえず全ソースファイルの文字コードをUTF-8に強制変換かけてみるも効果無し。
仕方ないので文字をヘキサ表示してみる

var_dump(bin2hex($key));

WS000211.JPG

おぉ…null文字が混入している。
null*nullになっている意味が正直分かりませんが取りあえずnullをどうにかしてあげるといいということですね。

$key = str_replace('@', '', $key);

これを

$key = str_replace("\x00", "", str_replace('@', '', $key));

こうするとcurrentPageのところが
WS000212.JPG

こうなって取りあえず文字化けは解消しました。
アスタリスクは…正直どうにもなりませんね。
コントローラー側で

        $array = stdClassToArray($results);
        $page = array(
            'current' => $array['*currentPage'],
            'perpage' => $array['*perPage'],
            'last' => $array['*lastPage'],
            'total' => $array['*total']
        );

アスタリスク足して指定してやることでちゃんととれました。
View(Smarty)側への受け渡しはいつも通りで。

4
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4