如何在没有foreach的情况下返回多个索引的对象数据

时间:2014-04-09 06:12:56

标签: php cakephp-2.0

我通过存储过程获取特定表的数据,演示代码是

Array
(
    [0] => Array
        (
            [object_types] => Array
                (
                    [ID] => 11
                    [Code] => Item001
                    [Name] => Item
                    [Description] => Items
                    [DisplayName] => Items
                    [ObjectTypeIdentifier] => 1
                    [CheckPermissions] => 1
                    [DefaultLedgerType_002] => 
                    [DefaultNarration] => 
                    [CopyTaxesFromParent] => 1
                    [CreatedBy] => 1
                    [ModifiedBy] => 1
                    [CreatedDate] => 2014-04-02 00:00:00
                    [ModifiedDate] => 2014-04-08 00:00:00
                    [RevisionNumber] => 1
                    [IsAdd] => 1
                    [IsEdit] => 
                    [IsDelete] => 1
                )

        )

    [1] => Array
        (
            [object_types] => Array
                (
                    [ID] => 12
                    [Code] => Uom001
                    [Name] => Uom
                    [Description] => Uom
                    [DisplayName] => Uom
                    [ObjectTypeIdentifier] => 1
                    [CheckPermissions] => 1
                    [DefaultLedgerType_002] => 1
                    [DefaultNarration] => 1
                    [CopyTaxesFromParent] => 1
                    [CreatedBy] => 1
                    [ModifiedBy] => 1
                    [CreatedDate] => 2014-04-02 00:00:00
                    [ModifiedDate] => 2014-04-02 00:00:00
                    [RevisionNumber] => 1
                    [IsAdd] => 1
                    [IsEdit] => 1
                    [IsDelete] => 1
                )

        )

    [2] => Array
        (
            [object_types] => Array
                (
                    [ID] => 13
                    [Code] => Role
                    [Name] => Role
                    [Description] => Role
                    [DisplayName] => Role
                    [ObjectTypeIdentifier] => 1
                    [CheckPermissions] => 1
                    [DefaultLedgerType_002] => 1
                    [DefaultNarration] => 
                    [CopyTaxesFromParent] => 1
                    [CreatedBy] => 1
                    [ModifiedBy] => 1
                    [CreatedDate] => 2014-04-03 00:00:00
                    [ModifiedDate] => 2014-04-03 00:00:00
                    [RevisionNumber] => 1
                    [IsAdd] => 1
                    [IsEdit] => 
                    [IsDelete] => 1
                )

        )

    [3] => Array
        (
            [object_types] => Array
                (
                    [ID] => 14
                    [Code] => User
                    [Name] => User
                    [Description] => Use
                    [DisplayName] => User
                    [ObjectTypeIdentifier] => 1
                    [CheckPermissions] => 1
                    [DefaultLedgerType_002] => 1
                    [DefaultNarration] => 71
                    [CopyTaxesFromParent] => 1
                    [CreatedBy] => 1
                    [ModifiedBy] => 1
                    [CreatedDate] => 2014-04-03 00:00:00
                    [ModifiedDate] => 2014-04-09 00:00:00
                    [RevisionNumber] => 1
                    [IsAdd] => 1
                    [IsEdit] => 
                    [IsDelete] => 1
                )

        )

    [4] => Array
        (
            [object_types] => Array
                (
                    [ID] => 15
                    [Code] => AccountMaster
                    [Name] => AccountMaster
                    [Description] => AccountMaster
                    [DisplayName] => Account
                    [ObjectTypeIdentifier] => 1
                    [CheckPermissions] => 1
                    [DefaultLedgerType_002] => 1
                    [DefaultNarration] => 1
                    [CopyTaxesFromParent] => 1
                    [CreatedBy] => 1
                    [ModifiedBy] => 1
                    [CreatedDate] => 2014-04-05 00:00:00
                    [ModifiedDate] => 2014-04-05 00:00:00
                    [RevisionNumber] => 1
                    [IsAdd] => 1
                    [IsEdit] => 
                    [IsDelete] => 
                )

        )

    [5] => Array
        (
            [object_types] => Array
                (
                    [ID] => 16
                    [Code] => Contact
                    [Name] => Contact
                    [Description] => Contact
                    [DisplayName] => Contact
                    [ObjectTypeIdentifier] => 1
                    [CheckPermissions] => 1
                    [DefaultLedgerType_002] => 103
                    [DefaultNarration] => 71
                    [CopyTaxesFromParent] => 1
                    [CreatedBy] => 1
                    [ModifiedBy] => 1
                    [CreatedDate] => 2014-04-07 00:00:00
                    [ModifiedDate] => 2014-04-08 00:00:00
                    [RevisionNumber] => 1
                    [IsAdd] => 1
                    [IsEdit] => 1
                    [IsDelete] => 1
                )

        )

)

我想在不使用foreach循环语句的情况下重新启动所有数据。

$data = array();
            foreach($modelData as $row){
                $data[] = $row[$model->name];
            }
            return $data;

我不想使用这个foreach循环。所以请建议我适当的解决方案。

5 个答案:

答案 0 :(得分:0)

JSON怎么样?你可以从数组中获取一个字符串。

  

http://www.php.net/manual/en/function.json-encode.php

答案 1 :(得分:0)

您可以使用array_map功能

做类似的事情:

$data = array_map(function($model){
return $model['object_type']['Name'];
},$modelData);

代码未经过测试,但它应该是这样的

根据你的var_dump对另一个解决方案的响应,尝试:

$data = array_map(function($model){
return $model['object_type']['Name']['text'];
},$modelData);

答案 2 :(得分:0)

我不确定这是否是你想要的,但是如果它是,我尝试做同样的事情,得到一个没有别名的数组:$array['first_name']而不是$array['User']['first_name']为了以JSON格式打印它,但不幸的是没有办法本机地这样做,所以我坚持使用foreach

答案 3 :(得分:0)

如果不是foreach,请尝试for,因为要迭代数组,您必须使用一些循环,最快的是for,如下所示

$data = array();
$size = sizeof($modelData);

for($i=0; $i<=$size; $i++)
{
    $data[] = $modelData[$i][$model->name];
}
return $data;

答案 4 :(得分:0)

尝试

  $final_array = array_map(
  function ($a) { 

    return  $a['object_types']['Name'];
  }, 
    $modelData
  );

 print_r($final_array);