从数组中删除空值

时间:2013-04-15 18:39:01

标签: php arrays

我正在使用php函数创建带分页的条目的用户列表。我需要删除没有名称的条目,所以我尝试了这段代码,但它不起作用:没有删除没有名字的条目。

$listing->entries( $order, $eLimit, $eLimStart, true, array('owner' => $userid, 'name' != null), false, $section);

相反的效果很好,使用此代码我得到的所有条目都没有名称:

$listing->entries( $order, $eLimit, $eLimStart, true, array('owner' => $userid, 'name' => null), false, $section);

名称在DB中存储如下:

- type: varchar(250)
- interclassement: utf8_general_ci
- Null: Yes
- Default: NULL

我正在使用PHP版本5.3.16。

我做错了什么?非常感谢。

** 编辑 * * 上下文:Joomla 2.5.9和SobiPro 1.08。我使用SobiPro API创建列表:

$listing = SPFactory::Controller('listing');
$listing = new SPListingCtrl();

功能条目如下所示

public function entries( $eOrder, $eLimit = null, $eLimStart = null, $count = false, $conditions = array(), $entriesRecursive = false, $pid = -1 )
{
    return $this->getEntries( $eOrder, $eLimit, $eLimStart, $count, $conditions, $entriesRecursive, $pid );
}

函数getEntries看起来像这样:

/**
 * @param string $eOrder
 * @param int $eLimit
 * @param int $eLimStart
 * @return array
 */
public function getEntries( $eOrder, $eLimit = null, $eLimStart = null, $count = false, $conditions = array(), $entriesRecursive = false, $pid = 0 )
{
    /* var SPDb $db */
    $db =& SPFactory::db();
    $eClass = SPLoader::loadModel( 'entry' );
    $entries = array();
    $eDir = 'asc';
    $oPrefix = null;
    $conditions = is_array( $conditions ) ? $conditions : array();

    /* get the ordering and the direction */
    if ( strstr( $eOrder, '.' ) ) {
        $eOr = explode( '.', $eOrder );
        $eOrder = array_shift( $eOr );
        $eDir = implode( '.', $eOr );
    }
    $pid = $pid ? $pid : SPRequest::sid();
    /* if sort by name, then sort by the name field */
    if ( $eOrder == 'name' ) {
        $eOrder = SPFactory::config()->nameField()->get( 'fid' );
    }
    if ( $entriesRecursive ) {
        $pids = $this->_model->getChilds( 'category', true );
        if ( is_array( $pids ) ) {
            $pids = array_keys( $pids );
        }
        $pids[ ] = SPRequest::sid();
        $conditions[ 'sprl.pid' ] = $pids;
    }
    else {
        $conditions[ 'sprl.pid' ] = $pid;
    }
    if ( $pid == -1 ) {
        unset( $conditions[ 'sprl.pid' ] );
    }

    /* sort by field */
    if ( strstr( $eOrder, 'field_' ) ) {
        static $field = null;
        $specificMethod = false;
        if ( !$field ) {
            try {
                $db->select( 'fieldType', 'spdb_field', array( 'nid' => $eOrder, 'section' => Sobi::Section() ) );
                $fType = $db->loadResult();
            }
            catch ( SPException $x ) {
                Sobi::Error( $this->name(), SPLang::e( 'CANNOT_DETERMINE_FIELD_TYPE', $x->getMessage() ), SPC::WARNING, 0, __LINE__, __FILE__ );
            }
            if ( $fType ) {
                $field = SPLoader::loadClass( 'opt.fields.' . $fType );
            }
        }
        if ( $field && method_exists( $field, 'sortBy' ) ) {
            $table = null;
            $oPrefix = null;
            $specificMethod = call_user_func_array( array( $field, 'sortBy' ), array( &$table, &$conditions, &$oPrefix, &$eOrder, &$eDir ) );
        }
        if ( !$specificMethod ) {
            $table = $db->join(
                array(
                    array( 'table' => 'spdb_field', 'as' => 'fdef', 'key' => 'fid' ),
                    array( 'table' => 'spdb_field_data', 'as' => 'fdata', 'key' => 'fid' ),
                    array( 'table' => 'spdb_object', 'as' => 'spo', 'key' => array( 'fdata.sid', 'spo.id' ) ),
                    array( 'table' => 'spdb_relations', 'as' => 'sprl', 'key' => array( 'fdata.sid', 'sprl.id' ) ),
                )
            );
            $oPrefix = 'spo.';
            $conditions[ 'spo.oType' ] = 'entry';
            $conditions[ 'fdef.nid' ] = $eOrder;
         //   $eOrder = 'baseData.' . $eDir;
         $eOrder = 'baseData = "", CONVERT(baseData,UNSIGNED INTEGER).' . $eDir;
        }
    }

    else {
        $table = $db->join( array(
            array( 'table' => 'spdb_relations', 'as' => 'sprl', 'key' => 'id' ),
            array( 'table' => 'spdb_object', 'as' => 'spo', 'key' => 'id' )
        ) );
        $conditions[ 'spo.oType' ] = 'entry';
        $eOrder = $eOrder . '.' . $eDir;
        $oPrefix = 'spo.';
        if ( strstr( $eOrder, 'valid' ) ) {
            $eOrder = $oPrefix . $eOrder;
        }
    }

    /* check user permissions for the visibility */
    if ( Sobi::My( 'id' ) ) {
        $this->userPermissionsQuery( $conditions, $oPrefix );
        if( isset( $conditions[ $oPrefix . 'state' ] ) && $conditions[ $oPrefix . 'state' ] ) {
            $conditions[ 'sprl.copy' ] = 0;
        }
    }
    else {
        $conditions = array_merge( $conditions, array( $oPrefix . 'state' => '1', '@VALID' => $db->valid( $oPrefix . 'validUntil', $oPrefix . 'validSince' ) ) );
        $conditions[ 'sprl.copy' ] = '0';
    }
    try {
        $db->select( $oPrefix . 'id', $table, $conditions, $eOrder, $eLimit, $eLimStart, true );
        $results = $db->loadResultArray();
    }
    catch ( SPException $x ) {
        Sobi::Error( $this->name(), SPLang::e( 'DB_REPORTS_ERR', $x->getMessage() ), SPC::WARNING, 0, __LINE__, __FILE__ );
    }
    Sobi::Trigger( $this->name(), 'AfterGetEntries', array( &$results, $count ) );
    if ( count( $results ) && !$count ) {
        $memLimit = ( int )ini_get( 'memory_limit' ) * 2097152;
        foreach ( $results as $i => $sid ) {
            // it needs too much memory moving the object creation to the view
            //$entries[ $i ] = SPFactory::Entry( $sid );
            $entries[ $i ] = $sid;
        }
    }
    if ( $count ) {
        return $results;
    }
    return $entries;
}

2 个答案:

答案 0 :(得分:0)

不是寻找非空条目,也许你可以做相反的事情并寻找具有某些东西的条目,如下:

$listing->entries( $order, $eLimit, $eLimStart, true, array('owner' => $userid, 'name' => '%'), false, $section);

然而,这纯粹是一种猜测,因为我们需要查看entries函数来查看各种参数的处理方式。

答案 1 :(得分:0)

已解决 - 感谢大家的帮助!

$ listing->条目($ order,$ eLimit,$ eLimStart,true,array('owner'=> $ userid,'!name'=> null),false,-1);