laith jasser 0 Posted April 8, 2013 Report Share Posted April 8, 2013 $array2=array('name'=>array('laith','raad','hamid'),'age'=>array(20,30,40),'gender'=>'male'); print_r($array2); echo $c2=count($array2); foreach($array2 as $k=>$v) { if($array2 < $c2) { foreach($array2 as $k=>$v) { echo $k . ' ' . $v . "<br/>"; } } echo $k . ' ' . $v . "<br/>"; } // i have a problem //NOTICE: array to string conversion on the last line // i need solution ????????????????????????????????? Quote Link to post Share on other sites
Larry 433 Posted April 8, 2013 Report Share Posted April 8, 2013 A bigger problem is that your code kind of doesn't make any sense. Initial thoughts... - Your $array2 design isn't appropriate. You should create each person as a separate subarray, with "name", "age", and "gender" indexes for each. - You echo the assignment of count() to $c2. This is not the same as printing the value of $c2. - count() only gives you the number of items in the main array, so it'll be 3 in your situation. If you add two more names and ages (the way you have it), the count will still be 3. - Your IF conditional checks of a multidimensional array is less than the count of that array. This doesn't work or make sense. - You do two foreach loops on the same array, which is unnecessary and buggy. - You print out $k and $v twice, which is unnecessary and buggy. I gather you're just learning, but the NOTICE is the least of the problems. You need to rethink the logic of what you're trying to do here. Quote Link to post Share on other sites
Antonio Conte 426 Posted April 8, 2013 Report Share Posted April 8, 2013 Begin with this: $persons = array( array("name" => "Laith", "age" => 20, "gender" => "male"), // person 1 // Next person here... ); print_r($persons); Quote Link to post Share on other sites
Larry 433 Posted April 8, 2013 Report Share Posted April 8, 2013 I think this would be a better start, wouldn't you, Antonio? $persons = array( array("name" => "Laith", "age" => 20, "gender" => "male"), // person 1 array("name" => "Raad", "age" => 30, "gender" => "male"), // person 2 array("name" => "Hamid", "age" => 40, "gender" => "male"), // person 3 ); 1 Quote Link to post Share on other sites
HartleySan 826 Posted April 8, 2013 Report Share Posted April 8, 2013 Larry, you're missing ending quotes after each of the "gender" indexes. Also, your third array doesn't need a comma at the end. Quote Link to post Share on other sites
Antonio Conte 426 Posted April 8, 2013 Report Share Posted April 8, 2013 Yeah, I do Larry. I guess Larry copy-pasted my code, so I'm at fault here, Jon. Haha. I really need a cup of coffee before my brain functions properly. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.