Skip to content

Instantly share code, notes, and snippets.

@tadasuke
Last active August 29, 2015 14:22
Show Gist options
  • Save tadasuke/a89e544c466e2132d88e to your computer and use it in GitHub Desktop.
Save tadasuke/a89e544c466e2132d88e to your computer and use it in GitHub Desktop.
PHPで連想配列をforeachで回した時のkeyの型 ref: http://qiita.com/tadasuke/items/52e6a02d9af813443e17
$array = array( '1' => 'hoge' );
foreach ( $array as $key => $value ) {
if ( $key === '1' ) {
echo( 'ゲッツ!!' );
}
}
foreach ( $array as $key => $value ) {
if ( $key === 1 ) {
echo( 'ゲッツ!!' );
}
}
$array = array(
'1' => 'hoge'
, 1 => 'fuga'
);
Array( [1] => fuga )
$array = array();
$array[] = 'hoge';
$array[] = 'fuga';
$array['1'] = 'foo';
Array( [1] => fuga )
$array = array();
$array[] = 'hoge';
$array[] = 'fuga';
$array['1'] = 'foo';
Array( [0]=>hoge [1]=>foo )
Array( [0]=>hoge [1]=>foo )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment