1

$array is this.First two index has null value.I want this null to be stored in array of $value. how to do this

Array
    (
        [ew-language__en] => 
        [ew-language__en__0__phrase__locale] => 
        [ew-language__en__1__phrase__use_system_locale] => 1
        [ew-language__en__2__phrase__decimal_point] => .
        [ew-language__en__3__phrase__thousands_sep] => ,
        [ew-language__en__4__phrase__mon_decimal_point] => .
        [ew-language__en__5__phrase__mon_thousands_sep] => ,
        [ew-language__en__6__phrase__currency_symbol] => $
        [ew-language__en__7__phrase__positive_sign] => 
        [ew-language__en__8__phrase__negative_sign] => -
        [ew-language__en__9__phrase__frac_digits] => 2
        [ew-language__en__10__phrase__p_cs_precedes] => 1
        [ew-language__en__11__phrase__p_sep_by_space] => 0
        [ew-language__en__12__phrase__n_cs_precedes] => 1
        [ew-language__en__13__phrase__n_sep_by_space] => 0
        [ew-language__en__14__phrase__p_sign_posn] => 3
        [ew-language__en__15__phrase__n_sign_posn] => 3
        [ew-language__en__16__phrase__time_zone] => US/Pacific
        [submit] => SAVE VALUE INTO XML FILE
    )

foreach ($array as $key => $value)
                 {//some code}

i want this $value to include null values also

3
  • simply you can do like [ew-language__en__0__phrase__locale] => null, Commented May 25, 2015 at 5:50
  • this array is getting values dynamically from xml file where value=" " it displays like this i can't do it like this. Commented May 25, 2015 at 5:52
  • Then you can use xml property to set null value in xml and then you can bind here , like <PropName xsi:nil="true"> Reference here : docs.intersystems.com/ens20121/csp/docbook/… Commented May 25, 2015 at 5:55

3 Answers 3

3

Normally, like this:

Array(
    "[ew-language__en]" => null
)

Now, you have just mentioned that you are getting these values dynamically from xml file where value=" ". You can then do:

foreach ($array as $key => $value) {
    if ($value == "" || ctype_space($value) ) {
        $array[$key] = null;
    }
}

If the array[key] is empty or only has whitespace, it will be converted to null.

Ref: ctype_space

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

3 Comments

this array is getting values dynamically from xml file where value=" " it displays like this i can't do it like this.
Given this new information, Helping Hands has good solution for XML
@Rozeena Here is the exact function you can use
1

Use a loop then -

foreach ($array as $key => $value)
{
    if(check_condition_to_set_null) $array[$key] = null;
}

Comments

1

If you're puling files dynamically from an XML file, Drake's answer won't work as it treats the value of the key as the string literal "null". Instead, what you can do, is create a string literal then replace it with a null when you load the array.

In your XML:

<ew-language__en__0__phrase__locale>NULL</ew-language__en__0__phrase__locale>

In your PHP, when you load the array:

foreach ($array as $key => $value) {
if ($array[$key] == "NULL") {
$array[$key] = null;
}
}

2 Comments

See, we didn't know this was even related to XML until the comments ;)
@Drakes Yep, nothing against you. Yours is the simplest solution :)

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.