Algorithm PHP函数,用于根据变量条件提取多维数组的部分
给定一个多维数组,我正在寻找一种方法,在给定变量(即不同的)条件下提取该数组的各个部分 例如,如果这是我的数据:Algorithm PHP函数,用于根据变量条件提取多维数组的部分,algorithm,multidimensional-array,php-5.3,Algorithm,Multidimensional Array,Php 5.3,给定一个多维数组,我正在寻找一种方法,在给定变量(即不同的)条件下提取该数组的各个部分 例如,如果这是我的数据: array( '0' => array( '0' => 'aaaaaa', '1' => 'bbbbb', '2' => 'ccccc' ), '1' => array( '0' => 'aa2ssa', '1' => 'bb3242bb,
array(
'0' => array(
'0' => 'aaaaaa',
'1' => 'bbbbb',
'2' => 'ccccc'
),
'1' => array(
'0' => 'aa2ssa',
'1' => 'bb3242bb,
'2' => 'ccccc234'
),
'2' => array(
'0' => 'aaa234aa',
'1' => 'b3242b',
'2' => 'cewrcc'
),
(etc)
)
我想能够调用一个函数
function new_array( index, sub_index )
返回一个基于索引和子索引参数的数组。使用相同的数据但不同的参数将返回不同的数据
例1
new_数组(数组(0,2)、(数组(1,2)、数组(0,2)))
预期成果:
array(
'0' => array(
'1' => 'bbbbb',
'2' => 'ccccc'
),
'2' => array(
'0' => 'aaa234aa',
'2' => 'cewrcc'
)
)
array(
'2' => array(
'0' =>'aaa234aa',
'1' => 'b3242b'
)
)
例2
new_数组(数组(2)、(数组(0,2)))
预期成果:
array(
'0' => array(
'1' => 'bbbbb',
'2' => 'ccccc'
),
'2' => array(
'0' => 'aaa234aa',
'2' => 'cewrcc'
)
)
array(
'2' => array(
'0' =>'aaa234aa',
'1' => 'b3242b'
)
)
有人知道怎么做吗?谢谢大家! 为什么不
$a = array('0' =>
array('1' => 'bbbb',
'2' => 'ccccc'),
'2' =>
array('0' => 'aaaa',
'2' => 'cewrcc')
);
为什么要使用函数来做同样的事情?为什么不只是
$a = array('0' =>
array('1' => 'bbbb',
'2' => 'ccccc'),
'2' =>
array('0' => 'aaaa',
'2' => 'cewrcc')
);
为什么要使用函数来执行相同的操作?假设您希望函数处理现有数组并过滤掉数据,那么您可以执行以下操作:
function new_array($original, $topKeys, $subKeys) {
if (count($topKeys) != count($subKeys)) {
return $original;
}
$newArray = array();
foreach ($topKeys as $cTopKey) {
$cSubKeys = array_shift($subKeys);
if (array_key_exists($cTopKey, $original)) {
$newArray[$cTopKey] = array();
foreach ($cSubKeys as $cSubKey) {
if (array_key_exists($cSubKey, $original[$cTopKey])) {
$newArray[$cTopKey][$cSubKey] = $original[$cTopKey][$cSubKey];
}
}
}
}
return $newArray;
}
如果您有PHP v5.1+,并且保证索引可用,那么我相信您可以做得更简单:
function new_array($original, $topKeys, $subKeys) {
$newArray = array_intersect_key($original, array_flip($topKeys));
foreach ($newArray as $cKey => $cSub) {
$cSubKeys = array_shift($subKeys);
$newArray[$cKey] = array_intersect_key($cSub, $cSubKeys);
}
return $newArray;
}
其中的危险是我不知道是否定义了
数组\u intersect\u key()
来保持元素的原始顺序。如果没有,则需要添加进一步的代码,以将子键与原始子键匹配,理想情况下,子键无论如何都将是第一个参数的子数组。假设您希望函数处理现有数组并过滤掉数据,那么您可以这样做:
function new_array($original, $topKeys, $subKeys) {
if (count($topKeys) != count($subKeys)) {
return $original;
}
$newArray = array();
foreach ($topKeys as $cTopKey) {
$cSubKeys = array_shift($subKeys);
if (array_key_exists($cTopKey, $original)) {
$newArray[$cTopKey] = array();
foreach ($cSubKeys as $cSubKey) {
if (array_key_exists($cSubKey, $original[$cTopKey])) {
$newArray[$cTopKey][$cSubKey] = $original[$cTopKey][$cSubKey];
}
}
}
}
return $newArray;
}
如果您有PHP v5.1+,并且保证索引可用,那么我相信您可以做得更简单:
function new_array($original, $topKeys, $subKeys) {
$newArray = array_intersect_key($original, array_flip($topKeys));
foreach ($newArray as $cKey => $cSub) {
$cSubKeys = array_shift($subKeys);
$newArray[$cKey] = array_intersect_key($cSub, $cSubKeys);
}
return $newArray;
}
其中的危险是我不知道是否定义了
数组\u intersect\u key()
来保持元素的原始顺序。如果没有,则需要添加进一步的代码以将子键与原始子键匹配,理想情况下,子键无论如何都将是第一个参数的子数组。另一种@Orbling的解决方案是:
function populateData() // CHANGE ME to populate the info how you please
{
$seed = 'abcdefghijklmnopqrstuvwxyz0123456789';
$_ = ''; $length = rand(5,8);
for ($__ = 0; $__ < $length; $__++) $_ .= substr($seed,rand(0,strlen($seed)),1);
return $_;
}
function new_array()
{
$args = func_num_args();
if ($args == 0)
return FALSE; // flag error if no arguments are passed
$template = func_get_arg(0);
if ($template == null || !is_array($template) || $args < (count($template)+1))
return FALSE; // flag error if we don't have enough information
$resultAry = Array();
$arg = 1;
foreach ($template as $t)
{
$resultArySub = Array();
$templateSub = func_get_arg($arg++);
if ($templateSub == FALSE || !is_array($templateSub))
return FALSE; // error checking for valid input
foreach ($templateSub as $tS)
$resultArySub[$tS] = populateData();
$resultAry[$t] = $resultArySub;
}
return $resultAry;
}
header('Content-Type: text/plain');
echo "your request (or so i understood):\r\n";
$test = new_array(array(0,2),array(1,2),array(0,2));
var_dump($test);
echo "\r\nextra array on end is ignored:\r\n";
$test = new_array(array(4,2),array(1,2),array(0,2),array(3,5));
var_dump($test);
echo "\r\nno data is a FALSE:\r\n";
$test = new_array();
var_dump($test);
echo "\r\ntoo few arguments for what was supplied in first argument is a FALSE:\r\n";
$test = new_array(array(1,2,3),array(4,5),array(6,7));
var_dump($test);
echo "\r\nas long as there's as \"array argument\" for every element of the \"first argument\", this will work:\r\n";
$test = new_array(array(1,2,3,4,5,6,7),array(1),array(2),array(3),array(4),array(5),array(6),array(7));
var_dump($test);
echo "\r\nall arguments must be an array\r\n";
$test = new_array(array(1,2),'not','arrays');
var_dump($test);
@Orbling的另一种解决方案是:
function populateData() // CHANGE ME to populate the info how you please
{
$seed = 'abcdefghijklmnopqrstuvwxyz0123456789';
$_ = ''; $length = rand(5,8);
for ($__ = 0; $__ < $length; $__++) $_ .= substr($seed,rand(0,strlen($seed)),1);
return $_;
}
function new_array()
{
$args = func_num_args();
if ($args == 0)
return FALSE; // flag error if no arguments are passed
$template = func_get_arg(0);
if ($template == null || !is_array($template) || $args < (count($template)+1))
return FALSE; // flag error if we don't have enough information
$resultAry = Array();
$arg = 1;
foreach ($template as $t)
{
$resultArySub = Array();
$templateSub = func_get_arg($arg++);
if ($templateSub == FALSE || !is_array($templateSub))
return FALSE; // error checking for valid input
foreach ($templateSub as $tS)
$resultArySub[$tS] = populateData();
$resultAry[$t] = $resultArySub;
}
return $resultAry;
}
header('Content-Type: text/plain');
echo "your request (or so i understood):\r\n";
$test = new_array(array(0,2),array(1,2),array(0,2));
var_dump($test);
echo "\r\nextra array on end is ignored:\r\n";
$test = new_array(array(4,2),array(1,2),array(0,2),array(3,5));
var_dump($test);
echo "\r\nno data is a FALSE:\r\n";
$test = new_array();
var_dump($test);
echo "\r\ntoo few arguments for what was supplied in first argument is a FALSE:\r\n";
$test = new_array(array(1,2,3),array(4,5),array(6,7));
var_dump($test);
echo "\r\nas long as there's as \"array argument\" for every element of the \"first argument\", this will work:\r\n";
$test = new_array(array(1,2,3,4,5,6,7),array(1),array(2),array(3),array(4),array(5),array(6),array(7));
var_dump($test);
echo "\r\nall arguments must be an array\r\n";
$test = new_array(array(1,2),'not','arrays');
var_dump($test);
您想用随机数据填充函数传递的项吗?需要依赖于传递的参数的新_数组。数据来自哪里?你的问题越清楚,你的答案就越精确……你想用随机数据填充函数传递的项吗?需要依赖于传递的参数的新数组。数据来自哪里?你的问题越清楚,你的答案就越精确…需要依赖于已传递参数的新数组。需要依赖于已传递参数的新数组。数据的来源还有待观察,你的来源和我的一样合理。这充其量只是在黑暗中拍摄。哦,有点语言障碍,缺乏细节。他给了你一个接受的机会,所以我猜这是随机数据,哈哈@奥普林:嘿,我还以为你拿到了呢。我把评论回复给你,看到你被录取了。我重游,我拥有它。我为抢了你的风头而道歉。我保证我会尊重火炬哈哈,我不知道发生了什么,也不知道我们谁是对的。谢谢你纪念我。;-)数据的来源还有待观察,你的来源和我的一样明智。这充其量只是在黑暗中拍摄的。哦,有点语言障碍,缺乏细节。他给了你一个接受的机会,所以我猜这是随机数据,哈哈@奥普林:嘿,我还以为你拿到了呢。我把评论回复给你,看到你被录取了。我重游,我拥有它。我为抢了你的风头而道歉。我保证我会尊重火炬哈哈,我不知道发生了什么,也不知道我们谁是对的。谢谢你纪念我。;-)谢谢你的回答并理解我的问题。谢谢你的回答并理解我的问题。