I have an array like this
Array
(
[0] => Array
(
[0] => space
[1] => Venus
[2] => NASA
[3] => apple
)
[1] => Array
(
[0] => link1
[1] => link2
[2] => link3
[3] => link4
)
)
I want to sort it, case INsensitively, by the [0] term. How can I do this? All answers I've found show how to do this only if the array has keynames.
If that's not clear, I want this:
Array
(
[0] => Array
(
[0] => apple
[1] => NASA
[2] => space
[3] => Venus
)
[1] => Array
(
[0] => link4
[1] => link3
[2] => link1
[3] => link2
)
)
If it's easier with keynames, or it can't be done unless there are keynames (which I doubt), how can I modify my original array to contain names "keywords" for [0] and "links" for [1]?
Many thanks.
This code merges the multiple dimension version back into a key/value single array which makes it easy to sort, and then optionally brings it back to multiple dimensions. Demo here: https://3v4l.org/CStsS
$data = [
['space', 'Venus', 'NASA', 'apple'],
['link1', 'link2', 'link3', 'link4'],
];
// Convert the multiple dimensional array to avsingle
$merged = array_combine(...$data);
// Sort by key
uksort($merged, fn($a, $b) => strcasecmp($a, $b));
var_dump($merged);
// If it needs to be converted back to a multiple dimension array
$data = [
array_keys($merged),
array_values($merged),
];
var_dump($data);
EDIT
Another answer was provided by #lukas.j that used the built-in array_multisort function, but has since been deleted. In theory, being a native function, that code might be more performant for large arrays, and, with a couple of optimizations, it also skips the intermediary variables. I personally avoid multiple dimension arrays whenever possible (personal preference) so I’ve never used that function, but I think it works here perfectly.
$arr = [
[ 'space', 'Venus', 'NASA', 'apple' ],
[ 'link1', 'link2', 'link3', 'link4' ]
];
array_multisort($arr[0], SORT_NATURAL | SORT_FLAG_CASE, $arr[1]);
print_r($arr);
Demo here: https://3v4l.org/saI1C
Related
STARTING ARRAY
Array
(
[0] => Array
(
[0] => /searchnew.aspx?Make=Toyota&Model=Tundra&Trim=CrewMax+5.7L+V8+6-Spd+AT+SR5&st=Price+asc
[1] => 19
)
)
I have been struggling to break down this array for the past couple days now. I have found a few useful functions to extract the strings I need when a start and end point are defined, however, I can't see that being good for long term use. Basically I'm trying to take the string relative to [0], and extract the strings following "Model=" and "Trim=", in hopes to have array like this:
Array
(
[0] => Array
(
[0] => Tundra ***model***
[1] => CrewMax+5.7L+V8+6-Spd+AT+SR5 ***trim***
[2] => 19
)
)
I'm getting this information fed through an api, so coming up with a dynamic solution is my biggest challenge. I realize this a big question, but is there a better/less hacky way of approaching this problem?
parse_url() will get you the query string and parse_str() parses the variables from that:
$q = parse_url($array[0][0], PHP_URL_QUERY);
parse_str($q, $result);
print_r($result);
Yields:
Array
(
[Make] => Toyota
[Model] => Tundra
[Trim] => CrewMax 5.7L V8 6-Spd AT SR5
[st] => Price asc
)
Now just echo $result['Model'] etc...
I looked at different options how to sort arrays. But somehow none of the given PHP commands suit my purpose.
Example - I have an array like this :
Array
(
[abc] => Array
(
[2] => 2
[3] => 3
[5] => 5
)
)
But I want to change the array to
[0] => 2
[1] => 3
[2] => 5
In other words I want to remove all keys - sort all values from LOW to HIGH and then just give em the keys from zero to X
It's much easier to work with such an array if you want to use some loops like (for, while, etc.)
Just use sort and array_values.
<?php
$array = array(
'abc' => array(
2 => 2,
5 => 5,
3 => 3,
),
);
sort($array['abc']);
$array = array_values($array['abc']);
print_r($array);
I've popped up an example at http://3v4l.org/51naW
Hello i have an array in php
Array
(
[0] => red
[1] => blue
[2] => green
[3] => yellow
[4] => purple
[5] => white
)
and i want to sort it using that array
Array
(
[0] =>
[1] => 0
[2] => -1
[3] => -5
[4] => -5
[5] => 9
)
so i want the element with the greatest value on the second array to come first on the first array not its value from the second array but the element it self from the first array to move in the first position on the first array! The second bigger to the second place etc.. elements with the same value don't care me how they will be arranged!
the output i want to get is
Array
(
[0] => white
[1] => blue
[2] => green
[3] => yellow
[4] => purple
[5] => red
)
You can use array_multisort() :
$ar1 = array(/* your SO links */);
$ar2 = array(/* your numbers */);
array_multisort($ar2, SORT_DESC, $ar1);
Documentation here
Use array_multisort.
see http://www.php.net/manual/fr/function.array-multisort.php, follow the "Exemple #1 Trier plusieurs tableaux"
Cordially
Lets call your arrays are $dataArray, and $sortArray respectively
asort($sortArray);
foreach ( $sortArray as $key=>$val ) {
$newArray[] = $dataArray[$key];
}
If you need it reversed, just add in a call to array_reverse() at the end.
I think what the OP wants is to sort the first array by the second array's values.
IE- the second array is the vote count for the first array's web pages.
This could be solved more simply if in the first array you have each element as an array with two elements in it, webpage & vote count
$pages = Array
(
array(
'page' => 'http://stackoverflow.com/questions/640805/open-source-ios-components-reusable-views-controllers-buttons-table-cells-e',
'count' => 0
)
array(
'page' => 'http://stackoverflow.com/questions/3889634/fast-and-lean-pdf-viewer-for-iphone-ipad-ios-tips-and-hints',
'count' => -1
)
// etc for the rest...
)
But since the question asked how to do it in the current data structure:
$sorted_values = asort($pages);
$new_pages = array();
$sorted_keys = array_keys($sorted_values);
foreach($sorted_keys as $page_key)
array_push($new_pages, $pages[$page_key]);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Combine Two Arrays with numerical keys without overwriting the old keys
OK guys, was searching about this one with no luck - it always points only to array_merge or array_push or array_combine functions which are useless for my purpose.
Here are two arrays (number indexed):
Array (
[0] => 12345
[1] => "asdvsdfsasdfsdf"
[2] => "sdgvsdfgsdfbsdf"
)
Array (
[0] => 25485
[1] => "tyjfhgdfsasdfsdf"
[2] => "mojsbnvgsdfbsdf"
)
and I need to create one "joined" (unioned) array, so it will look like:
Array (
[0] => 12345
[1] => "asdvsdfsasdfsdf"
[2] => "sdgvsdfgsdfbsdf"
[3] => 25485
[4] => "tyjfhgdfsasdfsdf"
[5] => "mojsbnvgsdfbsdf"
)
As I found nothing on this problem I tried by myself ($arr1 and $arr2 are the two small arrays):
$result_array = $arr1;
foreach($arr2 as $v) {
$result_array[] = $v;
}
This is, of course, working fine but I don't like this approach - imagine the situation when there will not be just 3 elements in second array...
Question: is there a better approach or at the best some built-in function (I do not know about)???
array_merge will work without any problem as your using numeric keys ... see the explanation below from the docs
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
emphasis mine
Array merge works fine for your numerically indexed arrays:
<?php
$arrayOne = array(
0 => 12345
,1 => "asdvsdfsasdfsdf"
,2 => "sdgvsdfgsdfbsdf"
);
$arrayTwo = array(
0 => 25485
,1 => "tyjfhgdfsasdfsdf"
,2 => "mojsbnvgsdfbsdf"
);
$arrayMerged = array_merge($arrayOne, $arrayTwo);
print_r($arrayMerged);
?>
output:
Array
(
[0] => 12345
[1] => asdvsdfsasdfsdf
[2] => sdgvsdfgsdfbsdf
[3] => 25485
[4] => tyjfhgdfsasdfsdf
[5] => mojsbnvgsdfbsdf
)
I have three arrays each having SimpleXML Objects in them. They are structured like so:
Array
(
[0] => SimpleXMLElement Object
(
[post_id] => 1476
[name] => Johnson Fisheries Ltd.
[owner] => Mr. John Johnson
)
)
I want to be able to compare all 3 arrays and filter out the differences so that the results have only the elements that are the same in all 3 arrays.
For example:
Array1
(
[0] => 1476
[1] => 1560
[2] => 1342
)
Array2
(
[0] => 2454
[1] => 1476
)
Array3
(
[0] => 3412
[1] => 7512
[2] => 2454
[4] => 1476
)
The resulting array would only contain [0] => 1476
What's the best way to do this? I've looked for a function that will compare arrays in this way but I have had no luck. Any ideas?
Any help is very appreciated!
Best option will be using php's built-in array-intersect function.
$answer = array_intersect($Array1,$Array2,$Array3);
Loop over your first array, checking it against the 2nd and 3rd using in_array, like this:
foreach($array1 as $post_id) {
if(in_array($post_id, $array2) && in_array($post_id, $array3)) {
// We have a winner!
}
}
create a new array.
add all elements of the first array into the new array.
iterate through all the other elements, find out which elements in your new array are in the array to be compared. remove all the elements that don't appear.
at the end of your iterations, you have your desired array.