Php 将数组值放入变量中
我有一个像这样的数组Php 将数组值放入变量中,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我有一个像这样的数组 $document[$doc_id][]= array( $user_id , $doc_type , $id_number , $issuer , $expiry_date , $doc_name
$document[$doc_id][]= array( $user_id
, $doc_type
, $id_number
, $issuer
, $expiry_date
, $doc_name
, $doc_path
, $name
);
它的输出是这样的:
Array (
[15] => Array (
[0] => Array (
[0] => 14
[1] => 1
[2] => 3242424
[3] => 1
[4] => 2016-01-26
[5] => 3242424_1452091784.jpg
[6] => ../documents/
[7] => USA Driving Licence
)
[1] => Array (
[0] => 15
[1] => 1
[2] => 3242424788
[3] => 1
[4] => 2016-01-26
[5] => 3242424_1452045645.jpg
[6] => ../documents/
[7] => US Driving Licence
)
)
)
------------------
$user_id1
$doc_type1
$id_number1
$issuer1
$expiry_date1
$document_name1
$document_path1
$name1
------------------
$user_id2
$doc_type2
$id_number2
$issuer2
$expiry_date2
$document_name2
$document_path2
$name2
使用这个数组,我需要将每个数组的值转换成php变量。
像这样:
Array (
[15] => Array (
[0] => Array (
[0] => 14
[1] => 1
[2] => 3242424
[3] => 1
[4] => 2016-01-26
[5] => 3242424_1452091784.jpg
[6] => ../documents/
[7] => USA Driving Licence
)
[1] => Array (
[0] => 15
[1] => 1
[2] => 3242424788
[3] => 1
[4] => 2016-01-26
[5] => 3242424_1452045645.jpg
[6] => ../documents/
[7] => US Driving Licence
)
)
)
------------------
$user_id1
$doc_type1
$id_number1
$issuer1
$expiry_date1
$document_name1
$document_path1
$name1
------------------
$user_id2
$doc_type2
$id_number2
$issuer2
$expiry_date2
$document_name2
$document_path2
$name2
有人能告诉我如何用php做吗?
希望有人能帮助我。
多谢各位 将变量名存储为数组键,然后可以引用命名键而不是数字,或者遍历数组并打印出键本身
$document[$doc_id][]= array( 'user_id' => $user_id
, 'doc_type' => $doc_type
, 'id_number' => $id_number
, 'issuer' => $issuer
, 'expiry_date' => $expiry_date
, 'doc_name' => $doc_name
, 'doc_path' => $doc_path
, 'name' => $name
);
这里,这正是你想要的。还有,你不应该做的事
<?php
$variables = array("user_id", "doc_type", "id_number", "issuer", "expiry_date", "doc_name", "doc_path", "name");
$i = 1;
foreach($your_array[0] as $array){
for($j = 0; $j < count($variables); $j++){
${$variables[$j] . $i} = $array[$j];
}
$i++;
}
?>
我没有足够的声誉来评论你的第一篇文章,但是: 是的,我通过mysql select的结果创建了该数组–user3733831 您可以使用函数,该函数将返回一个关联数组,这意味着结果集键将是数据库列后面的名称 假设您的DB表如下所示
Array (
[0] => Array (
[user_id] => 14
[doc_type] => 1
[id_number] => 3242424
[issuer] => 1
[expiry_date] => 2016-01-26
[doc_name] => 3242424_1452091784.jpg
[doc_path] => ../documents/
[name] => USA Driving Licence
)
)
用户id |单据类型|单据编号|发卡机构|到期日|单据路径|名称
您的结果集将如下所示
Array (
[0] => Array (
[user_id] => 14
[doc_type] => 1
[id_number] => 3242424
[issuer] => 1
[expiry_date] => 2016-01-26
[doc_name] => 3242424_1452091784.jpg
[doc_path] => ../documents/
[name] => USA Driving Licence
)
)
编辑
好的…检查了你的代码,所以
$stmt->bind_param('i', $edit);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result(
$doc_id
, $user_id
, $veryfy_doc_type
, $id_number
, $document_issuer
, $expiry_date
, $document_name
, $document_path
, $doc_name
);
$document = array();
while($stmt->fetch())
你可以用
$stmt->bind_param("i", $edit);
$stmt->execute();
/* instead of bind_result: */
$result = $stmt->get_result();
/* now you can fetch the results into an array - NICE */
while ($row = $result->fetch_assoc()) {
//do something
// here you can access each result as $row[<something>]
// ex: echo $row['user_id'] will print 14
}
好的,所以不要在事后修改数组,而是在处理查询结果时根据需要构建它,如下所示。当然,您可以根据需要调用数组中的条目
while($stmt->fetch()) {
if(!isset($document[$doc_id])) {
$document[$doc_id][]= array( 'userid' => $user_id,
'veryfy_doc_type' => $veryfy_doc_type,
'id_number' => $id_number,
'document_issue' => $document_issuer,
'expiry_date' => $expiry_date,
'document_name' => $document_name,
'document_path' => $document_path,
'doc_name' => $doc_name);
}
}
然后,它只是在数组上运行foreach循环的一种情况,但现在可以使用合理的名称
foreach ($document[$doc_id] as $id => $doc ) {
echo "id = $id veryfy_doc_type = " . $doc['veryfy_doc_type'];
}
我不知道你为什么要这么做。但在PHP中,可以使用变量。例如,假设您有一个字符串'my_var',那么您可以创建变量$my_var,如下所示:
$my_string = 'my_var';
$$my_string = 'some value'; // same as: $my_var = 'some_value';
对于您的特定请求,以下是如何将数组值提取到变量中,假设您只需要$doc_id的值:
// Replace the [...] with the proper variable names (obviously)...
$names = ['user_id', 'doc_type', 'id_number', [...], 'name'];
foreach ( $document[$doc_id] as $id => $values )
{
$id++; // 0 becomes 1, 1 becomes 2, 2 becomes...
foreach ( $values as $key => $value )
{
// Assume that this is the first value in your array above,
// then $key is 0, $id is 1 and $value is 14, so the following
// code is the equivalent of typing in: $user_id1 = 14;
${$names[$key].$id} = $value;
}
}
为什么不把它保存在一个数组中呢?你有一个非常好的数组,为什么你需要将数据移动到标量变量,并将你的内存需求翻一番呢。这就是为什么我在寻找这样的解决方案。问题:是你创建了那个数组,还是你只是在处理一个你无法控制的数组?这是数据库查询的结果吗?然后使用mysql函数返回数据bu名称,而不仅仅是索引。显示您的查询和将结果集卸载到数组中的代码不确定这是否真的有帮助muchIt可能不是他想要的,但这将取决于他以后对数组的处理。@Pamblam现在呢?应该是$array[$i][$j]。@Pamblam这是一个包含数组的单个数组数组,对吗?哦,我没有注意到[0]…你是对的。虽然更好的答案是删除[0]并按照我的建议更改$array,这样它将循环所有数组,而不仅仅是其中一个。@Pamblam那么我最好删除它。。。这和你的回答没什么不同,我一开始没注意到:谢谢你的回答,你能告诉我如何使用这些值吗?例如,如何使用veryfy_doc_type 1和2?我尝试了类似这样的foreach$document[$doc_id]作为$id=>$doc{$id_number=$doc['id_number'];}echo$id_number;我终于找到了这样的解决办法——欢迎你的想法。非常感谢。