Ordering a column in a two dimensional array - php

new here so thanks for taking the time to read my question.
I am running some PHP code that compares the numbers enter on the screen with those in a database. The problem I am having is ordering the two dimensional array after manipulating each line. It looks as though the array id numbers are being removed. I would like to order the array by column [2] in descending order. Can anyone offer any help?
while( $a_row = mysql_fetch_array( $result))
{
$draw = array($a_row['Drawn1'],
$a_row['Drawn2'],
$a_row['Drawn3'],
$a_row['Drawn4'],
$a_row['Drawn5'],
$a_row['Drawn6'],
$a_row['Drawn7'],
$a_row['Drawn8']);
$numbers = array("6", "9", "4", "8", "14", "18");
if (count(array_intersect($draw, $numbers)) >= 1) {
$rs = array(($a_row['DrawNo']), join(" , ",array_intersect($draw, $numbers)), count(array_intersect($draw, $numbers)));
} else {
$rs = null;
}
array_multisort($rs[1], SORT_NUMERIC, SORT_DESC, $rs[0], SORT_ASC, SORT_STRING);
print_r ($rs);
echo "<br />";
}
This is what the output looks like.
Array ( [0] => A0048 [1] => 14 [2] => 1 )
Array ( [0] => A0049 [1] => 6 , 14 , 8 , 18 [2] => 4 )
Array ( [0] => A0050 [1] => 14 [2] => 1 )
Array ( [0] => A0051 [1] => 14 [2] => 1 )
Array ( [0] => A0052 [1] => 18 [2] => 1 )
Array ( [0] => A0053 [1] => 6 , 14 [2] => 2 )
Array ( [0] => A0054 [1] => 6 [2] => 1 )
Array ( [0] => A0055 [1] => 14 [2] => 1 )
Array ( [0] => A0056 [1] => 4 [2] => 1 )
Array ( [0] => A0057 [1] => 9 , 6 , 4 [2] => 3 )
Thanks for your time
zeroanarchy

Numeric array keys being reindexed is part of the documented behavior of array_multisort(). If you need keys preserved, you need to convert them to string keys.

you are probably looking for:
http://php.net/manual/en/function.array-multisort.php

Related

How to sum 90+ Arrays with using function?

I would like to sum up several arrays. I have a script that dynamically creates arrays without name. Below is an example. In my script i have a 90+ arrays. I want to sum up all it. All keys in that arrays be user id, so i only want sum values of keys. How to do it? Regards
Array
(
[1] => 1
[2] => 1
[3] => 1
)
Array
(
[1] => 1
[2] => 1
)
Array
(
[1] => 1
)
I want to get only one array result like:
Array
(
[1] => 3
[2] => 2
[3] => 1
)
Add all your arrays into one array.
$all_arrays[] =Array
(
[1] => 1 ,
[2] => 1,
[3] => 1
);
$all_arrays[] = Array
(
[1] => 1,
[2] => 1
) ;
$all_arrays[] = Array
(
[1] => 1
);
$results = [];
foreach($all_arrays as $arr){
foreach($arr as $user_id=>$value){
if(in_array($user_id,$results)){
$results[$user_id] = $results[$user_id] + $value;
}else{
$results[$user_id] = $value;
}
}
}
You would need to specify all arrays in the array_merge:
$result = array_count_values(array_keys(array_merge($array1, $array2, $array3)));
If you can get the arrays dynamically added to another $main_array like:
Array
(
[0] => Array
(
[1] => 1
[2] => 1
[3] => 1
)
[1] => Array
(
[1] => 1
[2] => 1
)
[2] => Array
(
[1] => 1
)
)
Then it would be much easier:
$result = array_count_values(array_keys(array_merge(...$main_array)));

array unique make item disapear

I have this array :
Array
(
[0] => Array
(
[0] => Array
(
[0] => 10
[id_list] => 1
[id] => 1
)
[1] => Array
(
[0] => 11
[id_list] => 1
[id] => 1
)
[2] => Array
(
[0] => 12
[id_list] => 1
[id] => 1
)
)
[1] => Array
(
[0] => Array
(
[0] => 11
[id_list] => 2
[id] => 2
)
[1] => Array
(
[0] => 12
[id_list] => 2
[id] => 2
)
)
[2] => Array
(
[0] => Array
(
[0] => 13
[id_list] => 4
[id] => 4
)
)
)
and this code (where $dataListe is the result of a fetchAll query) :
$result = [];
foreach($dataListe as $listeDiff){
$result[] = $listeDiff;
}
// $resultUnique = array_unique($result);
echo "<pre>".print_r($result, true)."</pre>";
as you can see, there's some contact similar in my first and my second array (but contact can be the same in the 1st and the 3rd array, is I choose to add my contact in my 3rd array).
I want to remove the duplicate of each element in the general array.
But when I use array unique, I get this result :
Array
(
[0] => Array
(
[0] => Array
(
[0] => 10
[id_list] => 1
[id] => 1
)
[1] => Array
(
[0] => 11
[id_list] => 1
[id] => 1
)
[2] => Array
(
[0] => 12
[id_list] => 1
[id] => 1
)
)
)
Please I need help to only keep 1 item of each array at the end !
EDIT : I have almost the good result with the code below, but the id 12 is missing
$result = [];
foreach($dataListe as $listeDiff){
foreach($listeDiff as $contact){
if(!in_array($contact,$result)){
$result[] = $contact;
}
break;
}
}
As the PHP docs says :
Note: Note that array_unique() is not intended to work on multi dimensional arrays. (http://php.net/manual/en/function.array-unique.php)
You can try this solution
$uniqueResult = array_map("unserialize", array_unique(
array_map("serialize", $result)
));
as suggested by #daveilers on this question How to remove duplicate values from a multi-dimensional array in PHP.

PHP copy array without references

When I copy PHP array with reference, copy already has references from original
$arr = [1,2,3];
print_r($arr); echo"<br>";
$x = &$arr[1];
$arr2 = $arr;
print_r($arr); print_r($arr2); echo"<br>";
$x = 8;
print_r($arr); print_r($arr2); echo"<br>";
Result:
Array ( [0] => 1 [1] => 2 [2] => 3 )
Array ( [0] => 1 [1] => 2 [2] => 3 ) Array ( [0] => 1 [1] => 2 [2] => 3 )
Array ( [0] => 1 [1] => 8 [2] => 3 ) Array ( [0] => 1 [1] => 8 [2] => 3 )
How can I copy an array, so it has not changed with the original reference?
Array ( [0] => 1 [1] => 2 [2] => 3 )
Array ( [0] => 1 [1] => 2 [2] => 3 ) Array ( [0] => 1 [1] => 2 [2] => 3 )
Array ( [0] => 1 [1] => 8 [2] => 3 ) Array ( [0] => 1 [1] => 2 [2] => 3 )
If your issue is solved using the duplicate link in your question's first comment (supported by 4 others at the time I'm writing this). Please delete your question so that SO can reduce duplicate questions / needless bloat.
Otherwise, just declare a static copy of the original array for future use.
$arr = [1,2,3];
$copy=$arr; // preserve the original
print_r($arr); echo"<br>";
$x = &$arr[1];
$arr2 = $arr;
print_r($arr); print_r($arr2); echo"<br>";
$x = 8;
print_r($arr); print_r($copy); echo"<br>";

Split an array into a MD array every nth line

bit of a question here. Ive got an array that contains data which has been parsed from a website using all our favourite php functions such as array_map.
The array is current 3 sub arrays deep.
Here is the code I am using:
for ($tcid = 1; $tcid <= count($categories); $tcid++) {
$catHeader[$tcid] = $categories[$tcid][0];
$event[$i]['tickets'] = $categories;
unset($categories[$tcid][0]);
$categories[$tcid] = array_map('trim', $categories[$tcid]);
$categories[$tcid] = array_values($categories[$tcid]);
$ab = 0;
for ($b = 0; $b <= count($categories[$tcid]); $b++) {
if ($categories[$tcid][$b] == "" || !$categories[$tcid][$b] || $categories[$tcid][$b] == null) {
unset($categories[$tcid][$b]);
}
}
}
and the array looks something like....
[1] => Array (
[data] => Array ( ...
)
[tickets] => Array (
[1] => Array (
[0] => xxx
[1] => etc
[3] => etc2
)
[2] => (
[0] => Std1
[1] => 10 / 10
[2] => £20.00
[3] => £200.00
[4] => Std2
[5] => 100 / 100
[6] => £13.00
[7] => £1,300.00
[8] => Std3
[9] => 10 / 320
[10] => £15.00
[11] => £150.00
)
)
)
My question to you today, is how on earth do I split the array every 4 \n's or array keys as they're known and explode each 4 into a further sub array?
So that Std1, Std2, Std3 will be their own sub array with its associated data of the 2nd key of tickets, but also doing this for every sub array of tickets that has more than 1 set of data (a set of data being 4 array keys).
I've tried all sorts but can't get it to work.
See below of how I want it to look.
[1] => Array (
[data] => Array ( ...
)
[tickets] => Array (
[1] => Array (
[0] => xxx
[1] => etc
[3] => etc2
)
[2] => (
[0] => Array (
[0] => Std1
[1] => 10 / 10
[2] => £20.00
[3] => £200.00
)
[1] => Array (
[0] => Std2
[1] => 100 / 100
[2] => £13.00
[3] => £1,300.00
)
[2] => Array (
[0] => Std3
[1] => 10 / 320
[2] => £15.00
[3] => £150.00
)
)
)
)
Thanks
As noted in the comments, you'd be best off handling your array by-reference to modify it's original contents somewhere within your loops
Provided the array groupings you want to chunk are in groups of 4, you could array_chunk() it:
$array['tickets'][2] = array_chunk($array['tickets'][2], 4);

Array sorting question

So I have an array such as this one:
Array
(
[-1] => Array
(
[3] => 3
[1] => 1
[6] => 6
[7] => 7
[5] => 5
)
)
It also contains some other keys that should not be modified.
I'd like to the numbers which are in a second array to come first (in the order of that second array), and then will be the numbers that don't exist in the second array, if any.
So for that matter, the second array would be:
Array
(
[0] => 6
[1] => 5
[2] => 3
)
And the final array should be as follows (please remember, there are some more keys inside of that array that should stay as they are):
Array
(
[-1] => Array
(
[6] => 6
[5] => 5
[3] => 3
[1] => 1
[7] => 7
)
)
Any ideas how that can be done?
Thanks!
It's not and shouldn't be termed as sorting but may be this code snippet may help you do what you want to:
$a1 = Array ( [-1] => Array ( [3] => 3 [1] => 1 [6] => 6 [7] => 7 [5] => 5 ) );
$a2 = Array ( [0] => 6 [1] => 5 [2] => 3 );
$sorted = getSortedArray($a1[-1] , $array2);
function getSortedArray($array1 , $array2){
$temp = Array();
$count = 0;
$totalKeys = sizeof($array2);
for($i=0;$i<sizeof($array2);$i++){
$temp[i] = $array1[$array2[i]];
unset($array1[$array2[i]]);
}
while($count!=sizeof($array1))
$temp[$totalKeys++] = $array1[$count++];
return $temp;
}
I believe the function you're looking for is called array_multisort().
array_multisort() can be used to sort
several arrays at once, or a
multi-dimensional array by one or more
dimensions.

Categories