<?php // Example 1 $testValues = "test1 test2 test3 test4 test5"; $testArray = explode(" ", $testValues); print_r($testArray); //Array ( [0] => test1 [1] => test2 [2] => test3 [3] => test4 [4] => test5 ) // Example 2 $testValues = "test1,test2,test3,test4,test5"; $testArray = explode(",", $testValues); print_r($testArray); //Array ( [0] => test1 [1] => test2 [2] => test3 [3] => test4 [4] => test5 ) ?>
Tag Archives: PHP
Convert array into string – PHP
<?php $testArray = array('Test1', 'Test2', 'Test3'); $testString = implode(",", $testArray); echo $testString; // Test1,Test2,Test3 ?>
Remove null values from an array – PHP
<?php $testArray = array('Test1', 'Test2', ''); print_r( array_filter($testArray)); // Array ( [0] => Test1 [1] => Test2 ) ?>
Remove duplicate entries from an array – PHP
<?php $testArray = array('Test1', 'Test2', 'Test1'); print_r( array_unique($testArray)); // Array ( [0] => Test1 [1] => Test2 ) ?>
Find out the number of elements in an array – PHP
<?php $testArray = array('Test1', 'Test2', 'Test1'); echo( count($testArray)); // 3 echo( sizeof($testArray)); // 3 ?>
Find out the length of a string – PHP
<?php echo $length = strlen("Efforts"); // 7 ?>
WordPress Query to fetch the Categories
global $wpdb; $categoriesData = $wpdb->get_results("SELECT c.*,ct.* FROM {$wpdb->prefix}terms as c JOIN {$wpdb->prefix}term_taxonomy as ct ON c.term_id=ct.term_id WHERE ct.taxonomy='category' "); foreach($categoriesData as $key=>$category) { echo $category->name; }
WordPress Query to fetch the subcategories
<?php global $wpdb; $cat="2"//Parent category id $categoriesData = $wpdb->get_results("SELECT c.*,ct.* FROM {$wpdb->prefix}terms as c JOIN {$wpdb->prefix}term_taxonomy as ct ON c.term_id=ct.term_id WHERE ct.taxonomy='category' AND ct.parent ='$cat' "); ?>
WordPress query to fetch the pages
<?php global $wpdb; $querystr = " SELECT distinct(post_title) ,ID,post_title FROM $wpdb->posts WHERE post_type='page' AND post_status='publish' "; $pageposts = $wpdb->get_results($querystr, OBJECT); ?>
WordPress query to fetch the posts
<?php global $wpdb; $querystr = " SELECT distinct(post_title) ,ID,post_title FROM $wpdb->posts WHERE post_type='post' AND post_status='publish' "; $pageposts = $wpdb->get_results($querystr, OBJECT); ?>