bvconway Posted February 11, 2013 Share Posted February 11, 2013 Ref. p.164 "Creating Multidimensional Arrays" Wondering why I keep getting "Notice"s about "Array to string conversion". I think this is covered in the book but I can't seem to find the place right now (or, I'm being lazy). I'm using Notepad v. 6.3 with XAMPP v. 3.1.0.3.1.0 with PHP v.5.4.7 and testing in Firefox (latest). Here's the "error": -------------------------------------------------------------------------------------------------------------------------- Notice: Array to string conversion in C:\xampp\htdocs\test\multidimensional_arrays\books.php on line 49 PHP VQS: Array -------------------------------------------------------------------------------------------------------------------------- I think it's covered on p.64 Error Reporting and has something to do with E_NOTICE settings. But, the program runs OK (as per the output below). I'm just wondering why it reports this "notice", or if I'm doing something wrong. Is it really performing an array-string conversion? If anything, it should non-report that I'm doing a normal, regular array-value output, where the value happens to be a string. Should I turn this "notice" off? error_reporting(0); The only slightly-odd thing I noticed while coding (using Notepadd++ v.6.3) is it keeps reporting xml:lang="en" as an unrecognized tag and colors it differently as per NPP's coloring scheme, i.e. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> Lets paste something from the PHP Manual, just for fun: Note: Enabling E_NOTICE during development hassome benefits. For debugging purposes: NOTICE messages will warn youabout possible bugs in your code. For example, use of unassigned valuesis warned. It is extremely useful to find typos andto save time for debugging. NOTICE messages will warn you about bad style. For example, $arr[item] is better to be written as$arr['item'] since PHP tries to treat"item" as constant. If it is not a constant, PHP assumesit is a string index for the array. ------------------------------------------------------------------------------------------------------------------------- My output: PHP:Groceries and Arrays - groceries.php The third chapter of my first book is HTML Forms and PHP. The first chapter of my second book is Advanced PHP Techniques The third chapter of my third book is Advanced Database Concepts Notice: Array to string conversion in C:\xampp\htdocs\test\multidimensional_arrays\books.php on line 49 PHP VQS: Array Notice: Array to string conversion in C:\xampp\htdocs\test\multidimensional_arrays\books.php on line 49 PHP Advanced VQP: Array Notice: Array to string conversion in C:\xampp\htdocs\test\multidimensional_arrays\books.php on line 49 PHP and MySQL VQP: Array ---------------------------------------------------------------------------------------------------------------------------------------------- Here is the full code I used: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>PHP:Multidimensional Arrays</title></head><body><h2>PHP:Groceries and Arrays - groceries.php</h2><br /> <?php $phpvqs = array( 1 => 'Getting Started with PHP', 'Variables', 'HTML Forms and PHP', 'Using Numbers'); //uses no.s for keys and strings for values //no.1 above refers to chapter 1 $phpadv = array( 1 => 'Advanced PHP Techniques', 'Developing Web Applications', 'Advanced Database Concepts', 'Security Techniques'); $phpmysql = array( 1 => 'Introduction to PHP', 'Programming with PHP', 'Creating Dynamic Web Sites', 'Introduction to MySQL'); $books = array ( 'PHP VQS' => $phpvqs, 'PHP Advanced VQP' => $phpadv, 'PHP and MySQL VQP' => $phpmysql ); //uses strings for keys and arrays for values //print out the name of the third chapter of the PHP //Visual QuickStart Guide //print "{$books['PHP VQS'][2]}"; //above line is wrong - 3rd chap is at index 3 print "<p>The third chapter of my first book is <i>{$books['PHP VQS'][3]}.</i></p>"; print "<p>The first chapter of my second book is <i>{$books['PHP Advanced VQP'][1]}</i></p>"; print "<p>The third chapter of my third book is <i>{$books['PHP Advanced VQP'][3]}</i></p>"; foreach ($books as $key => $value) { print "<p>$key: $value</p>\n"; } //the $key variable gets each abbreviated book title //the $value variable contains each chapter-array //compare to //$list = array( //1 => 'apples', //2 => 'bananas', //3 => 'oranges' //); //arrays automatically begin their indexing at 0, unless otherwise specified. You can assign the index when using array() as above ?> <pre> <?php print_r ($books); var_dump($books); foreach ($books as $title => $chapters) { print "<p>$title"; foreach ($chapters as $number => $chapter) { print "<br />Chapter $number is $chapter"; } print "</p>"; } ?> </pre> </body></html><pre style="visibility:hidden">Comments:<!--Multidimensional arrays:- you use arrays as elements instead of stringsor numbers---------------------------------------------------------------------></pre> Link to comment Share on other sites More sharing options...
Edward Posted February 11, 2013 Share Posted February 11, 2013 What is the code on line 49? We should be able to help you if we know that. Link to comment Share on other sites More sharing options...
Larry Posted February 12, 2013 Share Posted February 12, 2013 It's actually more simple than you think and it's just what the error message says. You're treating an array as a string when you do this: print "<p>$key: $value</p>\n"; $value is an array there, but you're trying to print it like it's a string, and the result is Array. 2 Link to comment Share on other sites More sharing options...
OkComputer Posted April 7, 2013 Share Posted April 7, 2013 Hi, I was having similar issues with this as well, till of course I realised my error and what Larry was referring to. Anyhow, here's my code(Btw, all the code beforehand is the same as in the book including html info and opening and closing php tags): Code: foreach ($books as $key => $value) { echo "<pre>", print "<p>" . print_r($key) . ":" . print_r($value) . "</p>\n"; "</pre>"; } ------------------------------------------------------------------------------------------- Same as above, just shortened: foreach ($books as $key => $value) { echo "<pre>", print "<p>" . print_r($key) . ":" . print_r($value) . "</p>\n"; "</pre>"; } Output: PHP VQSArray( [1] => Getting Started with PHP [2] => Variables [3] => HTML Forms and PHP [4] => Using Numbers) 1:1 1 PHP Advanced VQPArray( [1] => Advanced PHP Techniques [2] => Developing Web Applications [3] => Advanced Database Concepts [4] => Security Techniques) 1:1 1 PHP and MySQL VQPArray( [1] => Introduction to PHP [2] => Programming with PHP [3] => Creating Dynamic Web Sites [4] => Introduction to MySQL) 1:1 1 Also did it without the <pre> tags: foreach ($books as $key => $value) { echo "<p>" . print_r($key) . ":" . print_r($value) . "</p>\n";} Output: PHP VQSArray ( [1] => Getting Started with PHP [2] => Variables [3] => HTML Forms and PHP [4] => Using Numbers ) 1:1 PHP Advanced VQPArray ( [1] => Advanced PHP Techniques [2] => Developing Web Applications [3] => Advanced Database Concepts [4] => Security Techniques ) 1:1 PHP and MySQL VQPArray ( [1] => Introduction to PHP [2] => Programming with PHP [3] => Creating Dynamic Web Sites [4] => Introduction to MySQL ) 1:1 Link to comment Share on other sites More sharing options...
Recommended Posts