0

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 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

5 Comments

Hi again Orangepill, this is a follow up question though, nearly the same question as above, I have managed to solve the initial problem, now I am producing this kind of array: Array ( [0] => Array ( [0] => 11 [1] => 123 [2] => 232.22 ) [1] => Array ( [0] => 1 [1] => 2 [2] => 3 ) ) I tried the method $validate_field[0]->0; but it doesnt work. thanks again.
now it is an array of arrays and you can use the [$x][$y] notation. The tell here is in the original question the stdClass Object in the var dump
hm.. I seem to have solve the question, I can access the array using $validate_field[0][0]. That is weird though, how come I can access and get the value from that array using $array[$x][$y] compare to my first question that must use $validate_field[0]->MFID?
the first problem was an array or objects, the follow up was an array of arrays.
how can I determine whether an array of arrays or if it is an array of objects? so php treats these kind of data seperately? interesting..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.