dongqiang2024 2013-06-17 02:19
浏览 18
已采纳

PhP多维数组提取

I am not really familiar on how php handles array, in .NET I can access array using this method

array[x][y];

My question is:

I am retrieving records from the database and returning it to the $res_merchant_field

$res_merchant_field = $this->CI->merchantfield_model->merchantfield_list( $str_where );

and $res_merchant_field will be populated with this record:

Array
(
    [0] => stdClass Object
        (
            [MFID] => 1
            [MFName] => Bill No
            [FTID] => 1
            [DTID] => 1
            [MFRequired] => 1
            [MFDefaultValue] => 
            [MFDueDate] => 0
            [MFToBePaid] => 0
            [MFMaxLength] => 12
            [MFOrderNo] => 1
            [MFStatus] => 1
        )

    [1] => stdClass Object
        (
            [MFID] => 2
            [MFName] => Gallons Consumed
            [FTID] => 1
            [DTID] => 2
            [MFRequired] => 1
            [MFDefaultValue] => 
            [MFDueDate] => 0
            [MFToBePaid] => 0
            [MFMaxLength] => 5
            [MFOrderNo] => 2
            [MFStatus] => 1
        )

    [2] => stdClass Object
        (
            [MFID] => 3
            [MFName] => Amount Due
            [FTID] => 3
            [DTID] => 1
            [MFRequired] => 1
            [MFDefaultValue] => 
            [MFDueDate] => 0
            [MFToBePaid] => 1
            [MFMaxLength] => 15
            [MFOrderNo] => 3
            [MFStatus] => 1
        )

)

How can I access and fetch the record from that array with this condition:

  1. it will look through all the array find specific index, lets say index 0 which is MFID,
  2. after getting the MFID and comparing it with another variable, if it is true,
  3. it will get the DTID for that array MFID.

example:

get MFID = 1, the DTID will be 1, if I get the MFID = 3, the DTID will be 1.

or how can I access the array like $array[x][y]?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • douhunkuang8955 2013-06-17 02:26
    关注

    The problem is that the second level is not an array but instead an object, to access a property you will have to use this format.

     $array[$x]->$y;
    

    Unfortunately you cannot access a property by an index do o get the MFID of the 0th Item you will need to say

     $array[0]->MFID;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效