sort multidimensional array by values in PHP - php

I have an array fetched from mysql database tables of type. I want to sort it in order of particular value.
$arr1=array(array(12, 8, 5, 34),
array(54, 87, 32, 10),
array(23, 76, 98, 13),
array(53, 16, 24, 19));
How can i sort it by value?
Like sorting by 2nd value should result to.
$arr1=array(array(12, 8, 5, 34),
array(53, 16, 24, 19),
array(23, 76, 98, 13),
array(54, 87, 32, 10));

I like to use usort to solve these problems.
$sortKey = 1;
usort($arr1, function($a, $b) use($sortKey){
return $a[$sortKey] - $b[$sortKey];
});

Got to agree with #RocketHazmat, array_multsort is a royal pain in the backside. usort is much easier to follow but I thought I'd have a go anyway:
$sortKey = 1;
array_multisort(array_map(function($v) use($sortKey){
return $v[$sortKey];
}, $arr1), $arr1);
It only took 20 minutes... :(
Here's a demo: http://ideone.com/2rZYIz

Related

how to get same values if an array have values same with values of anther array values in PHP?

Having two arrays of authRoom and partiRoom with one same value inside them. Want to find that same value if it was matched
Found array_search function that work only with single variable
$authRoom = [8, 7, 1, 22, 13, 18, 10];
$partiRoom= [3, 6, 5, 9, 8];
I want the output to be 8 which is the same value of these two arrays
You can use array_intersect which will give you an array of the same values in both $authRoom and $partiRoom like so:
$authRoom = [8, 7, 1, 22, 13, 18, 10];
$partiRoom = [3, 6, 5, 9, 8];
$res = array_intersect($authRoom, $partiRoom);
print_r($res); // [8]
If you want to get the value 8 outside of the array, you can simply access the first value using index 0:
$res = array_intersect($authRoom, $partiRoom)[0];
echo $res; // 8

Compare the new array with the previous one

I have an array with 4 elements
[1, 2, 3, 4]
So far I am printing several arrays, all with different elements in each array, to a limit set by me.
for($i = 0; $i<=100; $i++){//...
Output so far:
[11, 22, 32, 44]
[22, 33, 44, 45]
[12, 24, 25, 31]
[15, 16, 31, 41]
[22, 33, 44, 45]//already exist
[11, 22, 32, 44]//already exist
...
How can I compare an outgoing array to the next one going out and delete the new one if is equal to the previous array?
You could create a key for an array with the help of implode() and have a set array which has this key. If key is already present, then the current array in iteration is a duplicate one, else it's a new one. Remember to sort the current array as the order of the numbers matter here for proper key check.
<?php
$arr = [
[11, 22, 32, 44],
[22, 33, 44, 45],
[12, 24, 25, 31],
[15, 16, 31, 41],
[22, 33, 44, 45],
[44, 22, 32, 11]
];
$set = [];
foreach($arr as $curr_array){
sort($curr_array);
$hash = implode("|",$curr_array);
if(isset($set[$hash])) echo "Duplicate",PHP_EOL;
else{
print_r($curr_array);
$set[$hash] = true;
}
}
Demo: https://3v4l.org/EXXRu

two-dimensional array inside two-dimensional arrays in php

Is it possible to achieve something like this? A two-dimensional array containing two-dimensional jagged array?
$jobOrder = array(array(1, "Web Developer", 100,
array(array(1, "PHP", 1),
array(2, "HTML", 1), array(3, "JAVA", 1)),
array(array(1, "pleasing personality", 1),
array(2, "english skills", 1)), 0),
array(2, "Senior Programmer", 50,
array(array(3, "Phython", 1),
array(5, "RUBY", 1),
array(10, "c#", 1)),
array(array(5, "good social skills", 1),
array(11, "management skills", 1))));
I want to store Job Order details into an array that should contain an orderID, job title, number of openings, skills(may have multiple skills so stored in an array; 2-d because I also wanted to store skillID, skill name and flag: if its been removed or not), qualifications(may have multiple qualifications same with skills), requirements and benefits (also same with skills). I would like to know how to access it.
You can access element like this:
$jobOrder = array(array(1, "Web Developer", 100,
array(array(1, "PHP", 1),
array(2, "HTML", 1), array(3, "JAVA", 1)),
array(array(1, "pleasing personality", 1),
array(2, "english skills", 1)), 0),
array(2, "Senior Programmer", 50,
array(array(3, "Phython", 1),
array(5, "RUBY", 1),
array(10, "c#", 1)),
array(array(5, "good social skills", 1),
array(11, "management skills", 1))));
print_r($jobOrder[0][3]);
I will print the array and also you can access the elements in the array also by adding further indexes.
Yes It is possible as you can store number of array rows in the array: means you can add number of rows as well number of elements inside the array even it may multidimensional array as you defined.

Array splice PHP and error in the documentation?

Take a look at this page with the documentation for array_splice, and inspect the first example.
http://www.php.net/manual/en/function.array-splice.php
It is confusing me as the following code in example 1 is not correct as far as I can tell,
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
// $input is now array("red", "green")
When I do this on my local machine I get the following,
$array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
array_splice($array, 10);
// $array is now array(11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
On PHPs page the example returns the elements, whereas on my local machine it removes them instead?
I don't get it? Am I missing something?
Probably the documentation is wrong.
I tested your code and the code on php.net and works perfectly great, as in your example.
I am getting this:
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
// $input is now array("blue", "yellow")
So i would say that php.net is wrong (weird!! isn't it????)

converting a php array into a database schema

Currently I am storing adjacencies in a php file in an array. Here's a sample of it:
$my_neighbor_lists = array(
1=> array(3351=> array (2, 3, 5 , 6, 10)),
2=> array(3264=> array (322, 12, 54 , 6, 10), 3471=>array (122, 233, 35 , 476, 210)),
3=> array(3309=> array (52, 32, 54 , 36, 210), 3469=>array (152, 32, 15 , 836, 10)),
etc
I would like to basically migrate this into a db. Any suggestions on how many table I should have? I am looking at three tables here.
two tables:
1. vertices (id)
2. edgecost (idfrom, idto, time, cost)

Categories