laith jasser Posted April 8, 2013 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 ????????????????????????????????? Link to comment Share on other sites More sharing options...
Larry Posted April 8, 2013 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. Link to comment Share on other sites More sharing options...
Antonio Conte Posted April 8, 2013 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); Link to comment Share on other sites More sharing options...
Larry Posted April 8, 2013 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 Link to comment Share on other sites More sharing options...
HartleySan Posted April 8, 2013 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. Link to comment Share on other sites More sharing options...
Antonio Conte Posted April 8, 2013 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. Link to comment Share on other sites More sharing options...
Recommended Posts