Jump to content

how to replace empty values in an array


shaddf
Go to solution Solved by Barand,

Recommended Posts

i have this array:

$flavors = array('Japanese' => array('hot' => 'wasabi',
'salty' => 'soy sauce'),
'Chinese' => array('hot' => '',
'pepper-salty' => 'prickly ash'),'indian' => array('hot' => '',
'pepper-salty' => 'prickly ash'),'mexican' => array('hot' => 'soanzo',
'salty' => 'soy sauce'),
'russian' => array('hot' => '',
'pepper-salty' => 'prickly ash'),'ganda' => array('hot' => '',
'pepper-salty' => 'prickly ash'));

 

how can i loop through it  and replace the empty values with the value of the previously filled part of say japanese ' hot  for  the two arrays next to it and mexican' soanzo for the next two to it using for loop

 

 

Link to comment
Share on other sites

is this what you had in mind?

$prevkey = '';
foreach ($flavors as $k1 => $arr) {
    foreach ($arr as $k2 => $v ) {
        if ($v=='') {
            if (isset($flavors[$prevkey][$k2])) {
                $flavors[$k1][$k2] = $flavors[$prevkey][$k2];
            }
        }
        $prevkey = $k1;
    }
}
echo '<pre>',print_r($flavors, true),'</pre>';

gives

Array
(
    [Japanese] => Array
        (
            [hot] => wasabi
            [salty] => soy sauce
        )

    [Chinese] => Array
        (
            [hot] => wasabi
            [pepper-salty] => prickly ash
        )

    [indian] => Array
        (
            [hot] => wasabi
            [pepper-salty] => prickly ash
        )

    [mexican] => Array
        (
            [hot] => soanzo
            [salty] => soy sauce
        )

    [russian] => Array
        (
            [hot] => soanzo
            [pepper-salty] => prickly ash
        )

    [ganda] => Array
        (
            [hot] => soanzo
            [pepper-salty] => prickly ash
        )

)
Link to comment
Share on other sites

 

is this what you had in mind?

$prevkey = '';
foreach ($flavors as $k1 => $arr) {
    foreach ($arr as $k2 => $v ) {
        if ($v=='') {
            if (isset($flavors[$prevkey][$k2])) {
                $flavors[$k1][$k2] = $flavors[$prevkey][$k2];
            }
        }
        $prevkey = $k1;
    }
}
echo '<pre>',print_r($flavors, true),'</pre>';

gives

Array
(
    [Japanese] => Array
        (
            [hot] => wasabi
            [salty] => soy sauce
        )

    [Chinese] => Array
        (
            [hot] => wasabi
            [pepper-salty] => prickly ash
        )

    [indian] => Array
        (
            [hot] => wasabi
            [pepper-salty] => prickly ash
        )

    [mexican] => Array
        (
            [hot] => soanzo
            [salty] => soy sauce
        )

    [russian] => Array
        (
            [hot] => soanzo
            [pepper-salty] => prickly ash
        )

    [ganda] => Array
        (
            [hot] => soanzo
            [pepper-salty] => prickly ash
        )

)

$flavors[]=

 array('Japanese' =>  array('hot' => 'wasabi','salty' => 'soy sauce','sweety' => 'soy rdsauce','bitter' => 'pepper sauce'),

'Chinese' => array('hot' => '','salty' => '','sweety' => 'soy rdsauce_one','bitter' => 'pepper sauce3'),

'indian' => array('hot' => '','salty' => '','sweety' => '','bitter' => 'pepper sauce4'),

'mexican' => array('hot' => 'soanzo','salty' => 'soy sauce1','sweety' => 'soy sauceeur','bitter' => 'pepper sauce_sausage'),

'russian' => array('hot' => '','salty' => '','sweety' => '','bitter' => 'pepper sauce_pork'),'ganda' => array('hot' => '',

'salty' => '','sweety' => 'soy sauce_beef','bitter' => 'luwombo_chicken'));

 

can you make it work for such an array. such that the empty values are filled with the content of the previous full array closest to it. say for ganda, it shpul be filled with the mexican values corresponding to each empty value, this is true for russian too. And the same setting has to be applied to those next to the full japanese array.

 

the final array should be like this.keeping in mind that it can be bigger than this.

Array

(

    [Japanese] => Array

        (

            [hot] => wasabi

            [salty] => soy sauce

        [sweety] => soy rdsauce

        [bitter] => pepper sauce

        )

 

    [Chinese] => Array

        (

            [hot] => wasabi

            [salty] => soy sauce

        [sweety] => soy rdsauce_one

        [bitter] => pepper sauce3

        )

 

    [indian] => Array

        (

            [hot] => wasabi

            [salty] => soy sauce

        [sweety] => soy rdsauce

        [bitter] => pepper sauce4

        )

 

    [mexican] => Array

        (

            [hot] => soanzo

            [salty] => soy sauce1

        [sweety] => soy sauceeur

        [bitter] => pepper sauce_sausage

        )

 

    [russian] => Array

        (

            [hot] => soanzo

            [salty] => soy sauce1

        [sweety] => soy sauceeur

        [bitter] => pepper sauce_pork

 

        )

 

    [ganda] => Array

        (

            [hot] => soanzo

            [salty] => soy sauce1

        [sweety] => soy sauce_beef

        [bitter] => luwombo_chicken

 

        )

 

)

 

Link to comment
Share on other sites

  • Solution
$prevkey = '';
foreach ($flavors as $k1 => $arr) {
    foreach ($arr as $k2 => $v ) {
        if ($v=='') {
            if (isset($flavors[$prevkey][$k2])) {
                $flavors[$k1][$k2] = $flavors[$prevkey][$k2];
            }
        }
    }
    $prevkey = $k1;
}
echo '<pre>',print_r($flavors, true),'</pre>';

Sorry, I had $prevkey = $k1; in the wrong place.

Link to comment
Share on other sites

$prevkey = '';
foreach ($flavors as $k1 => $arr) {
    foreach ($arr as $k2 => $v ) {
        if ($v=='') {
            if (isset($flavors[$prevkey][$k2])) {
                $flavors[$k1][$k2] = $flavors[$prevkey][$k2];
            }
        }
    }
    $prevkey = $k1;
}
echo '<pre>',print_r($flavors, true),'</pre>';

Sorry, I had $prevkey = $k1; in the wrong place.

 

thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.