0

I am having trouble accessing the values in an array, the array looks like this,

Array
(
    [0] => Array
        (
            [id] => 1661
            [code] => 849651318
            [job_status] => 4
            [looking_for] => Lorem ipsum
            [keywords_education] => Derby University
            [sector_id_csv] => 10,21,9,22,26
            [last_job_title_1] => Programmer
            [last_job_employer_1] => HBOS
            [city] => Bury
            [expected_salary_level] => LEVEL_2
            [education_level] => COLLEGE
            [job_looking_for] => 
            [is_contract] => Y
            [is_permanent] => N
            [is_temporary] => Y
        )

)
Array
(
    [0] => Array
        (
            [id] => 402
            [code] => 849650059
            [job_status] => 3
            [looking_for] => Lorem ipsum
            [keywords_education] => Paris College
            [sector_id_csv] => 27,22,19,21,12
            [last_job_title_1] => Programmer
            [last_job_employer_1] => HSBC
            [city] => Bury
            [expected_salary_level] => LEVEL_2
            [education_level] => COLLEGE
            [job_looking_for] => 
            [is_contract] => N
            [is_permanent] => Y
            [is_temporary] => Y
        )

)
Array
(
    [0] => Array
        (
            [id] => 1653
            [code] => 849651310
            [job_status] => 3
            [looking_for] => Lorem ipsum
            [keywords_education] => Crewe University
            [sector_id_csv] => 27,15,19,21,24
            [last_job_title_1] => Programmer
            [last_job_employer_1] => ICI
            [city] => Bury
            [expected_salary_level] => LEVEL_2
            [education_level] => UNIVERSITY
            [job_looking_for] => 
            [is_contract] => N
            [is_permanent] => Y
            [is_temporary] => Y
        )

)

I am trying to get the values out, I have tried doing the following,

foreach ($result as $rslt) {
    echo $rslt->id;
}

I have also tried,

foreach ($result as $rslt) {
    $rslt['id'];
}

But none of this works, I dont know why, can anyone help?

3 Answers 3

5

To point out a few things and hopefully clarify any confusion, in your first example:

foreach ($result as $rslt) {
    echo $rslt->id;
}

The arrow operator (->) is misused. It's commonly used to invoke a method on a class object, in your case $rslt would be a class object and id would be a method, which isn't the case.

In your second example,

foreach ($result as $rslt) {
    $rslt['id'];
}

You almost hit the nail on the head, but you had forgot to call echo or print to output the value onto the screen.

Also, your $result array has a sub-array at index 0, so that would need to change

$rslt['id'];

to, along with the echo or print statement.

echo $rslt[0]['id'];
Sign up to request clarification or add additional context in comments.

Comments

1

The second is correct, but you're missing an echo or print

echo $rslt['id'];

From the example code you give it's not clear what $result is.
If $result encompasses all the listed arrays, you will have to do

foreach($result as $rslt) {
    echo $rslt[0]['id'];
}

Further reference:

Comments

0
foreach($result as $rslt) {
    echo $rslt[0]['id'];
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.