[英]Associative array
I have two arrays:我有两个数组:
arr1([0]=>1400.20
[1]=>1630.32
[2]=>2531.30
[3]=>9845.62)
arr2([0]=>150
[1]=>134
[2]=>901
[3]=>631)
and I have combined them as:我将它们组合为:
arr3 ([150]=>1400.20
[134]=>1630.32
[901]=>2531.30
[631]=>9845.62)
But I need to convert arr3
to this form :但我需要将
arr3
转换为这种形式:
arr4 ('150'=>1400.20,
'134'=>1630.32,
'901'=>2531.30,
'631'=>9845.62)
Is there any way to convert arr3
to this form?有没有办法将
arr3
转换为这种形式?
I use two PHP program to create a graph by phpMyGraph The first program takes information from the database and creates two arrays, arr1
, arr2
.The values in arr1
will be the x-axis and the values in arr2 will be y-axis.我使用两个 PHP 程序通过phpMyGraph创建一个图形第一个程序从数据库中获取信息并创建两个数组,
arr1
, arr2
。 arr1
值将是 x 轴, arr2 中的值将是 y 轴。 After combining them组合它们之后
$arr3 = array_combine($arr2, $arr1)
I use 'serialize' to send arr3
to the other program.我使用“序列化”将
arr3
发送到另一个程序。 But it doesn't show the graph and shows this message:但它不显示图形并显示此消息:
exception 'Exception' with message 'Provide data is not an array.'
带有消息“提供数据不是数组”的异常“异常”。 in blah/blah/phpMyGraph4.0.php:801 Stack trace: ....
在 blah/blah/phpMyGraph4.0.php:801 堆栈跟踪:....
I think maybe I need to use arr4 format as it is in the exmple我想也许我需要使用 arr4 格式,因为它在示例中
foreach( $arr3 as &$key => &$value ){
$key = '' . $key . '';
}
But as others have said, PHP has very weak type standards.但正如其他人所说,PHP 的类型标准非常薄弱。
$arr3 = array();
foreach( $arr1 as $key => $value ){
// arr1 [0]=>150
// arr2 [0]=>1500
// arr3[150] => 1500
$arr3[$value] = $arr2[$key];
}
for avoid memory usage为避免内存使用
foreach( $arr1 as $key => $value ){
// arr1 [0]=>150
// arr2 [0]=>1500
// arr2 [150] => 1500
// delete arr2[0];
$arr2[$value] = $arr2[$key];
unset($arr2[$key]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.