如何更改数组中的位置元素?
How change position element in Array?
我在 Laravel 5 有项目。项目工作正常,但我的数组中的排序位置有问题。
我的数组包含错误的位置 ID 和电子邮件:
Array
(
[0] => Array
(
[0] => sda@swqdqwdwq.pll
[1] => 957
[2] => xxxx
[3] => xxxx
[4] => xxxx
[5] => 2021-01-19
[6] => tak
[7] =>
<a href="http://projekt2.test/admin/client/info/957" class="btn btn-xs blue get-info ">
<i class="fa fa-search"></i>
Przeglądaj
</a>
<a href="http://projekt2.test/admin/client/edit/957" class="btn btn-xs blue ">
<i class="fa fa-pencil"></i>
Edytuj
</a>
<a href="http://projekt2.test/admin/client/delete/957" class="btn btn-xs red delete">
<i class="fa fa-trash-o"></i>
Usuń
</a>
<a href="http://projekt2.test/admin/client/deactive/957" class="btn btn-xs yellow">
<i class="fa fa-ban"></i>
Dezaktywuj
</a>
)
[1] => Array
(
[0] => xxxx@op.pl
[1] => 958
[2] => xxxx
[3] => xxxx
[4] => firmowe
[5] => 2021-02-03
[6] => tak
[7] =>
<a href="http://projekt2.test/admin/client/info/958" class="btn btn-xs blue get-info ">
<i class="fa fa-search"></i>
Przeglądaj
</a>
<a href="http://projekt2.test/admin/client/edit/958" class="btn btn-xs blue ">
<i class="fa fa-pencil"></i>
Edytuj
</a>
<a href="http://projekt2.test/admin/client/delete/958" class="btn btn-xs red delete">
<i class="fa fa-trash-o"></i>
Usuń
</a>
<a href="http://projekt2.test/admin/client/deactive/958" class="btn btn-xs yellow">
<i class="fa fa-ban"></i>
Dezaktywuj
</a>
)
...
}
我的php代码:
$data = $this->getList();
print_r($data);
我需要更改职位 ID 和电子邮件(第一个职位 ID,第二个电子邮件)。
我需要结果:
Array
(
[0] => Array
(
[0] => 957
[1] => sda@swqdqwdwq.pll
[2] => xxxx
[3] => xxxx
[4] => xxxx
[5] => 2021-01-19
[6] => tak
[7] =>
<a href="http://projekt2.test/admin/client/info/957" class="btn btn-xs blue get-info ">
<i class="fa fa-search"></i>
Przeglądaj
</a>
<a href="http://projekt2.test/admin/client/edit/957" class="btn btn-xs blue ">
<i class="fa fa-pencil"></i>
Edytuj
</a>
<a href="http://projekt2.test/admin/client/delete/957" class="btn btn-xs red delete">
<i class="fa fa-trash-o"></i>
Usuń
</a>
<a href="http://projekt2.test/admin/client/deactive/957" class="btn btn-xs yellow">
<i class="fa fa-ban"></i>
Dezaktywuj
</a>
)
我怎样才能做到?
我有旧项目 Laravel 5。
请帮帮我
使用此代码,您可以循环数组并更改前 2 个元素的位置:
foreach ($array as $subArray) {
$tmp = $subArray[0];
$subArray[0] = $subArray[1];
$subArray[1] = $tmp;
}
试一试:
<?php
$arr = ['foo', 'foobar@gmail.com', 'myName', 'myInfo', 954];
$arrMask = ['/\d+/', '/\w+@\w+.\w+/', '/*/', '/*/', '/*/'];
print_r(sortArrayByExp($arr, $arrMask));
/**
* Sort a array by RegExp
*
* Sort a array by regular expression
*
* @param array $arr Array to sort
* @param array $arrRegExp Array to postion Regex
*
* @return array Return a new array mirrored by value in $arrRegExp
*
* @throws \InvalidArgumentException
*/
function sortArrayByExp($arr, $arrRegExp)
{
if(! is_array($arr))
throw new \InvalidArgumentException('Value in parameter $arr must be array!', 556);
if(! is_array($arrRegExp))
throw new \InvalidArgumentException('Value in parameter $arrRegExp must be array!', 557);
$newArray = [];
foreach($arrRegExp as $key => $mask)
{
if(! is_string($mask))
throw new \InvalidArgumentException('Each value in $arrRegExp must be string or RegExp', 558);
$response = null;
if(@preg_match($mask, '') !== false)
{
foreach($arr as $posArray => $value)
if(preg_match($mask, $value))
{
$response = $value;
unset($arr[$posArray]);
break;
}
}
if($response !== null)
$newArray[$key] = $response;
}
return $newArray;
}
/* output must be:
Array
(
[0] => 954
[1] => foobar@gmail.com
[2] => foo
[3] => myName
[4] => myInfo
)
*/
此函数按包含 regular expression
的新数组中的每个值对数组进行排序,试试我的示例 here
编辑 1:
您也可以使用 Laravel 的 collect
来对数组进行排序,如下所示:
<?php
require "vendor/autoload.php";
$myCollect = collect(['foo', 'foobar@gmail.com', 'myName', 'myInfo', 954]);
print_r($myCollect->sort(function($above, $below)
{
$belowIsId = is_numeric($below);
$aboveIsId = is_numeric($above);
$belowIsMail = preg_match('/\w+@\w+.[\w\.]+/', $below);
$aboveIsMail = preg_match('/\w+@\w+.[\w\.]+/', $above);
$pos = 0;
if($belowIsId)
$pos = 1;
elseif($belowIsMail)
$pos = 1;
elseif($aboveIsId || $aboveIsMail)
$pos = -1;
return $pos;
})->toArray());
/* output must be:
Array (
[4] => 954
[1] => foobar@gmail.com
[0] => foo
[2] => myName
[3] => myInfo
)
*/
查看更多细节如何使用方法 sort
here,但看起来键被保留了,id 在前但数组中的键仍然相同
我在 Laravel 5 有项目。项目工作正常,但我的数组中的排序位置有问题。 我的数组包含错误的位置 ID 和电子邮件:
Array
(
[0] => Array
(
[0] => sda@swqdqwdwq.pll
[1] => 957
[2] => xxxx
[3] => xxxx
[4] => xxxx
[5] => 2021-01-19
[6] => tak
[7] =>
<a href="http://projekt2.test/admin/client/info/957" class="btn btn-xs blue get-info ">
<i class="fa fa-search"></i>
Przeglądaj
</a>
<a href="http://projekt2.test/admin/client/edit/957" class="btn btn-xs blue ">
<i class="fa fa-pencil"></i>
Edytuj
</a>
<a href="http://projekt2.test/admin/client/delete/957" class="btn btn-xs red delete">
<i class="fa fa-trash-o"></i>
Usuń
</a>
<a href="http://projekt2.test/admin/client/deactive/957" class="btn btn-xs yellow">
<i class="fa fa-ban"></i>
Dezaktywuj
</a>
)
[1] => Array
(
[0] => xxxx@op.pl
[1] => 958
[2] => xxxx
[3] => xxxx
[4] => firmowe
[5] => 2021-02-03
[6] => tak
[7] =>
<a href="http://projekt2.test/admin/client/info/958" class="btn btn-xs blue get-info ">
<i class="fa fa-search"></i>
Przeglądaj
</a>
<a href="http://projekt2.test/admin/client/edit/958" class="btn btn-xs blue ">
<i class="fa fa-pencil"></i>
Edytuj
</a>
<a href="http://projekt2.test/admin/client/delete/958" class="btn btn-xs red delete">
<i class="fa fa-trash-o"></i>
Usuń
</a>
<a href="http://projekt2.test/admin/client/deactive/958" class="btn btn-xs yellow">
<i class="fa fa-ban"></i>
Dezaktywuj
</a>
)
...
}
我的php代码:
$data = $this->getList();
print_r($data);
我需要更改职位 ID 和电子邮件(第一个职位 ID,第二个电子邮件)。 我需要结果:
Array
(
[0] => Array
(
[0] => 957
[1] => sda@swqdqwdwq.pll
[2] => xxxx
[3] => xxxx
[4] => xxxx
[5] => 2021-01-19
[6] => tak
[7] =>
<a href="http://projekt2.test/admin/client/info/957" class="btn btn-xs blue get-info ">
<i class="fa fa-search"></i>
Przeglądaj
</a>
<a href="http://projekt2.test/admin/client/edit/957" class="btn btn-xs blue ">
<i class="fa fa-pencil"></i>
Edytuj
</a>
<a href="http://projekt2.test/admin/client/delete/957" class="btn btn-xs red delete">
<i class="fa fa-trash-o"></i>
Usuń
</a>
<a href="http://projekt2.test/admin/client/deactive/957" class="btn btn-xs yellow">
<i class="fa fa-ban"></i>
Dezaktywuj
</a>
)
我怎样才能做到? 我有旧项目 Laravel 5。
请帮帮我
使用此代码,您可以循环数组并更改前 2 个元素的位置:
foreach ($array as $subArray) {
$tmp = $subArray[0];
$subArray[0] = $subArray[1];
$subArray[1] = $tmp;
}
试一试:
<?php
$arr = ['foo', 'foobar@gmail.com', 'myName', 'myInfo', 954];
$arrMask = ['/\d+/', '/\w+@\w+.\w+/', '/*/', '/*/', '/*/'];
print_r(sortArrayByExp($arr, $arrMask));
/**
* Sort a array by RegExp
*
* Sort a array by regular expression
*
* @param array $arr Array to sort
* @param array $arrRegExp Array to postion Regex
*
* @return array Return a new array mirrored by value in $arrRegExp
*
* @throws \InvalidArgumentException
*/
function sortArrayByExp($arr, $arrRegExp)
{
if(! is_array($arr))
throw new \InvalidArgumentException('Value in parameter $arr must be array!', 556);
if(! is_array($arrRegExp))
throw new \InvalidArgumentException('Value in parameter $arrRegExp must be array!', 557);
$newArray = [];
foreach($arrRegExp as $key => $mask)
{
if(! is_string($mask))
throw new \InvalidArgumentException('Each value in $arrRegExp must be string or RegExp', 558);
$response = null;
if(@preg_match($mask, '') !== false)
{
foreach($arr as $posArray => $value)
if(preg_match($mask, $value))
{
$response = $value;
unset($arr[$posArray]);
break;
}
}
if($response !== null)
$newArray[$key] = $response;
}
return $newArray;
}
/* output must be:
Array
(
[0] => 954
[1] => foobar@gmail.com
[2] => foo
[3] => myName
[4] => myInfo
)
*/
此函数按包含 regular expression
的新数组中的每个值对数组进行排序,试试我的示例 here
编辑 1:
您也可以使用 Laravel 的 collect
来对数组进行排序,如下所示:
<?php
require "vendor/autoload.php";
$myCollect = collect(['foo', 'foobar@gmail.com', 'myName', 'myInfo', 954]);
print_r($myCollect->sort(function($above, $below)
{
$belowIsId = is_numeric($below);
$aboveIsId = is_numeric($above);
$belowIsMail = preg_match('/\w+@\w+.[\w\.]+/', $below);
$aboveIsMail = preg_match('/\w+@\w+.[\w\.]+/', $above);
$pos = 0;
if($belowIsId)
$pos = 1;
elseif($belowIsMail)
$pos = 1;
elseif($aboveIsId || $aboveIsMail)
$pos = -1;
return $pos;
})->toArray());
/* output must be:
Array (
[4] => 954
[1] => foobar@gmail.com
[0] => foo
[2] => myName
[3] => myInfo
)
*/
查看更多细节如何使用方法 sort
here,但看起来键被保留了,id 在前但数组中的键仍然相同