Title: Can&#8217;t get data from array
Last modified: August 19, 2016

---

# Can’t get data from array

 *  Resolved [Deniska](https://wordpress.org/support/users/deniska/)
 * (@deniska)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/cant-get-data-from-array/)
 * I want to get data from array. This code is working:
 *     ```
       add_filter( 'single_template', 'my_template' );
       function my_template( $template ) {
       $catid = array(4,5,6);
       if ( in_category($catid) )
       $template = locate_template( array( 'single-port.php' ) );
       return $template;
       }
       ```
   
 * but I try to set data like this :
 *     ```
       add_filter( 'single_template', 'my_template' );
       function my_template( $template ) {
       $catid = array(get_option('imp_cat'));
       if ( in_category($catid) )
       $template = locate_template( array( 'single-port.php' ) );
       return $template;
       }
       ```
   
 * Last example doesnt work. Categories ID doesn’t received.What I need to do?
    
   Thanks!

Viewing 8 replies - 1 through 8 (of 8 total)

 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/cant-get-data-from-array/#post-1398479)
 * What is inside `imp_cat`?
 *  Thread Starter [Deniska](https://wordpress.org/support/users/deniska/)
 * (@deniska)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/cant-get-data-from-array/#post-1398484)
 * inside categories id’s 4,5,6. I’m put this id’s in my theme options. `imp_cat`
   thats a options textfield id. In this textfield I put categories ID’s -4,5,6.
   Before this in all other cases all works is fine but this code isn’t work.
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/cant-get-data-from-array/#post-1398506)
 * You’re not feeding it an array though, you’re placing a string into an array (
   the array only has one item).
 *     ```
       // Example var holding text field value
       $imp_cat = '1,2,3';
   
       // This array only has one value, the data in the string above
       $some_array = array( $imp_cat );
   
       // This would also be the same as above
       $some_array = array('1,2,3');
   
       // The array only has one item, a string with 3 numbers and commas
       ```
   
 * If you want to take a string and split it into an array of items, then you should
   use something like explode on the string to divide it into an array.
 * [http://php.net/manual/en/function.explode.php](http://php.net/manual/en/function.explode.php)
 *     ```
       $var = get_option('option_with_string_of_ids');
   
       // The first parameter is the delimter to find in the string and divide the items by (it removes the delimeter in the process)
       $var = explode( ',' , $var );
       /*
       $var is now an array
       Array(
       0 => ID1,
       1 => ID2
       )
       ..and so on..
       */
       ```
   
 *  Thread Starter [Deniska](https://wordpress.org/support/users/deniska/)
 * (@deniska)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/cant-get-data-from-array/#post-1398513)
 * I’ll try to make this. This isn’t work.
 *     ```
       add_filter( 'single_template', 'my_template' );
   
       function my_template( $template ) {
       $catid = get_option('imp_cat');
       if ( in_category(array($catid)) )
       $template = locate_template( array( 'single-port.php' ) );
       return $template;
       }
       ```
   
 * I dont understand about explode and how to get this work with my function.
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/cant-get-data-from-array/#post-1398517)
 * After this..
 *     ```
       $catid = get_option('imp_cat');
       ```
   
 * Add..
 *     ```
       $catid = strpos( $catid , ',' ) ? explode( ',' , $catid ) : array( (int) $catid );
       ```
   
 * And change this..
 *     ```
       if ( in_category(array($catid)) )
       ```
   
 * ..for..
 *     ```
       if ( in_category( $catid ) )
       ```
   
 * Refs:
    [http://codex.wordpress.org/Function_Reference/in_category](http://codex.wordpress.org/Function_Reference/in_category)
   [http://php.net/manual/en/function.strpos.php](http://php.net/manual/en/function.strpos.php)
   [http://php.net/manual/en/function.explode.php](http://php.net/manual/en/function.explode.php)
 *  Thread Starter [Deniska](https://wordpress.org/support/users/deniska/)
 * (@deniska)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/cant-get-data-from-array/#post-1398520)
 * Man THANK YOU VERY MUCH. You’re a good man. Thanks for this help.
 *  Thread Starter [Deniska](https://wordpress.org/support/users/deniska/)
 * (@deniska)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/cant-get-data-from-array/#post-1398535)
 * Sorry for insolence, maybe you can explaine how to resolve [This ](http://wordpress.org/support/topic/364516)
   problem.
    Past few week trying to resolve this problem.
 *  [Shane G.](https://wordpress.org/support/users/shane-g-1/)
 * (@shane-g-1)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/cant-get-data-from-array/#post-1398536)
 * Hi,
 * Refer these articles:
 * [http://codex.wordpress.org/Data_Validation#Arrays](http://codex.wordpress.org/Data_Validation#Arrays)
   
   [http://codex.wordpress.org/Custom_Fields#Advanced_Techniques_for_Custom_Fields](http://codex.wordpress.org/Custom_Fields#Advanced_Techniques_for_Custom_Fields)
 * Thanks,
 * Shane G.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Can’t get data from array’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 8 replies
 * 3 participants
 * Last reply from: [Shane G.](https://wordpress.org/support/users/shane-g-1/)
 * Last activity: [16 years, 2 months ago](https://wordpress.org/support/topic/cant-get-data-from-array/#post-1398536)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
