Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Facing a weird situation with arrays.. I am using LinkedIn API to get profile info which returns data in two formats..

If user has just one educational item

educations=>education=>school-name
educations=>education=>date
...

If more than one education item

educations=>education=>0=>school-name
educations=>education=>0=>date
...
educations=>education=>1=>school-name
educations=>education=>1=>date
...

Now I am trying to make it consistent and convert

educations=>education=>school-name

to

educations=>education=>0=>school-name

But getting error in code that i believe should work

if(empty($educations['education'][0]['school-name']))
{
    $temp = array();
    $temp['education'][0]=$educations['education'];
    $educations = $temp;
}

This fails for "just one educational item", generates error on the first line for (isset,is_array and empty)

PHP Fatal error:  Cannot use string offset as an array in ...

print_r returns

[educations] => Array
    (
        [education] => Array
            (
                     [id] => 109142639
                     [school-name] => St. Fidelis College
                     [end-date] => Array
                         (
                             [year] => 2009
                         )

            )

    )
share|improve this question
 
Can you var_dump the content of $educations? –  Jefffrey Apr 8 at 16:35

2 Answers

You want:

if(array_key_exists('school-name',$educations['education']))
{
    $educations['education'] = array($educations['education']);
}
share|improve this answer

Usually you'd write the assignment like this:

$temp = array(
    "education" => array($educations['education'])
);

To avoid any issues with indexes. This might also fix yours.

If you're unsure about the contents of $educations['education'][0]['school-name'] you can simply check each part:

if(isset($educations['education'], $educations['education'][0], $educations['education'][0]['school-name']))

This works because isset doesn't behave like a normal function. It takes multiple arguments in a lazy manner.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.