Say I have a form containing an input field with
name="my_options[my_elements][1][name]"
and another with
name="my_options[my_elements][1][category]"
When I submit the form all is well and a new array containing name and category keys/values is added to the my_elements array in the my_options array in the DB.
I can also use name="my_options[my_elements][][name]"
to create a new array within my_options[my_elements]
However I obviously can't then use name="my_options[my_elements][][category]"
as this will put the category value into a further new array.
Is there a way of dynamically keeping track of the position of the new array created with the first [] so that values can be added at that particular level of the multidimensional array?
To clarify, I'm looking to dynamically add elements to the following array:
'my_options' =>
array (size ?)
0 =>
'my_elements' =>
array (size=2)
0 =>
array (size=2)
'name' => 'name 1'
'category' => 'cat 1'
1 =>
array (size=2)
'name' => 'name 2'
'category' => 'cat 2'
`
I wouldn't use array definitions in the html part of your application.
Stick with a input name, like name="category" and in your php file fetch the submitted data and organize it in an array the way you want it.
assuming you use POST as form method:
$category = $_POST['category'];
(don't forget to validate the input)
Related
I am writing a page that pulls images and image data out of a multidimensional array. I need to be able to click a button that calls a function to sort out the images by tags(IE tag_GlassDoor & tag_GlassWall) - basically to show only images that do or do not have that particular element (in this case im using 0 and 1 for yes and no), such as a glass door. I can currently make that array display the data, but I cant figure out how to sort the data by one of the array keys, or even really the syntax to pull a single value out at will.
$arrImages[] =
[
'img_sm'=>'image1.jpg',
'tag_GlassDoor'=>0,
'tag_GlassWall'=>1,
];
$arrImages[] =
[
'img_sm'=>'image2.jpg',
'tag_GlassDoor'=>1,
'tag_GlassWall'=>1,
];
Filtering is the answer, it can be used to filter one dimensional Arrays and multidimensional arrays.
the general implementation would be something like this:
$arr = array(
array(
'image' => "data",
'hasObject' => 1
),
array(
'image' => "data",
'hasObject' => 0
),
);
$finteredArray = array_filter($arr, function ($r) {
return (bool) $r['hasObject'];
});
print_r($finteredArray);
// it outputs:
// Array ( [0] => Array ( [image] => data [hasObject] => 1 ) )
I have an array that has been filled with default data as shown below
$arrivals = array(
"source" => "arrivals",
"data" => array(
0 => array("flight"=>"000","scheduled"=>"0000","city"=>"Geneva","airline"=>"UAL","gate"=>"A00","status"=>"1","remarks"=>"BOARDING"),
1 => array("flight"=>rand(1,2000),"scheduled"=>randomTime(),"city"=>"Baltimore","airline"=>randomAirline(),"gate"=>"A7","status"=>"0","remarks"=>"")
)
);
Now i want to create the same array with data from a table in a loop using the same identifiers such as 'city' but with variable names .
The other part is that the first part of 'data' array is a number which of course in a loop I can use a counter.
The problem is that the Array is created with the static value of ""source" => "arrivals" for which there is only one value for the array and then the arrays of 'data'.
I would like an easy way to set up an array dynamically with a number of records but with the one header of ""source" => "arrivals" and multiple entries for "data' i.e. one element per record I fetch from my table
Thank you
You can do this with a foreach loop in php after you have retrieved your data.
// Get the data from your table source
$data = get_some_data();
$arrivals = [
'source' => 'arrivals',
'data' => []
];
foreach ($data as $city) {
$arrivals['data'][] = [
'flight' => $city['flight'],
'scheduled'=> $city['scheduled'],
'city' => $city['city'],
// etc.
];
}
Alternatively, if you would like to assign the city name as the array key in arrivals, you can replace the first line inside the foreach loop with $arrivals['data'][$city['city']] (or whatever array item holds the city value).
I'm using PFBC (http://www.imavex.com/pfbc3.x-php5/) to generate form fields. I made some changes to the name of the elements: changed the class Textbox extends \PFBC\Element to class T_GENERAL extends \PFBC\Element
And I use
$form->addElement(new Element\T_GENERAL("", "cod_postal", array(
"required" => 1,
"placeholder" => "*Postcode:",
)));
to generate a form with 1 field. When I submit the form I have S_POST with this array:
array (size=1)
'cod_postal' => string '7000' (length=4)
and then transform this array to a string like this:
[["cod_postal","7000"]]
I need to add to the $_POST array one more value to be like this :
[["cod_postal","7000","0"]] --> $Label_1, $Value_1, $Type_1
I defined define("T_GENERAL", 0);
How I can do this ?
I have simple array from Wordpress manage_posts_columns filter, to change the columns in custom post type admin. The array looks like
$columns = array ('lastname' => 'Lastname', 'firstname' => 'Firstname', 'city' => 'City' );
and I'm adding ID column
$columns['id'] = 'ID';
I would need to move the id element to second position in the array. How can this be done?
Instead, you can use array_unshift to prepend elements onto an array, or array_push to add an element at the end of the array.
To reorder the associative array, you can use array_splice. A good example is here: http://uk.php.net/manual/en/function.array-splice.php#92651
I have a form in which i use two dimensional array for field names.The fields names are below
myform[message][]
myform[name][]
myform[add][]
it means there are three arrays.Every array has arrays inside it.when i var_dump my form after putting values and submitting it.I get the below structure of 2d array.
array
''message'' =>
array
0 => string 'adnan' (length=5)
1 => string 'khan' (length=4)
2 => string 'salman' (length=6)
''name'' =>
array
0 => string 'khan' (length=4)
1 => string 'kamran' (length=6)
2 => string 'khan' (length=4)
''add'' =>
array
0 => string 'asad' (length=4)
1 => string 'khan' (length=4)
2 => string 'abrar' (length=5)
As you can see the associative array.I want to store the values of message,name and add in a database table having three fields to store the values of message,name and add fields in a single query by using some loop like foreach.
when i use this code
foreach($_REQUEST['myform'] as $val)
foreach($val as $v)
{
echo $v;
}
I get all the values of the array.But i think i am unable to save it to database table
as all the values are in the variable $v.How to store message in a message field,name in name field and add in an add field in a table of a db.
please advice.Thanks
The looping is the easy part.
if (isset($_REQUEST['myform']))
{
foreach($_REQUEST['myform'] as $key=>$value)
{
// DO SOMETHING
}
}
The hard part is knowing what you want to do. You say put it in a database but you don't give any real info about what or where. Just make sure you carefully escape any user input before storing it, or better yet use prepared queries.