DssTrainer Posted April 1, 2009 Share Posted April 1, 2009 This one is kinda tricky... and tho I'm decent with php, arrays can often blow me away.... I have an array of data pulled from a database: $res : array = 0: array = unit_id: string = "1" unit_name: string = "Apple" language_id: string = "1" 1: array = unit_id: string = "2" unit_name: string = "Banana" language_id: string = "1" 2: array = unit_id: string = "2" unit_name: string = "plátano" language_id: string = "2" language_id 1 = English language_id 2 = Spanish If I want English Items, I want to return: $res : array = 0: array = unit_id: string = "1" unit_name: string = "Apple" <== English language_id: string = "1" 1: array = unit_id: string = "2" unit_name: string = "Banana" <== English language_id: string = "1" If I want Spanish Items, I want all the spanish versions of the record as defined by unit_id and language_id. But if there is no spanish version for the id, I want it to default to the english version. So i should see: $res : array = 0: array = unit_id: string = "1" unit_name: string = "Apple" <== English language_id: string = "1" 2: array = unit_id: string = "2" unit_name: string = "plátano" <== Spanish language_id: string = "2" How can I do this? I'm trying something like: function getLang($res, $curlang='1') { foreach ($res as $r) { if ($r['language_id'] == $curlang) { $res2 = array($r); } else { ?????? } } return $res2; } But I'm not sure how to associate first the unit_ids, then get the language ids because it won't always be "unit_id".. it could "part_id" or "model_id" or any other "xxxx_id". However, it WILL always be "language_id" Link to comment https://forums.phpfreaks.com/topic/152092-array-help-get-value-in-one-language-if-it-doesnt-exist-in-the-other/ Share on other sites More sharing options...
DssTrainer Posted April 1, 2009 Author Share Posted April 1, 2009 actually the function that I'm trying is wrong above.. It should be: function getLang($res, $curlang='1') { $res2 = array(); foreach ($res as $r) { if ($r['language_id'] == $curlang) { res2[] = $r; } else { ?????? } } return $res2; } But its still incomplete. Link to comment https://forums.phpfreaks.com/topic/152092-array-help-get-value-in-one-language-if-it-doesnt-exist-in-the-other/#findComment-798835 Share on other sites More sharing options...
DssTrainer Posted April 9, 2009 Author Share Posted April 9, 2009 still no ideas for this one? Link to comment https://forums.phpfreaks.com/topic/152092-array-help-get-value-in-one-language-if-it-doesnt-exist-in-the-other/#findComment-805865 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.