简体   繁体   中英

PHP removing leading zeros

I am building an automated email system, as an extension for a membership system I have designed.

Take the following array:

array (size=5)
  '002858' => string '<p>These members have expired:</p><p>Adults: None</p><p>Children: Florence Carline</p><p>Disabled Members: None</p>' (length=115)
  '001974' => string '<p>These members have expired:</p><p>Adults: Kelly Hughes, Andrew Hughes</p><p>Children: Alfie Hughes</p><p>Disabled Members: None</p>' (length=134)
  '000033' => string '<p>These members have expired:</p><p>Adults: Kate Shirley</p><p>Children: Rolsin Shirley</p><p>Disabled Members: None</p>' (length=121)
  '000259' => string '<p>These members have expired:</p><p>Adults: Charmaine Walsh, Michael Baxter</p><p>Children: Liam Baxter, George Baxter</p><p>Disabled Members: None</p>' (length=152)
  '000000' => string '<p>These members are expiring in a month:</p><p>Adults: Edward Selby</p><p>Children: None</p><p>Disabled Members: None</p>' (length=122)

When I try to read the array back in a foreach loop (one iteration per email) I receive the following error:

Notice: Uninitialized string offset: 1974 in C:\wamp\www\subdomains\[host]\controllers\email.class.php on line 145

Notice this is the second one down in my array but without its '00'.

I have looked at this: php array behaving strangely with key value 07 & 08

So I checked (with is_int()) - The key is a string not int.

Can anyone shed some light on this for me?

EDIT:

Creation:

foreach($email_list_array['month'] as $key => $ticket) {

    $header[$key] =  "<p>Our records indicate that your pass (ticket number: $key) is expiring or has already expired.</p>";
    $header[$key] .= "<p>Please see the details below</p>";

    if(!isset($message[$key])) {
        $message[$key] = "<p>These members are expiring in a month:</p>";
    } else {
        $message[$key] .= "<p>These members are expiring in a month:</p>";
    }

    $message[$key] .= "<p>Adults: $adults</p>";
    $message[$key] .= "<p>Children: $children</p>";
    $message[$key] .= "<p>Disabled Members: $disabled</p>";

}

This is where I did the array dump

var_dump($message);

Test Output:

foreach($message as $key => $value) {

    $message_header = $header[$key];
    $message = $message[$key];

    echo $message_header;
    echo $message;
    echo "<br>";

}

Change the name of variable $message inside foreach:

$messageXXX = $message[$key];
echo $messageXXX;

I guess the array is accessed as integer not as string. So you should convert the integer to string.

Please note there is a difference between access by integer and access by string!

$arrayX = array( "A", "X" );
$arrayY = array( "0012"=> "A", "0001"=>"b" );


print_r( $arrayX );

print_r( $arrayY);

echo "array X ". $arrayX[1];  // returns "X"
echo "array Y".$arrayY["0012"];  // returns "A"
echo "array Y ".$arrayY[12];       // should also return "A"

The output is as follows

Array
(
    [0] => A
    [1] => X
)
Array
(
    [0012] => A
    [0001] => b
)
XA
Notice: Undefined offset: 12 in /test.php on line 14

In your case you also need to add the leading zeros with something like

$value = 12; // integer
$str = sprintf("%04d",$value);  // now $str = "0012"
echo "array Y ".$arrayY[ $str ];       // now returns "A"

Part of your problem is you are assigning a value to $message in your loop, but $message was your input variable. This is making PHP do what it is doing. It also appears that your are trying to grab the same keys from two different arrays. Is this correct?

Anyhow take a look at the following:

foreach($message as $key => $value) {

    $message_header = $header[$key];
    $messageText = $message[$key];

    echo $message_header;
    echo $messageText;
    echo "<br>";

}

This would hopefully show what changes you need in your test to stop that error from happening.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM