0

This is my code to display my associative array

$i=1;
echo '<pre>';
foreach($bendetails as $bd)
{
    $i=str_pad($i,'2','0',pad_left);
    echo $i.'. '.$bd['cd2'].'<br />';
    echo $i.'. '.$bd['bban'].'<br />';
    $i++;
}
echo '</pre>';

when i am using print_r($bendetails); it's output is fine

Array
(
    [0] => Array
        (
        [cd2] => A=Add/U=Update/D=Delete
        )

    [1] => Array
        (
            [bban] => Bank Account Number
        )

    [2] => Array
        (
            [cd2] => A=Add/U=Update/D=Delete
        )

    [3] => Array
        (
        [bban] => Bank Account Number
        )

    [4] => Array
        (
            [cd2] => A=Add/U=Update/D=Delete
        )

    [5] => Array
        (
          [bban] => Bank Account Number
        )
)

and when I use above mentioned foreach to display it, I don't know from where, it is showing the output like

01 A=Add/U=Update/D=Delete
01 
02 
02 Bank Account Number
03 A=Add/U=Update/D=Delete
03 
04 
04 Bank Account Number
05 A=Add/U=Update/D=Delete
05 
06 
06 Bank Account Number

Can anyone please tell is there anything missing? What is causing these extra spaces. I am new to PHP arrays. Thanks in advance.

3
  • 1
    What you got is the expected result, having the shown array structure. Commented Jun 25, 2014 at 8:44
  • @hek2mgl - but it is showing extra lines, and i didn't want them... if u have downvoted the question, remove the down vote. i already said, i am NEW... Commented Jun 25, 2014 at 8:51
  • This has nothing to do with being NEW. Just use your eyes. However, @SyamMohan has already the right answer for you. Commented Jun 25, 2014 at 8:54

3 Answers 3

1

Change your array format like this

Array
(
  [0] => Array
    (
        [cd2] => A=Add/U=Update/D=Delete
        [bban] => Bank Account Number
    )

  [1] => Array
    (
        [cd2] => A=Add/U=Update/D=Delete
        [bban] => Bank Account Number
    )

  [2] => Array
    (
        [cd2] => A=Add/U=Update/D=Delete
        [bban] => Bank Account Number
    )

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

1 Comment

and how do i do that?
0

Because your always printing both values, even if the keys do not exist. You should check that the key exists before printing:

foreach ($bendetails as $bd)
{
    $i=str_pad($i,'2','0', STR_PAD_LEFT);
    if (array_key_exists('cd2', $bd)){
        echo $i.'. '.$bd['cd2'].'<br />';
    }
    if (array_key_exists('bban', $bd)){
        echo $i.'. '.$bd['bban'].'<br />';
    }
    $i++;
}

1 Comment

Thanks @Christian P.. duration is one minute only??? i have accepted your answer..
0

The correct use of the str_pad() function involves using a PHP CONSTANT called STR_PAD_LEFT and not just an arbitrary name pad_left so the padding command should be

$i=str_pad($i, '2', '0', STR_PAD_LEFT);

Maybe this is causing the unexplained odd spacing? Especially as the default padding is STR_PAD_RIGHT which is probably what you are being given.

Comments

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.