Jump to content

[SOLVED] Removing entries from arrays


mikesta707

Recommended Posts

Ok, so what I'm trying to do is go through an array, and get the columns of the array that are numeric (its a 2-d array) Is there an easy way to simply delete an entry in an array. I looked up on google for a few answer, but most answers used the array_filter function, which isn't what I want.

 

basically I have the following so far

temp_cols = array();
$non_cols = array();
$arr = $this->data;
	foreach($arr as $row){
		foreach($row as $key => $cols){
			if (ctype_digit($cols) && !in_array($key, $non_cols)){
				if (!in_array($key, $temp_cols)){
					temp_cols[] = $key;
				}
			}
			else {
				$non_cols[] = $key;
				if (in_array($key, $temp_cols)){
				//remove entry here
				}
			}
		}
	}

 

basically, this function goes through every cell in the 2d array, and checks if the specific cell is numeric, and if the column it is part of isn't in the array of numeric columns, then I want to add it to the columns. however, since some columns may have some numeric, and some non-numeric entries, I put any columns that have non-numeric entries into the another array, and remove the column from the numeric columns array if it is in there.

 

I know this is probably pretty simple, but I just can't think of how to do it efficiently and painless (I would rather not have to use array_filter, and make my own function)

 

Any ideas?

 

Anyone have any ideas?

 

EDIT: Just noticed im using in_array horribly wrong. Fixed now to reflect my actual code

Link to comment
https://forums.phpfreaks.com/topic/171035-solved-removing-entries-from-arrays/
Share on other sites

Ok i fixed it. If anyone is curious, I used the array splice method. Here is the full function:

 

public function getNumericCols(){
	if ($this->data == null || !is_array($this->data)){
		print_r($this->data);
		$this->error($this->errors['INVALID_DATA_TYPE']);
	}
	$temp_cols = array();
	$non_cols = array();
	$arr = $this->data;
	foreach($arr as $row){
		$row = array_values($row);//my array is associative, so I need to make it numeric, because I want col nums, not name
		foreach($row as $key => $cols){
			if (ctype_digit($cols) && !in_array($key, $non_cols)){
				if (!in_array($key, $temp_cols)){
					$temp_cols[] = $key;
				}
			}
			else {
				if (!in_array($key, $non_cols)){
					$non_cols[] = $key;
				}
				if (in_array($key, $temp_cols)){
				$key1 = array_keys($temp_cols, $key);
				array_splice($temp_cols, $key1, 1);
				}
			}
		}
	}
	print_r(array_slice($this->data, -5, 5));
	echo "<br />";
	print_r($temp_cols);
}

 

The output was

 

first print_r:

Array (

[0] => Array ( [ORG_ID] => txsabor [TABLE_NAME] => LST_CH_RESIDENTIAL [FIELD_NAME] => ORG_ID [COUNT_ALL] => 13185 [COUNT_POPULATED] => 13185 )

[1] => Array ( [ORG_ID] => txsabor [TABLE_NAME] => LST_CH_RESIDENTIAL [FIELD_NAME] => LISTING_ID [COUNT_ALL] => 13185 [COUNT_POPULATED] => 13185 )

[2] => Array ( [ORG_ID] => txsabor [TABLE_NAME] => LST_CH_RESIDENTIAL [FIELD_NAME] => AD_GEO_ZIP_CODE [COUNT_ALL] => 13185 [COUNT_POPULATED] => 12425 )

[3] => Array ( [ORG_ID] => txsabor [TABLE_NAME] => LST_CH_RESIDENTIAL [FIELD_NAME] => IN_LIST_DATE [COUNT_ALL] => 13185 [COUNT_POPULATED] => 12827 )

[4] => Array ( [ORG_ID] => txsabor [TABLE_NAME] => LST_CH_RESIDENTIAL [FIELD_NAME] => IN_LIST_PRICE [COUNT_ALL] => 13185 [COUNT_POPULATED] => 13185 ) )

 

Second print_r:

Array ( [0] => 3 [1] => 4 )

 

As you can see the last two columns (index 3 and 4) are numeric. the others are not, so this worked well.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.