Search code examples
phparraysxmlsortingsimplexml

SimpleXML expecting array error PHP


ok below is my code

<?php
// Last 10 Jobs
function last10IT(){
$xml = simplexml_load_file('http://www.cv-library.co.uk/cgi-bin/feed.xml?affid=101899');


$new_array = array();
//$limit = 5;
//$c = 0;
foreach ($xml->jobs->job as $job) {
//    if ($limit == $c) {
//        break;
//    }
    $jobref = $job->jobref;
    $title = $job->title;
    $date = $job->date;
    $new_array[$jobref.$date] = array(
        'jobref' => $jobref,
        'date' => $date,
        'title' => $title,
        'salary' => $job->salary,
        'location' => $job->location,
    );
}
}
ksort($new_array);
$showl = 10;
$n = 0;
foreach ($new_array as $date => $listing) {

    print $listing['title'] . PHP_EOL;

}
?>

All I want it to do is filter by category & display a max of 10 results for example IT so is there a way I can pass the category value into the function that I want it to filter by instead of having to replicate for each category

All I get is :

Warning: ksort() expects parameter 1 to be array, null given in C:\wamp\www\RECRUITMENTFAIR\functions.php on line 28

Please help guys

It something SO simple causing this error but its driving me mad because I just cannot see it


Solution

  • it is relative simple: you try to use the variable $new_array out of scope: it is defined within your last10IT() function, but the function ends after the first foreach.

    you should either return the array and call the function to get the array or move the part with the ksort and the printing into the function, depending on your needs.