0

I have this PHP that creates an array of jobs as $jobs. However I'd like the keys to be [ref] rather than 0, 1, 2 etc

$xml_feed = new DOMDocument();
$xml_feed->load('http://jobs.recruitsosimple.com/job/762/xml');
$jobs = array();
foreach ($xml_feed->getElementsByTagName('job') as $node) {
    $item = array ( 
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'ref' => $node->getElementsByTagName('referencenumber')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('url')->item(0)->nodeValue,
        );
    array_push($jobs, $item);
}

The array created is:

Array
(
    [0] => Array
        (
            [title] => Theatre Nurse
            [ref] => MT29
            [link] => http://jobs.com/job/1192/29
        )

    [1] => Array
        (
            [title] => Ward Nurse
            [ref] => MT39
            [link] => http://jobs.com/1192/762/39
        )

)

But I would like the keys to be

Array
(
    [MT29] => Array
        (
            [title] => Theatre Nurse
            [ref] => MT29
            [link] => http://jobs.com/job/1192/29
        )

    [MT39] => Array
        (
            [title] => Ward Nurse
            [ref] => MT39
            [link] => http://jobs.com/1192/762/39
        )

)

How can I do this?

1
  • 1
    just create your enties like that: $jobs[$ref] = $item; instead of the arraypush Commented Nov 5, 2016 at 14:26

1 Answer 1

1

Change this:

array_push($jobs, $item);

to this:

$jobs[$item['ref']] = $item;
Sign up to request clarification or add additional context in comments.

3 Comments

@Jeff OK, thanks, I'll keep that in mind. I haven't really used StackOverflow so it's good to know. I just noticed that you also answered the question but you put it as a comment rather than an answer. Why?
because I didn't have the time for a real (good) answer. Writing it as a comment was easier & quicker.
Thanks so much. That also helped me to learn that array_push($jobs, $item); does the same as $jobs[] = $item;

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.