1

I have 2 arrays, one array has default values which should be used if values from main array are empty or not set at all.
And i have set cookie called 'lang' with values 'ru' or 'lv' or 'en'. So if cookie is set to 'en', the php should use only data which has 'en' in the end of variable.

This i set before all:

$pTitleru = "111";
$pTitlelv = "";
$pTitleen = "";
$ogTitleru = '2222';
$ogTitlelv = '';
$ogTitleen = '';
$ogType = '333';
$ogDescrru = '4444';
$ogDescrlv = '';
$ogDescren = '';
$ogImgru = '555';
$ogImglv = '';
$ogImgen = '';

Here i get the cookie 'lang' value, which is 'en':

$cookielang = $_COOKIE[$lang]; // $cookielang = 'en'

Here is array with default values which should be used only if original values are not set or empty:

$ogDefMetas = array(
    '$pTitleru' => 'wwww', 
    '$pTitlelv' => 'eeee', 
    '$pTitleen' => 'rrrr', 
    '$ogTitleru' => 'tttt', 
    '$ogTitlelv' => 'yyyy', 
    '$ogTitleen' => 'uuuu', 
    '$ogType' => 'iiii', 
    '$ogDescrru' => 'oooo', 
    '$ogDescrlv' => 'pppp', 
    '$ogDescren' => 'ssss',
    '$ogImgru' => 'ddd', 
    '$ogImglv' => 'fff', 
    '$ogImgen' => 'ggg',  );

And here's the main array:

$ogMetas = array(
    '$pTitleru' => $ptitleru,
    '$pTitlelv' => $ptitlelv,
    '$pTitleen' => $ptitleen,
    '$ogTitleru' => $ogTitleru,
    '$ogTitlelv' => $ogTitlelv,
    '$ogTitleen' => $ogTitleen,
    '$ogType' => $ogType,
    '$ogDescrru' => $ogDescrru,
    '$ogDescrlv' => $ogDescrlv,
    '$ogDescren' => $ogDescren,
    '$ogImgru' => $ogImgru,
    '$ogImglv' => $ogImglv,
    '$ogImgen' => $ogImgen, );

I've managed to get values that are empty, but not sure what to do next :(

foreach ($ogMetas as $ogMeta => $ogMetaVal) {
    if (empty($ogMetaVal)) {
         echo "this '".$ogMeta."' is empty <br>";
    }
}

I Hope it makes sense and i'll really appreciate any help!

8
  • Unrelated, but there shouldn't be a comma at the end of each array Commented Jul 26, 2016 at 11:58
  • What do you mean with use? What should exactly happen with the elements, where nor value exists? Commented Jul 26, 2016 at 12:00
  • @JulianKuchlbauer Not sure about that, but so in the end i would be able to simply echo the end result to html like: <meta property="og:description" content="<?=$endOgDescr?>" /> Commented Jul 26, 2016 at 12:03
  • @MaximSlotov Are you just asking how to set the default values to the empty strings? Commented Jul 26, 2016 at 12:05
  • So should the main array be filled with the values of the def array, if they are empty? Your target is to have a full main array? Commented Jul 26, 2016 at 12:06

1 Answer 1

2

If I understood your question correctly, you're just asking how to populate the empty values of the main array with the corresponding values of the default array.


You can use the ?: (ternary) assignment operator as a more straightforward alternative to assign the default values right on assignment of the variable, instead of creating a loop to do it.

Here is the modified $ogMetas initialization:

$ogMetas = array(
    '$pTitleru' => $ptitleru ?: $ogDefMetas['$ptitleru'],
    '$pTitlelv' => $ptitlelv ?: $ogDefMetas['$ptitlelv'],
    '$pTitleen' => $ptitleen ?: $ogDefMetas['$ptitleen'],
    '$ogTitleru' => $ogTitleru ?: $ogDefMetas['$ogTitleru'],
    '$ogTitlelv' => $ogTitlelv ?: $ogDefMetas['$ogTitlelv'],
    '$ogTitleen' => $ogTitleen ?: $ogDefMetas['$ogTitleen'],
    '$ogType' => $ogType ?: $ogDefMetas['$ogType'],
    '$ogDescrru' => $ogDescrru ?: $ogDefMetas['$ogDescrru'],
    '$ogDescrlv' => $ogDescrlv ?: $ogDefMetas['$ogDescrlv'],
    '$ogDescren' => $ogDescren ?: $ogDefMetas['$ogDescren'],
    '$ogImgru' => $ogImgru ?: $ogDefMetas['$ogImgru'],
    '$ogImglv' => $ogImglv ?: $ogDefMetas['$ogImglv'],
    '$ogImgen' => $ogImgen ?: $ogDefMetas['$orImgen']
);

However, because the above looks a little bulky, if you wanted to go through with your loop, you just have to set the values:

foreach ($ogMetas as $ogMeta => $ogMetaVal) {
    if (empty($ogMetaVal)) {

         // this line changed
         $ogMetas[$ogMeta] = $ogDefMetas[$ogMeta];

    }
}

To answer the second part of your question, I'm not really sure what you mean by "use", but the below iterates over matching array elements only by way of matching a substr(). If you want to keep your method of filling in the default values, you can merge it with this loop (the loops are identical; just nest the if statements).

foreach($ogMetas as $ogMeta => $ogMetaVal) {
    if(substr($ogMeta, strlen($ogMeta)-2, 2) == $cookielang) {

        // code to deal with the array elements with the right $cookielang

    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

@MaximSlotov If my answer helped you, make sure to accept it (click the green checkmark next to my answer) to show the community it worked! Thanks!
Yes! Thank you! Everything works, except this one, here's the working line: (substr($ogMeta, strlen($ogMeta)-2) == $cookielang)
@MaximSlotov Oops I missed that. I'm glad it worked.

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.