jamesxg1 Posted March 23, 2010 Share Posted March 23, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/196222-how-do-i-check-if-a-multi-dimension-array-has-a-value/ Share on other sites More sharing options...
trq Posted March 23, 2010 Share Posted March 23, 2010 TWO is not array array index in your example. It is the value at index 1. Quote Link to comment https://forums.phpfreaks.com/topic/196222-how-do-i-check-if-a-multi-dimension-array-has-a-value/#findComment-1030451 Share on other sites More sharing options...
jamesxg1 Posted March 23, 2010 Author Share Posted March 23, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/196222-how-do-i-check-if-a-multi-dimension-array-has-a-value/#findComment-1030454 Share on other sites More sharing options...
trq Posted March 23, 2010 Share Posted March 23, 2010 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) ) ) )); } Quote Link to comment https://forums.phpfreaks.com/topic/196222-how-do-i-check-if-a-multi-dimension-array-has-a-value/#findComment-1030461 Share on other sites More sharing options...
jamesxg1 Posted March 23, 2010 Author Share Posted March 23, 2010 Cheers mate, just wondering how I use it LOL? Many many thanks James. Quote Link to comment https://forums.phpfreaks.com/topic/196222-how-do-i-check-if-a-multi-dimension-array-has-a-value/#findComment-1030470 Share on other sites More sharing options...
jamesxg1 Posted March 23, 2010 Author Share Posted March 23, 2010 Could I do this? if(count(array_keys($array)) == (is_array($array) && 0 !== count(array_diff_key($array, array_keys(array_keys($array)))))) { // good } else { // bad } Many thanks James. Quote Link to comment https://forums.phpfreaks.com/topic/196222-how-do-i-check-if-a-multi-dimension-array-has-a-value/#findComment-1030474 Share on other sites More sharing options...
trq Posted March 23, 2010 Share Posted March 23, 2010 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'; Quote Link to comment https://forums.phpfreaks.com/topic/196222-how-do-i-check-if-a-multi-dimension-array-has-a-value/#findComment-1030477 Share on other sites More sharing options...
jamesxg1 Posted March 23, 2010 Author Share Posted March 23, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/196222-how-do-i-check-if-a-multi-dimension-array-has-a-value/#findComment-1030483 Share on other sites More sharing options...
salathe Posted March 23, 2010 Share Posted March 23, 2010 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); Quote Link to comment https://forums.phpfreaks.com/topic/196222-how-do-i-check-if-a-multi-dimension-array-has-a-value/#findComment-1030501 Share on other sites More sharing options...
jamesxg1 Posted March 23, 2010 Author Share Posted March 23, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/196222-how-do-i-check-if-a-multi-dimension-array-has-a-value/#findComment-1030565 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.