Hi there, I couldn't find a solution to this small problem so posting it here.

I have some hardcoded values I want to send back to the browser. When I have this code:

...
$cr1 = '{"date_added":"8/3","type":"1","contact":"John Smith"}';
$cr2 = '{"date_added":"8/7","type":"2","contact":"Jane Smith"}';
$company_reqs[0] = json_decode($cr1,1);
$company_reqs[1] = json_decode($cr2,1);
$company_data['reqs'] = $company_reqs;
...
$results['company'] = $company_data;
echo json_encode($results);

the requirements for company get passed correctly doing an Ajax call. But if I want to have requirements for multiple companies, like these:

...
$cr1 = '{"date_added":"8/3","type":"1","contact":"John Smith"}';
$cr2 = '{"date_added":"8/7","type":"2","contact":"Jane Smith"}';
$company_reqs['100'][0] = json_decode($cr1,1);
$company_reqs['100'][1] = json_decode($cr2,1);
$company_data['reqs'] = $company_reqs[$company_id];
...
$results['company'] = $company_data;
echo json_encode($results);

the requirements are null. I tried sending it directly to the browser, and the requirements are not null to my surprise! I get this:


{"company":{"company_id":"109","reqs":[{"date_added":"8\/3","type":"1","contact":"John Smith"},{"date_added":"8\/7","type":"2","contact":"Jane Smith"}]},"contact":{...}}


Thanks in advance!

Member Avatar for diafol

Multiples mean that the data is in an array. If you use

print_r($company_reqs['100'][1]);

you get the array structure. Am I missing something here?

Multiples mean that the data is in an array. If you use

print_r($company_reqs['100'][1]);

you get the array structure. Am I missing something here?

That is correct. In the first case the requirements are in an array, in the second case they are stored in the correspondent company's requirements. I am just adding another nesting level, sort of saying? why wouldn't it work on the second case?

Member Avatar for diafol

I have to be honest, I don't understand what you're trying to do, or which bit doesn't work for you.

{"company":{"company_id":"109","reqs":[{"date_added":"8\/3","type":"1","contact":"John Smith"},{"date_added":"8\/7","type":"2","contact":"Jane Smith"}]},[B]"contact":{...}}[/B]

This doesn't really make much sense to me. Where does this extra contact come in?

Array structure:

company (ARRAY OF or SINGLE)
 id (SINGLE): value     reqs (ARRAY OF or SINGLE): date_added | type | contact

Using 'reqs' as an array even if there is only one entry will probably be the easiest method.

OK I admit I'm not being clear enough, but yes, that's exactly what I'm trying to do with requirements ('reqs'). First I had an array for reqs with just generic requirements for any company:

$company_reqs[0] = json_decode('{"date_added":"8/3","type":"1","contact":"John Smith"}',1);
$company_reqs[1] = json_decode('{"date_added":"8/7","type":"2","contact":"Jane Smith"}',1);
$company_results['reqs'] = $company_reqs;

When I include $company_reqs in my result array for a company, the passed JSON in the Ajax call looks good. Obviously the idea is to have requirements for each company. However, if I do this:

$company_reqs['100'][0] = json_decode('{"date_added":"8/3","type":"1","contact":"John Smith"}',1);
$company_reqs['100'][1] = json_decode('{"date_added":"8/7","type":"2","contact":"Jane Smith"}',1);
$company_results['reqs'] = $company_reqs[$company_id];

the 'reqs' array in $company_results is null. I tried just printing this second scenario to the screen instead of doing an Ajax call, and I DO see the requirements.

I think I'm repeating my first post, but hopefully it's clearer this time.

Thx!

Member Avatar for diafol

In the (second scenario?):

$company_results['reqs'] = $company_reqs[$company_id];

This will return an array, because you have array[0] and array[1]

So $company_results[0] should give you $company_reqs[0]

However, you have used a string key for $company_reqs i.e. '100', but are using a number-based counter $company_reqs[100], i.e. 100 in the final line:

$company_results['reqs'] = $company_reqs[$company_id];

I don't know whether this would make a difference.

Holy crap it was something really stupid, and it wasn't the string key, it was the actual key I was using.. that's what happens when you hard code, my bad!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.