Re: brackets []

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Craig Hoffman wrote:
Quick question...
I am hoping some could explain this to me.  I have an array like this:

$completed = array($value1, $value2, $value3);

where the values are sent via a form.
<input type="checkbox" name="completed[]" value="value1">

when I do print_ r() to see what's in the array I get this:
Array (  [0] => [1] => [2] => )

An empty array. But when I do this:
No, an array with 3 keys with empty/null values.

$completed[] = array($value1, $value2, $value3);
The [] operator is for adding items to the end of an array (appending).

Array ( [0] => 1 [1] => 1 [2] => Done [3] => 2 [4] => 1 [5] => 3 [6] => 1 [7] => 4 [8] => 1 [9] => Array ( [0] => [1] => [2] => ) )

I get a multidimensional array with keys and values. How can I get this array to be one dimensional?
array_merge() is a function that will do what you're trying there - be warned that it changed to error on non-arrays in php5, so for forward compatibility it would be wise to either instantiate all values you pass to it - or test if they're arrays and not do it if null/etc.

*However* - if you just want to add those items to the end of the array, you can also just do $completed[] = $value1; (repeat) - because I'm not sure you know exactly what format you're going for.

When you post the form, $_POST['completed'] holds an array with values for each box that was checked. You shouldn't be populating it yourself on the backend like that (if that's actual code and not just a description of how it should look) - and what you print looks particularly concerning as there are multiple keys that resolve to the same value (1's) - a checkbox only passes its value if checked, so you've got multiple checkboxes with the same value, or you're overworking yourself.

Basically - it's hard to diagnose your code when you've clearly cut and paste bits and pieces between. We don't need to see your super secret new method of sorting data, but we do need to see all code related to the question at hand. It looks to me like you're over-munging your data trying to get something out of it formatted just so, instead of just reading the data you need from where it is.

Cheers,
--
- Martin Norland, Database / Web Developer, International Outreach x3257
The opinion(s) contained within this email do not necessarily represent those of St. Jude Children's Research Hospital.


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux