如何在此数组中包含条件语句?

时间:2015-10-16 08:30:26

标签: php

当前代码:

var directory = Android.OS.Environment.ExternalStorageDirectory;

var fullFileName = Path.Combine(directory.ToString(), "1.mp3");
if (File.Exists(fullFileName))
{
    //proceed
}

我想创建什么(我知道这段代码无效,我需要有关如何实现目标的建议):

$columns = array(
    array('db' => 'code', 'dt' => 0),
    array('db' => 'img', 'dt' => 1),
    array('db' => 'name', 'dt' => 2),
    array('db' => 'short_description', 'dt' => 3),
);

2 个答案:

答案 0 :(得分:2)

试试这个

function get_my_posts(){

    $posts_per_page = $_POST['posts_per_page'];
    $category = $_POST['category'];

    $args = array(
        'posts_per_page'   => $posts_per_page,
        'offset'           => 0,
        'category'         => $category,
        'orderby'          => 'date',
        'order'            => 'DESC'
    );
    $myposts = get_posts( $args );

    if ( $myposts ) {

        foreach ( $myposts as $post ) {
            setup_postdata( $post );
            get_template_part( 'content', get_post_format() );
        }
        wp_reset_postdata();

    } else {
        echo 'Ingen innhold';
    }

    wp_die();
}

add_action("wp_ajax_get_my_posts", "get_my_posts");
add_action('wp_ajax_nopriv_get_my_posts', 'get_my_posts');

答案 1 :(得分:1)

您可以使用array_merge()

$columns = array(
    array('db' => 'code', 'dt' => 0),
    array('db' => 'img', 'dt' => 1),
    array('db' => 'name', 'dt' => 2),
    array('db' => 'short_description', 'dt' => 3),
);

if ({your condition}) {
    $columns = array_merge($columns, array(array('db' => 'short_description', 'dt' => 3)));
}