Php 使用动态生成的索引和值归档或平衡数组
基本上我想平衡一个数组 我要平衡的数组是从DB中检索的,可能未知 这是从数据库中检索到的示例数组,它可能会有所不同Php 使用动态生成的索引和值归档或平衡数组,php,arrays,recursion,Php,Arrays,Recursion,基本上我想平衡一个数组 我要平衡的数组是从DB中检索的,可能未知 这是从数据库中检索到的示例数组,它可能会有所不同 <?php $arr_from_DB = array( '1'=>NULL, '2'=>1, '3'=>1, ); $node_width_from_DB = 3; $node_depth_from_DB = 2; ?> //最大容量从$nod
<?php
$arr_from_DB = array(
'1'=>NULL,
'2'=>1,
'3'=>1,
);
$node_width_from_DB = 3;
$node_depth_from_DB = 2;
?>
//最大容量从$node\u width\u from\u DB、$node\u depth\u from\u DB计算得出,并以DB为单位保存,下面模拟宽度=3,深度=2
$max_cap_of_数组=13
//将$arr_from_DB馈送到前端的显示器fxn,这就是结果
//我想要实现的是,我想要将$arr_从_DB转换到下面的数组,所有带负数的数组索引都被动态生成
<?php
$formated_arr = array(
'1'=>NULL,
'2'=>1,
'3'=>1,
'-1'=>1,
'-2'=>2,
'-3'=>2,
'-4'=>2,
'-5'=>3,
'-6'=>3,
'-7'=>3,
'-8'=>-1,
'-9'=>-1,
'-10'=>-1,
);
?>
//将$formatted_arr馈送到前端的显示器fxn,这就是结果
到目前为止我试过什么
<?php
$arr_filled = RECfillMissingDownlineArr($arr_from_DB);
var_dump($arr_filled);
function RECfillMissingDownlineArr($selectedArr, $pointer=1){
$node_width_from_DB = 3;
$node_depth_from_DB = 2;
//Max Capacity was caclulated from $node_width_from_DB, $node_depth_from_DB AND WAS ALSO SAVED in DB , simulated below for width = 3 , depth = 2
$max_cap_of_array = 13;
//var_dump($Allarr);
if($pointer>= $max_cap_of_array){
return $selectedArr;
}
$array_worked = array_count_values($selectedArr);
$array_worked3 = array_keys($selectedArr);
$array_worked2 = array_values($array_worked3);
//$key_p = $array_worked3[$pointer];
foreach($array_worked as $key=>$value){
//echo 'Value nm'.$value.'<br>';
if($value<$node_width_from_DB){
$key_p = $key;
break;
}
}
$array_worked1 = array_keys($selectedArr, $key_p);
for ($i=0; $i <$width ; $i++) {
if(empty($array_worked1[$i])){
$key_pointer = $pointer * -1;
$selectedArr[$key_pointer] = $key;
$pointer++;
return RECfillMissingDownlineArr($selectedArr,$pointer);
}
}
}
OUTPUT NULL, It's not what I want, it's returning null alltogether
?>
请不要大喊大叫。你的问题到底是什么?我想填写缺少的数组索引和值?我已经编辑了我的问题图片,请检查,以便我可以从图片1转换到图片2@JayBlanchard,你看到编辑了吗?请不要大喊大叫。你的问题到底是什么?我想填充缺少的数组索引和值?,我已经编辑了我的问题图片,请检查,以便我可以从图片1转换为图片2@JayBlanchard,你看过《编辑》吗?