Jump to content

Array help - Get value in one language if it doesn't exist in the other.


DssTrainer

Recommended Posts

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"

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.

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.