Made this function to delete elements in an array;
<?php
function array_del_elm($input_array, $del_indexes) {
if (is_array($del_indexes)) {
$indexes = $del_indexes;
} elseif(is_string($del_indexes)) {
$indexes = explode($del_indexes, " ");
} elseif(is_numeric($del_indexes)) {
$indexes[0] = (integer)$del_indexes;
} else return;
$del_indexes = null;
$cur_index = 0;
if (sort($indexes)) for($i=0; $i<count($input_array); $i++) {
if ($i == $indexes[$cur_index]) {
$cur_index++;
if ($cur_index == count($indexes)) return $output_array;
continue;
}
$output_array[] = $input_array[$i];
}
return $output_array;
}
?>
but then i saw the methods of doing the same by Tyler Bannister & Paul, could see that theirs were faster, but had floors regarding deleting multiple elements thus support of several ways of giving parameters. I combined the two methods to this to this:
<?php
function array_del_elm($target_array, $del_indexes) {
if (is_array($del_indexes)) {
$indexes = $del_indexes;
} elseif(is_string($del_indexes)) {
$indexes = explode($del_indexes, " ");
} elseif(is_numeric($del_indexes)) {
$indexes[0] = (integer)$del_indexes;
} else return;
unset($del_indexes);
for($i=0; $i<count($indexes); $i++) {
unset($target_array[$indexes[$i]]);
}
return $target_array;
}
?>
Fast, compliant and functional ;)
配列
- 導入
- インストール/設定
- 定義済み定数
- 配列のソート
- 配列 関数
- array_change_key_case — 配列のすべてのキーを変更する
- array_chunk — 配列を分割する
- array_combine — 一方の配列をキーとして、もう一方の配列を値として、ひとつの配列を生成する
- array_count_values — 配列の値の数を数える
- array_diff_assoc — 追加された添字の確認を含めて配列の差を計算する
- array_diff_key — キーを基準にして配列の差を計算する
- array_diff_uassoc — ユーザが指定したコールバック関数を利用し、 追加された添字の確認を含めて配列の差を計算する
- array_diff_ukey — キーを基準にし、コールバック関数を用いて配列の差を計算する
- array_diff — 配列の差を計算する
- array_fill_keys — キーを指定して、配列を値で埋める
- array_fill — 配列を指定した値で埋める
- array_filter — コールバック関数を使用して、配列の要素をフィルタリングする
- array_flip — 配列のキーと値を反転する
- array_intersect_assoc — 追加された添字の確認も含めて配列の共通項を確認する
- array_intersect_key — キーを基準にして配列の共通項を計算する
- array_intersect_uassoc — 追加された添字の確認も含め、コールバック関数を用いて 配列の共通項を確認する
- array_intersect_ukey — キーを基準にし、コールバック関数を用いて 配列の共通項を計算する
- array_intersect — 配列の共通項を計算する
- array_key_exists — 指定したキーまたは添字が配列にあるかどうかを調べる
- array_keys — 配列のキーをすべて返す
- array_map — 指定した配列の要素にコールバック関数を適用する
- array_merge_recursive — 二つ以上の配列を再帰的にマージする
- array_merge — ひとつまたは複数の配列をマージする
- array_multisort — 複数の多次元の配列をソートする
- array_pad — 指定長、指定した値で配列を埋める
- array_pop — 配列の末尾から要素を取り除く
- array_product — 配列の値の積を計算する
- array_push — 一つ以上の要素を配列の最後に追加する
- array_rand — 配列から一つ以上の要素をランダムに取得する
- array_reduce — コールバック関数を用いて配列を普通の値に変更することにより、配列を再帰的に減らす
- array_replace_recursive — 渡された配列の要素を再帰的に置き換える
- array_replace — 渡された配列の要素を置き換える
- array_reverse — 要素を逆順にした配列を返す
- array_search — 指定した値を配列で検索し、見つかった場合に対応するキーを返す
- array_shift — 配列の先頭から要素を一つ取り出す
- array_slice — 配列の一部を展開する
- array_splice — 配列の一部を削除し、他の要素で置換する
- array_sum — 配列の中の値の合計を計算する
- array_udiff_assoc — データの比較にコールバック関数を用い、 追加された添字の確認を含めて配列の差を計算する
- array_udiff_uassoc — データと添字の比較にコールバック関数を用い、 追加された添字の確認を含めて配列の差を計算する
- array_udiff — データの比較にコールバック関数を用い、配列の差を計算する
- array_uintersect_assoc — データの比較にコールバック関数を用い、 追加された添字の確認も含めて配列の共通項を計算する
- array_uintersect_uassoc — データと添字の比較にコールバック関数を用い、 追加された添字の確認も含めて配列の共通項を計算する
- array_uintersect — データの比較にコールバック関数を用い、配列の共通項を計算する
- array_unique — 配列から重複した値を削除する
- array_unshift — 一つ以上の要素を配列の最初に加える
- array_values — 配列の全ての値を返す
- array_walk_recursive — 配列の全ての要素に、ユーザー関数を再帰的に適用する
- array_walk — 配列の全ての要素にユーザ関数を適用する
- array — 配列を生成する
- arsort — 連想キーと要素との関係を維持しつつ配列を逆順にソートする
- asort — 連想キーと要素との関係を維持しつつ配列をソートする
- compact — 変数名とその値から配列を作成する
- count — 変数に含まれるすべての要素、 あるいはオブジェクトに含まれるプロパティの数を数える
- current — 配列内の現在の要素を返す
- each — 配列から現在のキーと値のペアを返して、カーソルを進める
- end — 配列の内部ポインタを最終要素にセットする
- extract — 配列からシンボルテーブルに変数をインポートする
- in_array — 配列に値があるかチェックする
- key — 配列からキーを取り出す
- krsort — 配列をキーで逆順にソートする
- ksort — 配列をキーでソートする
- list — 配列と同様の形式で、複数の変数への代入を行う
- natcasesort — 大文字小文字を区別しない"自然順"アルゴリズムを用いて配列をソートする
- natsort — "自然順"アルゴリズムで配列をソートする
- next — 内部配列ポインタを進める
- pos — current のエイリアス
- prev — 内部の配列ポインタをひとつ前に戻す
- range — ある範囲の整数を有する配列を作成する
- reset — 配列の内部ポインタを先頭の要素にセットする
- rsort — 配列を逆順にソートする
- shuffle — 配列をシャッフルする
- sizeof — count のエイリアス
- sort — 配列をソートする
- uasort — ユーザ定義の比較関数で配列をソートし、連想インデックスを保持する
- uksort — ユーザ定義の比較関数を用いて、キーで配列をソートする
- usort — ユーザー定義の比較関数を使用して、配列を値でソートする
配列
sunear at gmail dot com
04-Aug-2009 07:45
04-Aug-2009 07:45
Tyler Bannister
11-Jun-2009 03:59
11-Jun-2009 03:59
To delete an individual array element use the unset function
For example:
<?PHP
$arr = array( "A", "B", "C" );
unset( $arr[1] );
// now $arr = array( "A", "C" );
?>
Unlink is for deleting files.
Paul
21-May-2009 11:05
21-May-2009 11:05
To delete an individual array element use the unlink function
For example:
<?PHP
$arr = array( "A", "B", "C" );
unlink( $arr[1] );
// now $arr = array( "A", "C" );
?>
thomasdecaux at ebuildy dot com
15-May-2009 10:14
15-May-2009 10:14
To browse a simple array:
<?php
foreach ($myArray AS $myItem)
{
}
?>
To browse an associative array:
<?php
foreach ($myArray AS $key=>$value)
{
}
?>
http://www.ebuildy.com
macnimble at gmail dot com
27-Mar-2009 03:59
27-Mar-2009 03:59
Converting a linear array (like a mysql record set) into a tree, or multi-dimensional array can be a real bugbear. Capitalizing on references in PHP, we can 'stack' an array in one pass, using one loop, like this:
<?php
# array_stack()
# Original idea from:
# http://www.ideashower.com/our_solutions/
# create-a-parent-child-array-structure-in-one-pass/
function array_stack (&$a, $p = '@parent', $c = '@children')
{
$l = $t = array();
foreach ($a AS $key => $val):
if (!$val[$p]) $t[$key] =& $l[$key];
else $l[$val[$p]][$c][$key] =& $l[$key];
$l[$key] = (array)$l[$key] + $val;
endforeach;
return $a = array('tree' => $t, 'leaf' => $l);
}
# Example:
$node = array();
$node[1] = array('@parent' => 0, 'title' => 'I am node 1.');
# ^-----------------------v Link @parent value to key.
$node[2] = array('@parent' => 1, 'title' => 'I am node 2.');
$node[3] = array('@parent' => 2, 'title' => 'I am node 3.');
$node[4] = array('@parent' => 1, 'title' => 'I am node 4.');
$node[5] = array('@parent' => 4, 'title' => 'I am node 5.');
array_stack($node);
$node['leaf'][1]['title'] = 'I am node one.';
$node['leaf'][2]['title'] = 'I am node two.';
$node['leaf'][3]['title'] = 'I am node three.';
$node['leaf'][4]['title'] = 'I am node four.';
$node['leaf'][5]['title'] = 'I am node five.';
echo '<pre>',print_r($node['tree'],TRUE),'</pre>';
?>
Note that there's no parameter checking on the array value, but this is only to keep the function size small. One could easily a quick check in there to make sure the $a parameter was in fact an array.
Hope you find it useful. Huge thanks to Nate Weiner of IdeaShower.com for providing the original function I built on.
andyd273 at gmail dot com
24-Feb-2009 10:12
24-Feb-2009 10:12
A small correction to Endel Dreyer's PHP array to javascript array function. I just changed it to show keys correctly:
function array2js($array,$show_keys)
{
$dimensoes = array();
$valores = array();
$total = count ($array)-1;
$i=0;
foreach($array as $key=>$value){
if (is_array($value)) {
$dimensoes[$i] = array2js($value,$show_keys);
if ($show_keys) $dimensoes[$i] = '"'.$key.'":'.$dimensoes[$i];
} else {
$dimensoes[$i] = '"'.addslashes($value).'"';
if ($show_keys) $dimensoes[$i] = '"'.$key.'":'.$dimensoes[$i];
}
if ($i==0) $dimensoes[$i] = '{'.$dimensoes[$i];
if ($i==$total) $dimensoes[$i].= '}';
$i++;
}
return implode(',',$dimensoes);
}
Endel Dreyer
20-Jan-2009 01:16
20-Jan-2009 01:16
Function to convert PHP arrays to JavaScript Array Object:
Useful in some cases.
<?php
function array2js($array,$show_keys=false)
{
$dimensoes = array();
$valores = array();
$total = count ($array)-1;
$i=0;
foreach ($array as $key => $value) {
if (is_array($value)) {
$dimensoes[$i] = array2js($value,$show_keys);
} else {
$dimensoes[$i] = '"'.addslashes($value).'"';
if ($show_keys) '"'.$key.'":'.$dimensoes[$i];
}
if ($i==0) $dimensoes[$i] = '['.$dimensoes[$i];
if ($i==$total) $dimensoes[$i].= ']';
$i++;
}
return implode(',',$dimensoes);
}
?>
Anonymous
26-Nov-2008 07:36
26-Nov-2008 07:36
@jorge at andrade dot cl
This variant is faster:
<?php
function array_avg($array,$precision=2){
if(!is_array($array))
return 'ERROR in function array_avg(): this is a not array';
foreach($array as $value)
if(!is_numeric($value))
return 'ERROR in function array_avg(): the array contains one or more non-numeric values';
$cuantos=count($array);
return round(array_sum($array)/$cuantos,$precision);
}
?>
jorge at andrade dot cl
12-Nov-2008 09:03
12-Nov-2008 09:03
To calculate the average of a array
<?php
function array_avg($array,$precision="2"){
$a=0;
if(is_array($array)){
foreach($array as $value):
if(!is_numeric($value)){
$a++;
}
endforeach;
if($a==0){
$cuantos=count($array);
return round(array_sum($array)/$cuantos,$precision);
}else{
return "ERROR in function array_avg(): the array contains one or more non-numeric values";
}
}else{
return "ERROR in function array_avg(): this is a not array";
}
}
?>
alan dot lake at lakeinfoworks dot com
22-Oct-2008 05:46
22-Oct-2008 05:46
To delete an array key (and its value) from an array:
<?php
function array_delete(&$ary,$key_to_be_deleted)
{
$new = array();
if(is_string($key_to_be_deleted)) {
if(!array_key_exists($key_to_be_deleted,$ary)) {
return;
}
foreach($ary as $key => $value) {
if($key != $key_to_be_deleted) {
$new[$key] = $value;
}
}
$ary = $new;
}
if(is_array($key_to_be_deleted)) {
foreach($key_to_be_deleted as $del) {
array_delete(&$ary,$del);
}
}
}
?>
webmaster at infoproducts dot x10hosting dot com
11-Oct-2008 04:13
11-Oct-2008 04:13
New value can also be added to the array as shown below.
$theVariable["google"] = "http//google.com";
or
$theVariable["1"] = "http//google.com";
Jack A
07-Oct-2008 05:14
07-Oct-2008 05:14
Note that arrays are not allowed in class constants and trying to do so will throw a fatal error.
contact at greyphoenix dot biz
01-Jul-2008 06:36
01-Jul-2008 06:36
<?php
//Creating a multidimensional array
$theVariable = array("Search Engines" =>
array (
0=> "http//google.com",
1=> "http//yahoo.com",
2=> "http//msn.com/"),
"Social Networking Sites" =>
array (
0 => "http//www.facebook.com",
1 => "http//www.myspace.com",
2 => "http//vkontakte.ru",)
);
echo "The first array value is " . $theVariable['Search Engines'][0];
?>
-- Output--
The first array value is http://google.com
applegrew at rediffmail dot com
28-May-2008 05:23
28-May-2008 05:23
For newbies like me.
Creating new arrays:-
//Creates a blank array.
$theVariable = array();
//Creates an array with elements.
$theVariable = array("A", "B", "C");
//Creating Associaive array.
$theVariable = array(1 => "http//google.com", 2=> "http://yahoo.com");
//Creating Associaive array with named keys
$theVariable = array("google" => "http//google.com", "yahoo"=> "http://yahoo.com");
Note:
New value can be added to the array as shown below.
$theVariable[] = "D";
$theVariable[] = "E";
