自定义键逗号数组分配给一个逗号数组 [英] Custom key comma array allocated to an comma array

查看:175
本文介绍了自定义键逗号数组分配给一个逗号数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数组:

  $ doc_id =(3,5,4,6)
$ requirements2 =(2,0,0,1)

我试着让 $ doc_id 使用以下代码 $ requirements2 的关键元素:

 <?php 

包含../includes/auth.php;
包含../includes/header.php;


$ jat_id = intval($ _ GET ['id']);
$ div_id = intval($ _ GET ['div_id']);


$ order =SELECT * FROM document_assoc WHERE jat_id ='$ jat_id';
$ result = mysql_query($ order);
$ row = mysql_fetch_array($ result);

$ doc_id3 = $ row ['doc_id'];
$ doc_id = explode(,,$ doc_id3);

$要求= $行['要求'];
$ requirements2 = explode(,,$ requirements);

foreach($ doc_id as $ doc_id4){
$ requires3 = array($ doc_id4 => $ requirement3);

foreach($ requirements2 as $ require3){
$ requirements3 = array($ doc_id4 => $ requirement3);

print_r($ requirement3);

}}
包含../includes/footer.php;

?>

不过,我在print_r()上收到以下内容:
$ b数组([3] => 2)数组([3] => 0)数组([3] => 0)数组([5] => 2)数组([5] => 2)数组阵列([4] => 2)阵列([4] => 0)阵列([4] => 0)阵列> 0)Array([6] => 0)Array([6] => 1)



到目前为止,这是最接近的。但是显然还是错的。



从我读过的东西看来,我需要它:

1)
/ pre>

所以之后我可以通过 $ require3 [$ doc_id] 来调用它。 ($ doc_id在调用时是一个不同的变量来源,而不是在这个数组键中设置)。



如果有人能帮我解释我在做什么错2 foreach 语句我会非常感激的更好理解。



亲切的问候


<解决方案

$ require3 = array_combine($ doc_id,$ requirements2);

http://www.php.net/manual/en/function.array-combine.php



演示: http://codepad.org/bDhBw2aZ


I have the following arrays:

$doc_id = (3,5,4,6)
$requirement2 = (2,0,0,1)

I am trying to make $doc_id the key element of $requirement2 using the following code:

<?php

include "../includes/auth.php";
include "../includes/header.php";


$jat_id = intval($_GET['id']);
$div_id = intval($_GET['div_id']);


    $order = "SELECT * FROM document_assoc WHERE jat_id = '$jat_id'";
    $result = mysql_query($order);
    $row = mysql_fetch_array($result);

    $doc_id3 = $row['doc_id'];
    $doc_id = explode(",", $doc_id3);

    $requirement = $row['requirement'];
    $requirement2 = explode(",", $requirement);

    foreach ($doc_id as $doc_id4) {
    $requirement3 = array($doc_id4=>$requirement3);

    foreach ($requirement2 as $requirement3) {
    $requirement3 = array($doc_id4=>$requirement3);

    print_r ($requirement3);

    }}
include "../includes/footer.php";

?>

However I receive the following on the print_r():

Array ( [3] => 2 ) Array ( [3] => 0 ) Array ( [3] => 0 ) Array ( [3] => 1 ) Array ( [5] => 2 ) Array ( [5] => 0 ) Array ( [5] => 0 ) Array ( [5] => 1 ) Array ( [4] => 2 ) Array ( [4] => 0 ) Array ( [4] => 0 ) Array ( [4] => 1 ) Array ( [6] => 2 ) Array ( [6] => 0 ) Array ( [6] => 0 ) Array ( [6] => 1 ) 

Which is the closest I have been so far. But obviously still wrong.

From what I have read I need it to look like this:

Array ( [3] => 2 [5] => 0 [4] => 0 [6] => 1 ) 

So that later on I can call it by $requirement3[$doc_id]. (the $doc_id will be a different variable source when calling it than from setting it within this array key).

If someone could help explain what I am doing wrong with the 2 foreach statements I would greatly appreciate the better understanding.

Kind Regards

解决方案

$requirement3 = array_combine($doc_id,$requirement2);

http://www.php.net/manual/en/function.array-combine.php

Demo: http://codepad.org/bDhBw2aZ

这篇关于自定义键逗号数组分配给一个逗号数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆