i have this array
$immagini = array('1.jpg','2.jpg','3.jpg','4.jpg');
if i make var_dump($immagini) return this
array(4) {
[0]=> string(5) "1.jpg"
[1]=> string(5) "2.jpg"
[2]=> string(5) "3.jpg"
[3]=> string(5) "4.jpg"
}
now how do I order them in another way, i like start from index [2]....so i want this result
array(4) {
[2]=> string(5) "3.jpg"
[3]=> string(5) "4.jpg"
[0]=> string(5) "1.jpg"
[1]=> string(5) "2.jpg"
}
I would like from an index and return the complete list
You can't use var_dump to accomplish that. You'd need your own loop to print from the custom starting index. Start from the starting index and increment your loop iterator by one at each iteration until you have looped length of array times. When you want to access an array element, use loopIterator mod length of array
You could sort them with a comparison function that moves all index < 2 to the end of the list. For example, here would be such a comparison:
function cmp($a,$b) {
$a = ($a < 2) ? $a + 1000 : $a;
$b = ($b < 2) ? $b + 1000 : $b;
return $a - $b;
}
Called like this:
$immagini = array('1.jpg','2.jpg','3.jpg','4.jpg');
uksort($immagini, 'cmp');
var_dump($immagini);
Gives the following output:
array(4) {
[2]=>
string(5) "3.jpg"
[3]=>
string(5) "4.jpg"
[0]=>
string(5) "1.jpg"
[1]=>
string(5) "2.jpg"
}
Demo: http://ideone.com/XeAkQL
Related
I'm trying to sort a two dimensions array, and I have no idea where to start. I looked at array_multisort, but I don't really found a good solution with this sorting.
I need to sort by time, each time are associate with a race. I need to find who are the best 5 person so the best time.
My array looks like this:
[0]=>
array(2) {
[0]=>
string(15) "Beaumier Mélina"
[1]=>
string(7) "1:29.30"
}
[1]=>
array(2) {
[0]=>
string(14) "Frizzle Émilie"
[2]=>
string(7) "1:47.96"
}
[2]=>
array(3) {
[0]=>
string(18) "Morissette Camélia"
[2]=>
string(7) "1:50.26"
[1]=>
string(7) "1:50.97"
}
You can use usort. You give it a callback function and compare each index of the array. Since you make the callback function you can compare by the time for each index in the array.
http://php.net/usort
From the above documentation:
<?php
function cmp($a, $b)
{
return strcmp($a["fruit"], $b["fruit"]);
}
$fruits[0]["fruit"] = "lemons";
$fruits[1]["fruit"] = "apples";
$fruits[2]["fruit"] = "grapes";
usort($fruits, "cmp");
?>
I var_dumped two arrays, the top on is the array coming in, $new_array, while the other one is a preexisting array $current_array:
// New Array
array(3) {
["Contributor"]=>
array(2) {
[0]=>
string(13) "edit_carousel"
[1]=>
string(13) "read_carousel"
}
["Editor"]=>
array(1) {
[0]=>
string(16) "delete_mini_feed"
}
["Author"]=>
array(3) {
[0]=>
string(11) "edit_blocks"
[1]=>
string(12) "edit_blockss"
[2]=>
string(12) "edit_blockss"
}
}
// Preexisting
array(3) {
["Contributor"]=>
array(2) {
[0]=>
string(13) "edit_carousel"
[1]=>
string(13) "read_carousel"
}
["Editor"]=>
array(4) {
[0]=>
string(16) "delete_mini_feed"
[1]=>
string(15) "edit_mini_feeds"
[2]=>
string(23) "edit_private_mini_feeds"
[3]=>
string(15) "edit_mini_feeds"
}
["Author"]=>
array(3) {
[0]=>
string(11) "edit_blocks"
[1]=>
string(12) "edit_blockss"
[2]=>
string(12) "edit_blockss"
}
}
I am trying to do something like: var_dump(array_intersect_assoc($current_array, $new_array)); to see whats different in the current array as opposed to the new array and generate an array of "differences" keeping the structure intact.
The issue is:
Is the order of arrays to be compared right? compare old to new and get an array of whats different in old. or should it be compare new to old?
Doing this, results in: Array to string conversion notice, but also prints out an array which is below.
I cant tell if these are: "these are whats not in old, but in new" or "the are whats not in new but in old" .... (It should say: these are not whats in old but in new).
array(3) {
["Contributor"]=>
array(2) {
[0]=>
string(13) "edit_carousel"
[1]=>
string(13) "read_carousel"
}
["Editor"]=>
array(4) {
[0]=>
string(16) "delete_mini_feed"
[1]=>
string(15) "edit_mini_feeds"
[2]=>
string(23) "edit_private_mini_feeds"
[3]=>
string(15) "edit_mini_feeds"
}
["Author"]=>
array(3) {
[0]=>
string(11) "edit_blocks"
[1]=>
string(12) "edit_blockss"
[2]=>
string(12) "edit_blockss"
}
}
The php function array_intersect_assoc() should return everything in the first array (aka $current_array) that exists in the second array (aka $new_array).
The issue that you are running into is that array_intersect_assoc() doesn't preform the key comparison recursively. It's only comparing the first level of keys (Contributor, Editor and Author).
Here's more information about the recursive issue. PHP Question: how to array_intersect_assoc() recursively
Your problem is that you are trying to perform array_intersect on a multidimensional array, but the function does string comparison of elements, resulting in array to string conversion error.
As you have the same keys in both arrays, the simplest solution is just to foreach through and to compare subsequent arrays(and you rather need array_diff if you want difference between the elements)
foreach($array_1 as $index => $sub_array) {
$sub_array_2 = $array_2[$index];
$diff = array_diff($sub_array, $sub_array_2);
// do something with diff
}
UPDATE
If You want to get everything that's not in array_1 but in array_2:
$result = [];
# lets find if there's any new elements
$keys_1 = array_keys($array_1);
$keys_2 = array_keys($array_2);
$diff = array_diff($keys_1, $keys_2);
if(!empty($diff)) {
foreach($diff as $key) {
if(isset($array_2[$key])) {
# it's in array_2
$result[$key] = $array_2[$key];
}
}
}
# now get difference between shared elements
$intersection = array_intersect($keys_1, $keys_2);
foreach($intersection as $key) {
$element_1 = $array_1[$key];
$element_2 = $array_2[$key];
$diff = array_diff($element_1, $element_2);
if(sizeof($diff)) {
if(!isset($result[$key]) ||!is_array($result[$key]) ) {
$result[$key] = array($diff);
} else {
$result[$key][] = $diff;
}
}
}
I have an array of arrays like this:
array(18) {
[0]=>
array(3) {
["ID"]=>
string(5) "23"
["EYE_SIZE"]=> "203.C"
}
[1]=>
array(2) {
["ID"]=>
string(5) "1"
["EYE_SIZE"]=> "231.2A"
}
[2]=>
array(2) {
["ID"]=>
string(5) "32"
["EYE_SIZE"]=> "231.2B"
}
[3]=>
array(3) {
["ID"]=>
string(5) "90"
["EYE_SIZE"]=> "201A"
}
... and so on
}
And I want the arrays in the array to be sorted alphanumerically based on the EYE_SIZE value. For example, if the array had an EYE_SIZE value of 201A, I would want it to be before arrays with EYE_SIZEs of 203A, 201B, or 201.2A.
Is there a function in PHP that can help me achieve this?
You could use usort and write your own comparison function.
function cmp($a, $b)
{
return strcmp($a["EYE_SIZE"], $b["EYE_SIZE"]);
}
usort($your_array, "cmp");
or with a closure
usort($your_array, function($a, $b){
return strcmp($a["EYE_SIZE"], $b["EYE_SIZE"]);
});
I have 2 Arrays in 2 variables and both of them containing exactly the same values in the fields (in the 1st array = "image_id"-field and in the 2nd array = "ID-field").
I need to compare the 2 fields and would like to output the imagepath string of the 1st array (if the "ID"-field of 1st array and the field of 2nd array are equal)
Something like this:
if "2146" from 1st multi-array is equal to "2146" from 2nd multi-array, then echo apple.jpg..
But how does that work? Its really freakin me out the last days.. thanks in advance for your replies.
$multidimensional_array1:
array(4) {
[0]=>
string(9) "apple.jpg"
["imagepath"]=>
string(9) "apple.jpg"
[1]=>
string(4) "2146"
["image_id"]=>
string(4) "2146"
}
array(4) {
[0]=>
string(10) "ananas.jpg"
["imagepath"]=>
string(10) "ananas.jpg"
[1]=>
string(4) "2037"
["image_id"]=>
string(4) "2037"
}
array(4) {
[0]=>
string(8) "nuts.jpg"
["imagepath"]=>
string(8) "nuts.jpg"
[1]=>
string(4) "2024"
["image_id"]=>
string(4) "2024"
}
$multidimensional_array2:
array(2) {
[0]=>
string(4) "2146"
["ID"]=>
string(4) "2146"
}
array(2) {
[0]=>
string(4) "2037"
["ID"]=>
string(4) "2037"
}
array(2) {
[0]=>
string(4) "2024"
["ID"]=>
string(4) "2024"
}
As long as the arrays have the same keys, length and order, you can iterate over one and and pick values from both.
$len = count($arr1);
for ($i = 0; $i < $len; $i++)
{
if ($arr1[$i]['image_id'] == $arr2[$i]['ID'])
{
// output $arr1[$i]['imagepath']
}
}
If the information is from two tables in the same database, you would be better off by just joining the tables together. If the arrays are not ordered the same or not of the same length (so that $i might reference different elements in both arrays), use one as a lookup table:
$lookup = array();
foreach ($arr2 as $element)
{
$lookup[$element['ID']] = $element;
}
foreach ($arr1 as $element)
{
if (isset($lookup[$element['image_id']]))
{
// output $element['imagepath']
}
}
foreach($multidimensional_array1 as $arr1){
foreach($multidimensional_array2 as $arr2){
if($arr2['id']==$arr1['image_id']){
echo $arr1['imagepath'];
}
}
}
Note: The larger the arrays become the longer this will take.
Given an array of objects stored in $my_array, I'd like to extract the 2 objects with the highest count value and place them in a separate object array. The array is structured as below.
How would I go about doing that?
array(1) {
[0]=> object(stdClass)#268 (3) {
["term_id"]=> string(3) "486"
["name"]=> string(4) "2012"
["count"]=> string(2) "40"
}
[1]=> object(stdClass)#271 (3) {
["term_id"]=> string(3) "488"
["name"]=> string(8) "One more"
["count"]=> string(2) "20"
}
[2]=> object(stdClass)#275 (3) {
["term_id"]=> string(3) "512"
["name"]=> string(8) "Two more"
["count"]=> string(2) "50"
}
You can do this many ways. One rather naive way would be to use usort() to sort the array, and then pop off the last two elements:
usort($arr, function($a, $b) {
if ($a->count == $b->count) {
return 0;
}
return $a->count < $b->count ? -1 : 1
});
$highest = array_slice($arr, -2, 2);
Edit:
Note that the previous code uses an anonymous function, which is only available in PHP 5.3+. If you're using < 5.3, you can just use a normal function:
function myObjSort($a, $b) {
if ($a->count == $b->count) {
return 0;
}
return $a->count < $b->count ? -1 : 1
}
usort($arr, 'myObjSort');
$highest = array_slice($arr, -2, 2);
You can use array_walk() then write a function that checks the value of count.