如何从toArray创建的数组中获取值

时间:2023-02-09 13:32:23

I am using jquery sortable to organize some slides. I am able to return an array of the id's in their new order but am struggling with how to iterate over these in the controller to update the sort order of the slides.

我使用jquery sortable来组织一些幻灯片。我能够以新的顺序返回id的数组,但我正在努力如何在控制器中迭代这些数据以更新幻灯片的排序顺序。

The array looks like this

该数组看起来像这样

[2016-06-25 02:09:53] local.INFO: array (
'order' => 
array (
0 => '2',
1 => '3',
2 => '1',
3 => '4',
4 => '5',
5 => '6',
6 => '7',
7 => '8',
8 => '9',
9 => '10',
10 => '11',
11 => '12',
12 => '13',
13 => '14',
14 => '15',
15 => '16',
16 => '17',
17 => '18',
18 => '19',
19 => '20',
20 => '21',
21 => '22',
 ),
) 

So the id's of the slides are being returned in the new order, which is what I need, but now I need to do a foreach loop, or something, with that id number and update the sort order. Any help with how to grab each value in a foreach loop (or whatever the best way to achieve this may be) would be greatly appreciated.

所以幻灯片的id是以新的顺序返回的,这是我需要的,但是现在我需要用一个id号做一个foreach循环,或者更新排序顺序。任何帮助如何获取foreach循环中的每个值(或任何最好的方法来实现这一点)将不胜感激。

2 个解决方案

#1


2  

Well in this case you will need a seperate array to store your result.

那么在这种情况下,您将需要一个单独的数组来存储您的结果。

$new_sorted_array = [];
foreach($sorted_array as $key => $value)
{
 $new_sorted_array[$key + 1] = $value;
}

Or you can use only one array too.

或者您也可以只使用一个阵列。

$length = count($sorted_array);
for($i=$length ; $i >= 0 ; $i--)
{
 if($i == 0)
 {
   $sorted_array[$i] = 0; //some value if needed to specify.
 }
 $sorted_array[$i] = $sorted_array[i-1];
}

#2


0  

var points = [40, 100, 1, 5, 25, 10]; points.sort(function(a, b){return a-b});

var points = [40,100,1,5,25,10]; points.sort(function(a,b){return a-b});

1,5,10,25,40,100 will be the result of the code above

1,5,10,25,40,100将是上述代码的结果

http://www.w3schools.com/jsref/jsref_sort.asp is a great source for sorting arrays

http://www.w3schools.com/jsref/jsref_sort.asp是排序数组的绝佳来源

EDIT: Didn't notice the Laravel part. There is a method in Laravel called sort and another called sort recursive. Laravel has some good sample code regarding this. laravel.com/docs/5.1/helpers#method-array-sort

编辑:没有注意到Laravel部分。 Laravel中有一种叫做sort的方法,另一种叫做sort recursive。 Laravel有一些很好的示例代码。 laravel.com/docs/5.1/helpers#method-array-sort

#1


2  

Well in this case you will need a seperate array to store your result.

那么在这种情况下,您将需要一个单独的数组来存储您的结果。

$new_sorted_array = [];
foreach($sorted_array as $key => $value)
{
 $new_sorted_array[$key + 1] = $value;
}

Or you can use only one array too.

或者您也可以只使用一个阵列。

$length = count($sorted_array);
for($i=$length ; $i >= 0 ; $i--)
{
 if($i == 0)
 {
   $sorted_array[$i] = 0; //some value if needed to specify.
 }
 $sorted_array[$i] = $sorted_array[i-1];
}

#2


0  

var points = [40, 100, 1, 5, 25, 10]; points.sort(function(a, b){return a-b});

var points = [40,100,1,5,25,10]; points.sort(function(a,b){return a-b});

1,5,10,25,40,100 will be the result of the code above

1,5,10,25,40,100将是上述代码的结果

http://www.w3schools.com/jsref/jsref_sort.asp is a great source for sorting arrays

http://www.w3schools.com/jsref/jsref_sort.asp是排序数组的绝佳来源

EDIT: Didn't notice the Laravel part. There is a method in Laravel called sort and another called sort recursive. Laravel has some good sample code regarding this. laravel.com/docs/5.1/helpers#method-array-sort

编辑:没有注意到Laravel部分。 Laravel中有一种叫做sort的方法,另一种叫做sort recursive。 Laravel有一些很好的示例代码。 laravel.com/docs/5.1/helpers#method-array-sort