开发者

php foreach getting values from an array

开发者 https://www.devze.com 2022-12-28 00:34 出处:网络
I am having trouble accessing the values in an array,the array looks like this, Array ( [0] => Array

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
    开发者_如何学Go        [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?


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'];


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:

  • http://de3.php.net/manual/en/function.echo.php
  • http://de3.php.net/manual/en/function.print.php
  • http://de3.php.net/manual/en/language.types.array.php


I'm sure someone will post the answer in the next 0.3 seconds so I'll just try to help you with your methodology instead. Clearly you know how to use print_r (or var_dump). Have you tried using it on $rslt within your array? You seem to have difficulty understanding what $rslt is.


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

精彩评论

暂无评论...
验证码 换一张
取 消