0
Array
(
    [0] => Job
    [1] => 
    [2] => Array
        (
            [0] => stdClass Object
                (
                    [AppDataId] => 16368
                    [JobTitle] => Sigma Six Black Belt/Lean Administration Consultant
                    [Abstract] => Sigma Six Black Belt/Lean Administration Consultan ... - open
                )

            [1] => stdClass Object
                (
                    [AppDataId] => 16367
                    [JobTitle] => General Manager (Power Generation)
                    [Abstract] => General Manager (Power Generation) - Botswana
                )

            [2] => stdClass Object
                (
                    [AppDataId] => 16366
                    [JobTitle] => Resident  Engineer - Mpumalanga
                    [Abstract] => Resident  Engineer - Mpumalanga - Mpumalanga
                )

        )

    [3] => 
)

I need to get this into a PHP array and list all the JobTitle's or any other field

I've done this but I cannot figure out how to access only JobTitle firstly, and then secondly list all of them. $json_url is the actual url of the json file that outputs the info above.

$json1 = file_get_contents($json_url);
$array = json_decode($json1);
echo "<pre>";
print_r($array);
echo "</pre>";

$strJob=array();
foreach ($array as $value) { 
    $strJob[2][1]=$value->JobTitle;
}
echo '<br/>';
print_r($strJob);
echo '<br/>';
4
  • $strJob[2][1]=$value->JobTitle; - now how is that supposed to make any sense? The only entry you are creating in your array $strJob is the one under the keys [2][1], and you are overwriting that one in every loop iteration. Commented Aug 22, 2017 at 13:41
  • You want to loop over $array[2] to begin with, because only that contains the sub-structure of objects to loop over. And then you simply add the job title as a new entry into your array, $strJob[]=$value->JobTitle; Commented Aug 22, 2017 at 13:43
  • How would I loop over only $array[2] ? Commented Aug 22, 2017 at 13:49
  • Same way as you are trying to loop over $array right now …? Commented Aug 22, 2017 at 14:03

1 Answer 1

1

You should use below syntax:

if(isset($array[2])) {
  foreach ($array[2] as $value) {
     echo 'JobTitle ->' . $value->JobTitle . '<br/>';
     echo 'AppDataId ->' . $value->AppDataId. '<br/>';
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that is what I was looking for.

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.