Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将mysql结果数组自动循环到JSON数组或XML密钥复制中_Php_Arrays - Fatal编程技术网

Php 将mysql结果数组自动循环到JSON数组或XML密钥复制中

Php 将mysql结果数组自动循环到JSON数组或XML密钥复制中,php,arrays,Php,Arrays,我正在从一个结果返回一个mysql数组,并希望自动将结果输出到JSON返回或XML(包括除此之外的其他内容)的一部分,并希望使用一个简单的循环函数进行输出。除了重复数组索引/键外,它工作得非常好。例如: [23] => 21.00 [cost] => 21.00 [24] => 0.00 [costproduct] => 0.00 [25] => 21.00 [costtotal] => 21.00 我希望它是干净的,没有重复。我用来循环的简单代码是: fu

我正在从一个结果返回一个mysql数组,并希望自动将结果输出到JSON返回或XML(包括除此之外的其他内容)的一部分,并希望使用一个简单的循环函数进行输出。除了重复数组索引/键外,它工作得非常好。例如:

[23] => 21.00
[cost] => 21.00
[24] => 0.00
[costproduct] => 0.00
[25] => 21.00
[costtotal] => 21.00
我希望它是干净的,没有重复。我用来循环的简单代码是:

function array_loop_output( $array, $format = 'json', $output = '' ){

    if(is_array($array)){

        foreach($array as $key => $value){

            if(is_array($value)){

                if($format == 'xml'){

                    $output .= '<' . $key . '>';

                    $output .= array_loop_output( $value, $format );

                    $output .= '</' . $key . '>';

                }else{

                    $output[$key] = array_loop_output( $value, $format );

                }

            }else{

                if($format == 'xml'){

                    if(is_numeric($value)){

                        $output .= xmltagstring(array('tag'=>$key,'value'=>$value))."\n";

                    }else{

                        $output .= xmltagstring(array('tag'=>$key,'value'=>$value,'cdata'=>true))."\n";

                    }

                }else{ // json

                    if(is_numeric($value)){

                        $output[$key] = $value;

                    }else{

                        $output[$key] = forjson($value);

                    }   
                }
            }
        }
    }

    return $output;

}
函数数组\循环\输出($array,$format='json',$output=''){
if(is_数组($array)){
foreach($key=>$value的数组){
if(是_数组($value)){
如果($format='xml'){
$output.='';
$output.=数组\循环\输出($value,$format);
$output.='';
}否则{
$output[$key]=数组\循环\输出($value,$format);
}
}否则{
如果($format='xml'){
如果(是数值($value)){
$output.=xmltagstring(数组('tag'=>$key,'value'=>$value))。“\n”;
}否则{
$output.=xmltagstring(数组('tag'=>$key,'value'=>$value,'cdata'=>true))。“\n”;
}
}else{//json
如果(是数值($value)){
$output[$key]=$value;
}否则{
$output[$key]=forjson($value);
}   
}
}
}
}
返回$output;
}

有没有一个干净的方法来做到这一点,因为我认为我有我的金发时刻之一?非常感谢。

同时具有数字键和关联键的结果类型是由于使用了一个fetch数组函数:

  • mysqli\u result::fetch\u array()
  • PDO::FETCH_两者
  • mysql\u fetch\u array()
如果使用
assoc
而不是
array
,结果将只包含[string]关联键:

  • mysqli\u结果::fetch\u assoc()
  • PDO::获取助理
  • mysql\u fetch\u assoc()

数字键来自哪里?看起来您正在使用
mysql\u fetch\u array()
mysqli\u result::fetch\u array()
。相反,你应该获取
assoc
而不是
array
。这正是我要做的。我知道我在做傻事。谢谢你指出这一点,我现在觉得自己像个白痴,是的,它现在起作用了。这个问题现在解决了吗?是的,我想给你一个答案,作为一个更详细的答案:)