2009年12月07日 過去Blog
$example_array= array( "green", "red", "green", "blue", "red"); phpで上記のような配列から、重複要素を削除するには $example_array = array_unique($example_array); とすることで重複要素は削除されます。
このままだと
Array
(
    [0] => green
    [1] => red
    [3] => blue
)
のように重複を削除した箇所は添え字も削除され、添え字が飛び飛びになってしまいます。

これを添え字も以下のように、順番通りにしたい場合はarray_values関数を使います。
Array
(
    [0] => green
    [1] => red
    [2] => blue
)

$example_array = array_values($example_array);

とすることで添え字が再配布されます。

この投稿へのコメント

コメントはまだありません。

コメントを残す

メールアドレスが公開されることはありません。

次のHTML タグと属性が使えます。
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CAPTCHA


ピックアップ記事

phpでwhois情報の取得

2009年12月26日 過去Blog
Net_Whois::query() – whois 問い合わせを実行する pear install Net_Socket pear install Net_Whois もし、「WARNING: channel "pear.php.net" has updated its protocols, use "channel-update pear.php.net" to update」 という警告が出る場合は、以下を実行して、pear.php.netをアップデートします。 # pear channel-update pear.php.net 私の場合は、警告は出ましたが、アップグレードしなくても pear install Net_Socket pear install Net_Whois のインストールは完了しました。 …
「phpでwhois情報の取得」をはてなブックマークに追加

外部のサイトから画像をサーバに取得(コピー)

2009年12月06日 過去Blog
$img_path // 取得する画像のパス(URL) $path = "ここは保存先のパス";  //  /home/ドメイン/public_html/imagesなど $img = file_get_contents($img_path) ;//画像を取得 $fullpath = $path.basename($img_path);//画像の保存フルパス file_put_conten…
「外部のサイトから画像をサーバに取得(コピー)」をはてなブックマークに追加

PCのバックアップ

2010年10月05日 過去Blog
PCのバックアップ箇所のメモ デスクトップ マイドキュメント アウトルック(dbx,アカウント) ffftp hostsファイル IE,fireFox ブックマーク
「PCのバックアップ」をはてなブックマークに追加
© graffiti on the web . All rights reserved. WordPress Theme by comfy