append to multidimensional array?

How do I append a record to a multidimensional array, $array(id, para,co,add).

The following produces an error when I try to read the array with a foreach ( $array as $key=>$value):


  $num=count($array) ;
      if($num>=1)
        { $id=$array[$num-1]['id'] +1  ;
          $array[]=array('id' => $id, 'para'=>'con',  'co'=>'a', 'add'=>1);    
        }

pillmillAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
An error? What is the error message?
0
invsman249Commented:
It seems to work for me ok , see my code below and what it printed out

 
<?php
$array = array("blah","blah2","blah3");

$num = count($array);
foreach ( $array as $key=>$value)
{
	if($num >= 1)
	{ 
		$id=$array[$num-1]['id'] +1  ;
		$array[]=array('id' => $id, 'para'=>'con',  'co'=>'a', 'add'=>1);    
	}
}

print "<pre>";
print_r($array);
print "</pre>";
?>

Open in new window


 
Array
(
    [0] => blah
    [1] => blah2
    [2] => blah3
    [3] => Array
        (
            [id] => 1
            [para] => con
            [co] => a
            [add] => 1
        )

    [4] => Array
        (
            [id] => 2
            [para] => con
            [co] => a
            [add] => 1
        )

    [5] => Array
        (
            [id] => 3
            [para] => con
            [co] => a
            [add] => 1
        )

)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
invsman249Commented:
maybe you can give us some more info like the full array you have and where the records are going etc.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Wireless Networking

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.