This question already has answers here:
How do I Sort a Multidimensional Array in PHP [duplicate]
(10 answers)
Closed 9 years ago.
I have a form where I'm creating a number of item arrays:
<input type="hidden" value="Full/Double Mattress" name="pickup1-dropoff1Items[1][0]">
<input type="text" name="pickup1-dropoff1Items[1][1]">
<input type="hidden" value="20" name="pickup1-dropoff1Items[1][2]">
<input type="hidden" value="FMat" name="pickup1-dropoff1Items[1][3]">
<input type="hidden" value="1" name="pickup1-dropoff1Items[1][4]">
so the structure is basically:
array(
array('title', quantity, price, 'shorthand', order),
array('title', quantity, price, 'shorthand', order)
)
etc...
I'm getting this information using PHP and sending it in an email. I can get one of these arrays like so:
$pickup1_dropoff1Items = $_POST['pickup1-dropoff1Items'];
I would like to sort the arrays in $pickup1_dropoff1Items by the 'order' number (i.e. index #4, i.e. $pickup1-dropoff1Items[i][4]) in each of those arrays.
Can this be done using PHP ksort()? Does anyone have any idea how to sort an array like this using PHP?
Thanks!
It's not tested but I think this will do what you need:
// first create a new array of just the order numbers
// in the same order as the original array
$orders_index = array();
foreach( $pickup1_dropoff1Items as $item ) {
$orders_index[] = $item[4];
}
// then use a sort of the orders array to sort the original
// array at the same time (without needing to look at the
// contents of the original)
array_multisort( $orders_index, $pickup1_dropoff1Items );
This is essentially example 1 here:
http://www.php.net/manual/en/function.array-multisort.php
but our $ar2 is an array of arrays instead of an array of single values. Also if you need more control over the sort you'll see examples of options you can use at that URL: just add them to the list of arguments for array_multisort.
For sorting complex arrays like this, you can use something like usort() which "sorts an array by values using a user-defined comparison function".
See the example on php.net for more information.
Related
This question already has answers here:
Using array_search for multi value search
(3 answers)
Closed 4 years ago.
I have a multi-dimensional array similar to this:
$arr1 = array(
0 => array("departmentID"=>1,"userID"=>"3000001"),
1 => array("departmentID"=>2,"userID"=>"3000002"),
2 => array("departmentID"=>3,"userID"=>"3000003")
);
I basically need to search the array to see if a specific key/value pair exists. For example, I need to know if department ID 2 with userID 3000002 is in the array.
I've tried this code:
$key = array_search('2', array_column($arr1, 'departmentID'));
echo ("The key is: ".$key);
This works fine but it's only a search on the department ID. I need to know if the departmentID value of 2 exists with the userID value of 3000002 and I can't quite figure it out.
Would be grateful for any help!
$key = array_search(array("departmentID"=>2,"userID"=>"3000002"), $arr1);
This question already has answers here:
array_push() with key value pair
(8 answers)
Closed 4 years ago.
I have created empty array and want to push all values and its key to the new created array but I am getting error array_push() expects at least 2 parameters I know array_push needs two parameter and here I am passing only one but what I ant is all keys and values to be pushed directly to array
// Here 'userid' is just text or can say sample key and
// Here $userid is getting from table so expected output
// 'userid'=>$userid
$temp = array();
array_push($temp['userid'] = $userid);
To push to an array using a key - value pair, you do not need to use array_push.
array_push expects the array, and the value (no key) that you're pushing.
To push to an array using a key - value pair simply do :
$temp['user_id'] = $userid;
To use array_push you must give it the original array (modified by reference) and the new entry (value) for it. In this case since you have a key as well, you'd need to merge your arrays:
$temp = array_merge($temp, ['userid' => $userid]);
However, you can use simple array syntax instead to achieve the same thing:
$temp = array();
$temp['userid'] = $userid;
This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 6 years ago.
suppose i have an array of arrays in PHP:
$array = [
['value'=>1, 'other_attribute'=>'whatever'],
['value'=>13, 'other_attribute'=>'whatever'],
['value'=>45, 'other_attribute'=>'whatever'],
['value'=>64, 'other_attribute'=>'whatever'],
];
how can i get a list containing just a specific attribute of each of the array's elements? in my case, if i wanted to get the list of 'value', the output should look like this:
[1, 13, 45, 64]
using the Laravel framework, it's easy to do this with query builder objects, simply like this: $array->lists('value');. is there a way to do this in plain PHP?
Sure, just build your own loop to do it:
$values = []; // the new array where we'll store the 'value' attributes
foreach ($array as $a) { // let's loop through the array you posted in your question
$values[] = $a['value']; // push the value onto the end of the array
}
This question already has answers here:
get name of a post variable [duplicate]
(3 answers)
Closed 7 years ago.
Is there a way to get the names of the form fields from $_POST ?
I know I can get the values with $_POST['foo'], but how do I get the list of names (not sure what they are called).
Any help is great.
$_POST is an associative array. Use array_keys to get the keys of the array.
$names = array_keys($_POST);
As simple as that
print_r($_POST);
print_r — Prints human-readable information about a variable
http://php.net/manual/en/function.print-r.php
An array consists of array key and value.
$array = array('color1' => 'red','color2' => 'blue', 'color3' => 'green');
to get the key values use
$keys = array_keys($array);
print_r($keys);
I know there are some similar questions but none of them solves my issue.
I have a simple form:
<form method="post">
Import data: <textarea type="text" name="import"></textarea>
<input type="submit">
</form>
Then I get data from the "import" field:
$current = my_data();
$import = $_POST['import'];
$merge = array_merge($current,$import);
The problem is, even if I paste:
array('foo' => 'bar')
I get:
Warning: array_merge(): Argument #2 is not an array in
(address)
on line (line)
I can't change the HTML markup and I have to paste arrays there. Any ideas how to fix it? I've been reading about serialize() but not sure if there's anything to serialize is array() is not array() for PHP. Why is that? Any solutions? Thanks a lot!
UPDATE
$current hold an array of options for my theme.
$merge is supposed to hold the same keys with different values (around 30-50 of them, not multidimensional but might be in the future), but of course users might add new ones so in order to ignore them I'm actually using:
$imported_options = array_merge($current_options , array_intersect_key($_POST["import"], $current_options ));
(simplified this one as it's just an example)
So after all I want to load an array from the form and update the other array with it.
PHP will not create arrays in $_GET/$_POST unless you tell it to:
Import data: <textarea type="text" name="import[]"></textarea>
^^---- need these
Without the [], PHP will treat any duplicate field names as strings to be overwritten. With [] in the name, PHP will treat them as new elements in an array.
You can do
$current['import'] = $import;
Or you can change your html this way:
<textarea type="text" name="myarray['import']"></textarea>
And in php:
$import = $_POST['myarray'];
The second argument is not an array.
$_POST['import'] = value received from the form.
With that said, try:
$current [] = $_POST['import'];
What are you trying to get from $_POST['import'] ?
you are using a textarea to get an array?
if it's just a single variable then use array_push
http://php.net/manual/es/function.array-push.php
for array_merge you need to have 2 arrays.