I have two arrays something like this
1) array(3) { [0]=> string(3) "max" [1]=> string(3) "min" [2]=> string(3) "med" }
2) array(4) { [0]=> string(3) "max" [1]=> string(3) "min" [2]=> string(4) "other" [3]=> string(3) "med" }
now i want to merge and delete the double entries of the two arrays, important thing is here to keep the order of the first array in the final array (max,min,med -> from first array, and then all others from second array)
the two arrays have different lengths array(3) and array(4)
$myfinalarray = (array_unique(array_merge($arr_first, $arr_last)));
the problem is the order is lost
result:
array(4) { [0]=> string(3) "max" [1]=> string(3) "min" [2]=> string(4) "other" [3]=> string(3) "med" }
what i need is this
array(4) { [0]=> string(3) "max" [1]=> string(3) "min" [2]=> string(3) "med" [3]=> string(4) "other" }
You should erase the double entries from 2nd array first:
$array_last = array_diff($arr_last, $arr_first);
and then
$myfinalarray = (array_unique(array_merge($arr_first, $arr_last)));
Order is not lost, but if you want to sort array in alphabetical order use sort function
$myfinalarray=sort($myfinalarray);
Check the below code,
<?php
$arr1=array(0 => "max",1 => "min",2 => "med");
$arr3=array(0 => "max",1 => "min",2 => "other",3 =>"med");
$myfinalarray = (array_unique(array_merge($arr1, $arr3)));
print_r($myfinalarray);
?>
the output willbe like,
Array
(
[0] => max
[1] => min
[2] => med
[5] => other
)
check here
Related
So currently I'm having a problem where I have an array setup like this
array(8) {
[0]=> array(2) {
[0]=> float(2.1166666666667)
[1]=> string(7) "9434493"
}
[1]=> array(2) {
[0]=> float(2.07)
[1]=> string(7) "8591971"
}
[2]=> array(2) {
[0]=> float(2.0566666666667)
[1]=> string(8) "17015102"
}
[3]=> array(2) {
[0]=> float(2.0366666666667)
[1]=> string(7) "9637191"
}
[4]=> array(2) {
[0]=> float(2.015)
[1]=> string(8) "11405473"
}
[5]=> array(2) {
[0]=> float(1.9833333333333)
[1]=> string(8) "28233403"
}
[6]=> array(2) {
[0]=> float(2.0366666666667)
[1]=> string(8) "14248330"
}
[7]=> array(2) {
[0]=> float(2.0933333333333)
[1]=> string(8) "14987165"
}
}
After I use the function arsort() it looks like this:
array(8) {
[0]=> array(2) {
[0]=> float(2.1166666666667)
[1]=> string(7) "9434493"
}
[7]=> array(2) {
[0]=> float(2.0933333333333)
[1]=> string(8) "14987165"
}
[1]=> array(2) {
[0]=> float(2.07)
[1]=> string(7) "8591971"
}
[2]=> array(2) {
[0]=> float(2.0566666666667)
[1]=> string(8) "17015102"
}
[6]=> array(2) {
[0]=> float(2.0366666666667)
[1]=> string(8) "14248330"
}
[3]=> array(2) {
[0]=> float(2.0366666666667)
[1]=> string(7) "9637191"
}
[4]=> array(2) {
[0]=> float(2.015)
[1]=> string(8) "11405473"
}
[5]=> array(2) {
[0]=> float(1.9833333333333)
[1]=> string(8) "28233403"
}
}
So all cool that my array is sorted by the value of [0] index. But.....
When I try to loop through like this...
$x = 0;
while ($x < count($sorted_array)) {
$sorted_array[$x][0];
$x++;
}
It kept printing out the original array order. I then realized when I used the function arsort() it kept the original order of the indexes so that's why it was printing in the original array order.
Is there a function to fix this so I can loop it with an index? Any help will be much appreciated.
When you use arsort() you preserve the keys.
Because you are iterating using $x, you are effectively ignoring your sort call.
Either use rsort() with your loop.
Or use a foreach() loop after your arsort() call.
Or best, just call array_column() instead of looping.
Here are some demonstrations: (Demo Link)
$array=$copy=[
[2.1166666666667,9434493],
[2.07,8591971],
[2.0566666666667,17015102],
[2.0366666666667,9637191],
[2.015,11405473],
[1.9833333333333,28233403],
[2.0366666666667,14248330],
[2.0933333333333,14987165]
];
arsort($array);
var_export(array_column($array,0)); // <-- you lose the keys you preserved
echo "\n---\n";
foreach($array as $index=>$row){ // <-- you keep the keys you preserved
echo "$index : {$row[0]}\n";
}
echo "\n---\n";
rsort($copy); // you don't preserve the keys
for($x=0, $count=sizeof($copy); $x<$count; ++$x){ // you should cache the count instead of calling count() on every iteration
echo "$x : {$copy[$x][0]}\n";
}
Output:
array (
0 => 2.1166666666667,
1 => 2.0933333333333,
2 => 2.07,
3 => 2.0566666666667,
4 => 2.0366666666667,
5 => 2.0366666666667,
6 => 2.015,
7 => 1.9833333333333,
)
---
0 : 2.1166666666667
7 : 2.0933333333333
1 : 2.07
2 : 2.0566666666667
6 : 2.0366666666667
3 : 2.0366666666667
4 : 2.015
5 : 1.9833333333333
---
0 : 2.1166666666667
1 : 2.0933333333333
2 : 2.07
3 : 2.0566666666667
4 : 2.0366666666667
5 : 2.0366666666667
6 : 2.015
7 : 1.9833333333333
rsort() is great fit here.
Sort an array in reverse order
This function does not maintain index association
A quick look at PHP documentation suggests rsort() would work.
http://php.net/manual/en/array.sorting.php
If I have a matrix
[3,1,2,4]
[a,b,c,d]
And I need sort first row with usort key. But when I want reorder first array how do column moves at well
So output will be like this in this case described top
[1,2,3,4]
[b,c,a,d]
You can use array_multisort:
$x = [[3,1,2,4],['a','b','c','d']];
array_multisort($x[0], $x[1]);
var_dump($x);
Output:
array(2) {
[0]=>
array(4) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
}
[1]=>
array(4) {
[0]=>
string(1) "b"
[1]=>
string(1) "c"
[2]=>
string(1) "a"
[3]=>
string(1) "d"
}
}
I think what you're looking for is ksort.
Below I've appended the $var_dump of an array that I've created through user-actions involving an HTML form.
I'm certainly willing to consider other strategies for achieving the final effect (and I'm not, to be honest, positive that it would not be better achieved via WordPress functions, since that's the environment this problem originates in), but the simplest answer would be to take the array described below, get rid of the "NULL" responses, then alphabetize ALL of the remaining sets by last name (which I originally put first since in an earlier, similar version of the same task it happened to make the final sort a snap).
The output will appear on a sign-in list at an event, with first column numbered, second column showing first name - last name with company underneath, a signature line, a line describing the "ticket type," and the ticket price.
SO, just to make it easy to picture:
No. FIRST/LAST/company SIGNATURE TICKET TYPE PRICE
(#) Amy Abrams, Carolco ......... Member $5
The natural way to do it, for me, was to produce an HTML table in which each variable, located by some kind of index, is output via PHP. I've had some versions that ALMOST worked, involving foreach loops at the very limits of my understanding of array manipulation.
I suspect that the person who can answer my question could write what I've tried in his or her sleep, but, just to give a flavor without dumping in a bunch more code... having gotten the below array in a variable called $meta, I could foreach through it foreach ($meta as $reservation), then print some of the variables, but not all of them, and not all in right order, with $reservation['first_name'][0], for example. I tried dropping an $i in place of the [0], and fooling around with other key/value/iterator combinations, but at this point I'm just iterating through my own internal array of ca. 999 wrong vs 1 right solutions.
I know this is a common type of question, but in poring through the threads here, I have yet to find a version like this one directly answered.
array(8) {
[0]=>
array(5) {
["last_name"]=>
NULL
["first_name"]=>
NULL
["price"]=>
NULL
["company"]=>
NULL
["ticket_type"]=>
NULL
}
[1]=>
array(5) {
["last_name"]=>
array(3) {
[0]=>
string(6) "Winger"
[1]=>
string(6) "Dinger"
[2]=>
string(7) "Stassen"
}
["first_name"]=>
array(3) {
[0]=>
string(5) "Debra"
[1]=>
string(3) "Hum"
[2]=>
string(6) "Harold"
}
["price"]=>
array(3) {
[0]=>
string(2) "10"
[1]=>
string(2) "10"
[2]=>
string(4) "6.35"
}
["company"]=>
array(3) {
[0]=>
string(14) "Post Data Test"
[1]=>
string(14) "Post Data Test"
[2]=>
string(14) "Post Data Test"
}
["ticket_type"]=>
array(3) {
[0]=>
string(16) "Shippable Ticket"
[1]=>
string(16) "Shippable Ticket"
[2]=>
string(14) "Special Ticket"
}
}
[2]=>
array(5) {
["last_name"]=>
NULL
["first_name"]=>
NULL
["price"]=>
NULL
["company"]=>
NULL
["ticket_type"]=>
NULL
}
[3]=>
array(5) {
["last_name"]=>
array(3) {
[0]=>
string(9) "Mightwork"
[1]=>
string(1) "u"
[2]=>
string(3) "why"
}
["first_name"]=>
array(3) {
[0]=>
string(9) "Bizarrely"
[1]=>
string(11) "whatsamatta"
[2]=>
string(8) "done no "
}
["price"]=>
array(3) {
[0]=>
string(1) "5"
[1]=>
string(4) "12.7"
[2]=>
string(4) "12.7"
}
["company"]=>
array(3) {
[0]=>
string(13) "Get Data Test"
[1]=>
string(13) "Get Data Test"
[2]=>
string(13) "Get Data Test"
}
["ticket_type"]=>
array(3) {
[0]=>
string(16) "Shippable Ticket"
[1]=>
string(14) "Special Ticket"
[2]=>
string(14) "Special Ticket"
}
}
[4]=>
array(5) {
["last_name"]=>
NULL
["first_name"]=>
NULL
["price"]=>
NULL
["company"]=>
NULL
["ticket_type"]=>
NULL
}
[5]=>
array(5) {
["last_name"]=>
array(2) {
[0]=>
string(7) "Marlatt"
[1]=>
string(7) "Stewart"
}
["first_name"]=>
array(2) {
[0]=>
string(4) "Jeff"
[1]=>
string(3) "Al "
}
["price"]=>
array(2) {
[0]=>
string(1) "0"
[1]=>
string(1) "0"
}
["company"]=>
array(2) {
[0]=>
string(23) "Jeff Marlatt Consulting"
[1]=>
string(23) "Jeff Marlatt Consulting"
}
["ticket_type"]=>
array(2) {
[0]=>
string(16) "testing defaults"
[1]=>
string(16) "testing defaults"
}
}
[6]=>
array(5) {
["last_name"]=>
NULL
["first_name"]=>
NULL
["price"]=>
NULL
["company"]=>
NULL
["ticket_type"]=>
NULL
}
[7]=>
array(5) {
["last_name"]=>
array(3) {
[0]=>
string(10) "Flintstone"
[1]=>
string(10) "Flintstone"
[2]=>
string(6) "Rubble"
}
["first_name"]=>
array(3) {
[0]=>
string(4) "Fred"
[1]=>
string(5) "Wilma"
[2]=>
string(5) "Betty"
}
["price"]=>
array(3) {
[0]=>
string(1) "0"
[1]=>
string(1) "0"
[2]=>
string(1) "0"
}
["company"]=>
array(3) {
[0]=>
string(23) "Jeff Marlatt Consulting"
[1]=>
string(23) "Jeff Marlatt Consulting"
[2]=>
string(23) "Jeff Marlatt Consulting"
}
["ticket_type"]=>
array(3) {
[0]=>
string(6) "MEMBER"
[1]=>
string(6) "MEMBER"
[2]=>
string(6) "MEMBER"
}
}
}
So firstly you have built your array in a very strange way. If all you want to do is output all the different guests it would be better to create a new element in your array for each guest, that way you could sort them and iterate them with ease. However as you have asked the question I will give you a solution...
To be able to sort your array we need to take your array and turn it into the array I described above. This array will look something like this...
array(
[0] => array(
[last_name] => 'blah',
[first_name] => 'blah',
[company] => 'blah',
[ticket_type] => 'blah',
[price] => 'blah',
),
[1] => array(
[last_name] => 'blah',
[first_name] => 'blah',
[company] => 'blah',
[ticket_type] => 'blah',
[price] => 'blah',
),
etc...
);
Then you can use the PHP function usort() to sort your multidimensional array by the value of the subarrays. Here's my solution...
//This function is used by usort() to sort the guests array by last name
//UPDATE: this function is now case insensitive and sorts on first name secondarily
function sort_by_last_name( $a, $b ) {
//Convert values to lowercase to make sorting case insensitive
$a_firstname = strtolower( $a['first_name'] );
$a_lastname = strtolower( $a['last_name'] );
$b_firstname = strtolower( $b['first_name'] );
$b_lastname = strtolower( $b['last_name'] );
//If the last names are the same sort by first name
if( $a_lastname == $b_lastname ) {
return strcmp( $a_firstname, $b_firstname );
}
return strcmp( $a_lastname, $b_lastname );
}
//This function returns a nicely formatted array of
//reservations sorted alphabetically by last name
function sort_reservations( $reservations ) {
//If reservations is empty get outta there
if( !$reservations || !is_array( $reservations ) ) { return false; }
//Create an empty array to store the formatted data
$guests = array();
//Loop through reservations
foreach ( $reservations as $res ) {
//If the reservation is empty skip onto the next one
if( !$res['last_name'] ) { continue; }
//See how many people are in the reservation
$count = count( $res['last_name'] );
//iterate through reservation n number of times
for( $i = 0; $i <= $count - 1; $i++ ) {
//Add reservations to the nsorted guests array
$guests[] = array(
'last_name' => $res['last_name'][$i],
'first_name' => $res['first_name'][$i],
'price' => $res['price'][$i],
'company' => $res['company'][$i],
'ticket_type' => $res['ticket_type'][$i]
);
}
}
//sort gusts by last name alphabetically
usort( $guests, 'sort_by_last_name' );
//Return our nicely formatted and sorted array
return $guests;
}
Now for example your unformatted array of reservations is stored in a variable $reservations you can sort it by using...
$guests = sort_reservations( $reservations );
Then you build your table in html and for each table row you can iterate over this array printing out a new guest to every row. like this....
<?php $guests = sort_reservations( $reservations ); ?>
<table>
<thead>
<tr>
<th class="firstname">First Name</th>
<th class="lastname">Last Name</th>
<th class="company">Company</th>
<th class="tickettype">Ticket Type</th>
<th class="price">Price</th>
</tr>
</thead>
<tbody>
<?php foreach( $guests as $guest ) { ?>
<tr>
<td class="firstname"><?php echo $guest['first_name']; ?></td>
<td class="lastname"><?php echo $guest['last_name']; ?></td>
<td class="company"><?php echo $guest['company']; ?></td>
<td class="tickettype"><?php echo $guest['ticket_type']; ?></td>
<td class="price">£<?php echo round( $guest['price'], 2 ); ?></td>
</tr>
<?php } ?>
</tbody>
</table>
And bob's your uncle, you've got a table of guests sorted alphabetically by their surnames.
Hope that helps
Dan
This question already has answers here:
How to insert element into arrays at specific position?
(25 answers)
Insert new item in array on any position in PHP
(23 answers)
Closed 9 years ago.
I have an array
array(1) {
[0]=>
array(4) {
[0]=>
string(1) "1"
[1]=>
string(2) "10"
[2]=>
string(3) "100"
[3]=>
string(3) "200"
}
}
I want to insert two element into the array which must be the 3rd and last element.
Output:
array(6) {
[0]=>
array(6) {
[0]=>
string(1) "1"
[1]=>
string(2) "10"
[2]=>
string(1) ""
[3]=>
string(3) "100"
[4]=>
string(3) "200"
[5]=>
string(1) ""
}
}
how can i do this?
What I have tried
array_splice($input,3 ,0,"");
Then result become like this, the array not added in the middle
array(6) {
[0]=>
array(6) {
[0]=>
string(1) "1"
[1]=>
string(2) "10"
[2]=>
string(1) ""
[3]=>
string(3) "100"
[4]=>
string(3) "200"
[5]=>
string(1) ""
}
[1]=>
array(1) {
[0]=>
string(1) ""
}
}
To insert in the middle of array, you can use array_splice with a length of 0.
array_splice($input, 3, 0, "");
To add to the array, you can use either array_push or [] operator
By using array_splice you can insert element inside the array
$array = [0 => 'Data', 1 => 'data2', 2=> 'data3'];
array_splice($array, 1, 0, 'data append');
var_dump($array);
I have a $times array, which contains:
array(8) {
[0]=>
string(5) "10:00"
[1]=>
string(5) "13:00"
[2]=>
string(5) "10:00"
[3]=>
string(5) "11:00"
[4]=>
string(5) "12:00"
[5]=>
string(5) "13:00"
[6]=>
string(5) "14:00"
[7]=>
string(5) "15:00"
}
How can I a) sort it so it starts with lowest value b) only have one entry of each time? (no duplicates, currently theres two 10:00s and 13:00s etc)
Why not just use PHP's inbuilt functions:
$input = array_unique($input);
sort($input);
print_r($input);
Well, you could always use the php-shipped standard functions for arrays:
Array Unique
and sort()
$array = array_unique(sort($a));