Jump to content

Recommended Posts

Hiya!

 

I was wondering if there is a way to check if an array has values EG.

 

array('ONE' => 'ONE', 'TWO');

 

ONE = TRUE

TWO = FALSE

 

How would I then filter out that array key TWO does not have a value?

 

Many thanks

 

James.

 

 

TWO is not array array index in your example. It is the value at index 1.

 

Confused LOL :S.

 

What I'm building involves me to insert and array like the one I provided into a class but it will not work if there is a missing value (Like TWO), so what I need to do is filter the whole array, maybe if I filter for numeric's ?

 

This is my code.

 

<?php session_start();

class template {

private $tags = array();

	function newTemplate($tags) {

		if(!is_array($tags) OR !is_dir('./assets/templates/') OR !file_exists('./assets/templates/global.html') OR !is_file('./assets/templates/global.html') OR !file_exists('./assets/templates/error.html') OR !is_file('./assets/templates/error.html') OR !is_readable('./assets/templates/error.html') OR !is_readable('./assets/templates/global.html')):
			exit('<center>Sorry for inconvenience but our system has located an error due to this the site is now shutdown.</center>');
		else:
			$keys = array_keys($tags);
			$del = array();
			$tagcheck = array('ONE');
			$file = file_get_contents('./assets/templates/global.html');

			if(!$file OR !is_array($tagcheck)):
				exit('<center>Sorry for inconvenience but our system has located an error due to this the site is now shutdown.</center>');
			else:

				foreach($tags as $item => $itemvalue):
					if(!in_array($item, $keys, true)):
						$del[] = '/{' . $item . '}/';
					endif;
				endforeach;

				foreach($keys as $key => $value):
					$keys[$key] = '{' . $value . '}';
				endforeach;

				$file = preg_replace($del, '', $file);

				echo str_replace($keys, $tags, $file);

			endif;
		endif;

	}
}
?>

 

Many thanks

 

James.

So, you want to check if the array is an associative array?

 

function is_assoc($array) {
    return (is_array($array) && 0 !== count(
        array_diff_key(
            $array, array_keys(
                array_keys($array)
            )
        )
    ));
}

Cheers mate, just wondering how I use it LOL?

 

Many many thanks

 

James.

 

function is_assoc($array) {
    return (is_array($array) && 0 !== count(
        array_diff_key(
            $array, array_keys(
                array_keys($array)
            )
        )
    ));
}

$arr = array('ONE' => 'ONE', 'TWO');
echo is_assoc($arr) ? 'is assoc' : 'is not assoc';

Still not much luck mate,

 

$array = array('ONE', 'TWO' => 'TWO');

function is_assoc($array) {
    return (is_array($array) && 0 !== count(
        array_diff_key(
            $array, array_keys(
                array_keys($array)
            )
        )
    ));
}

echo is_assoc($array) ? 'is assoc' : 'is not assoc';

 

echo's; is assoc

 

Many thanks

 

James.

I might not quite be understanding what you want to get out of this but here's an idea.

 

$array = array('ONE' => 'ONE', 'TWO');
foreach ($array as $key => $value) {
    if (is_int($key)) {
        unset($array[$key]);
    }
}

// See what is in the array
print_r($array);

I might not quite be understanding what you want to get out of this but here's an idea.

 

$array = array('ONE' => 'ONE', 'TWO');
foreach ($array as $key => $value) {
    if (is_int($key)) {
        unset($array[$key]);
    }
}

// See what is in the array
print_r($array);

 

Stunning! Worked like a charm thanks your all your help guy's.

 

Many thanks

 

James.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.