문제

I fetch data of particular table by stored procedure ,demo code is

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
                )

        )

)

I want to retrun all data without foreach looping statement.

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

I didnt want to use this foreach loop. so please suggest me appropriate solution.

도움이 되었습니까?

해결책

You can use array_map function

do something like :

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

the code is not tested, but it should be something like this

Based on what your var_dump response was to the other solution, try :

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

다른 팁

How about JSON? You can get a string from array.

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

I'm not sure if this is what you wanted but in case it is, I tried doing the same thing, to get an array without the alias like: $array['first_name'] instead of $array['User']['first_name'] in order to print it in JSON format, but unfortunatelly there is no way to natively do that, so I sticked to the foreach

If not foreach, then try for because to iterate arrays you have to use some loops and the fastest is for as below

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

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

try

  $final_array = array_map(
  function ($a) { 

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

 print_r($final_array);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top