0

我很难根据从 mysql 数据库中获取的查询构建多维数组。目的是为 json 编码准备结果。这里有问题。

我的目标是:

Array ( 
    [68] => Array ( 
        [0] => Array ( 
            [id] => 64 
            [description] => yada, yada, yada... ) 
        [1] => Array ( 
            [id] => 65 
            [description] => yada, yada, yada...) 
        [2] => Array ( 
            [id] => 66 
            [description] => yada, yada, yada... ) ) 
    [69] => Array ( 
        [0] => Array ( 
            [id] => 64 
            [description] => yada, yada, yada... ) 
        [1] => Array ( 
            [id] => 65 
            [description] => yada, yada, yada...) 
        [2] => Array ( 
            [id] => 66 
            [description] => yada, yada, yada... ) ) 
...etc
)

从:

//this is a list of unknown length
$array_ids = (68, 69, 70, etc... );   // or:

Array ( 
    [0] => 68 
    [1] => 69
    [2] => 70 
    [3] => 71 
    etc..  
)


//this is a known length 
$array_contents = ( array ( array ( [id], [description]) );  // or:

Array ( 
    [0] => Array ( 
        [id] => 64 
        [description] =>yada, yada, yada... ) 
    [1] => Array ( 
        [id] => 65 
        [description] => yada, yada, yada... ) 
    [2] => Array ( 
        [id] => 66 
        [description] => yada, yada, yada... ) )

我的尝试:

foreach($array_ids as $row){
    $result = array($row=>array());
    foreach($array_contents as $key => $value){
        $result [$row][$key] = $value;
    }
}

结果:

Array ( 
    [68] => Array ( 
        [0] => Array ( 
            [id] => 64 
            [description] => yada, yada, yada... ) 
        [1] => Array ( 
            [id] => 65 
            [description] => yada, yada, yada...) 
        [2] => Array ( 
            [id] => 66 
            [description] => yada, yada, yada... ) ) 

)

......这就是它停止的地方。它不会继续到 69、70 等的下一行 id...

我究竟做错了什么?

4

4 回答 4

0

好的,重新阅读问题后,请尝试以下操作:

$outputArray = array();

foreach( $array_ids as $id ){
  $outputArray[$id] = $array_contents;
}
于 2012-09-20T14:57:02.243 回答
0

尝试以更短的方式更改您的代码

$result = array();
foreach($array_ids as $row){
    foreach($array_contents as $key => $value){
        $result [$row][$key] = $value;
    }
}
于 2012-09-20T14:57:14.973 回答
0

这条线似乎是问题所在:

$result = array($row=>array());

每次通过$array_ids它都会完全清除$result并用来自 的单个项目重新填充它$array_contents,而不是将其附加到末尾。

用它替换它应该可以工作:

$result[$row] = array();

这是一个完整的工作版本:

$array_ids = array(68,69,70);

$array_contents = array(
    array( 
        'id' => 64, 
        'description' => 'yada, yada, yada...'
    ),
  array( 
        'id' => 65,
        'description' => 'yada, yada, yada2...'
    ),
    array( 
    'id' => 66,
    'description' => 'yada, yada, yada3...'
    ),
);

foreach($array_ids as $row){
    $result[$row] = array();
    foreach($array_contents as $key => $value){
        $result [$row][$key] = $value;
    }
}

var_dump($result);
?>

这是输出:

array(3) {
  [68]=>
  array(3) {
    [0]=>
    array(2) {
      ["id"]=>
      int(64)
      ["description"]=>
      string(19) "yada, yada, yada..."
    }
    [1]=>
    array(2) {
      ["id"]=>
      int(65)
      ["description"]=>
      string(20) "yada, yada, yada2..."
    }
    [2]=>
    array(2) {
      ["id"]=>
      int(66)
      ["description"]=>
      string(20) "yada, yada, yada3..."
    }
  }
  [69]=>
  array(3) {
    [0]=>
    array(2) {
      ["id"]=>
      int(64)
      ["description"]=>
      string(19) "yada, yada, yada..."
    }
    [1]=>
    array(2) {
      ["id"]=>
      int(65)
      ["description"]=>
      string(20) "yada, yada, yada2..."
    }
    [2]=>
    array(2) {
      ["id"]=>
      int(66)
      ["description"]=>
      string(20) "yada, yada, yada3..."
    }
  }
  [70]=>
  array(3) {
    [0]=>
    array(2) {
      ["id"]=>
      int(64)
      ["description"]=>
      string(19) "yada, yada, yada..."
    }
    [1]=>
    array(2) {
      ["id"]=>
      int(65)
      ["description"]=>
      string(20) "yada, yada, yada2..."
    }
    [2]=>
    array(2) {
      ["id"]=>
      int(66)
      ["description"]=>
      string(20) "yada, yada, yada3..."
    }
  }
}
于 2012-09-20T14:54:15.467 回答
0

我尝试使用以下数组结构和逻辑它的工作原理,我的逻辑有点不同

$array = array(68,69,70);
$nextArray = array(
    array(
        'id'=>64,
        'desc'=>'test'
    ),
    array(
        'id'=>65,
        'desc'=>'test'
    ),
    array(
        'id'=>66,
        'desc'=>'test'
    ),
    array(
        'id'=>67,
        'desc'=>'test'
    ),
);
$tempArray = array();
foreach($array as $value){
    foreach($nextArray as $key => $value1){
        $tempArray[$value][$key] = $value1;
    }
}
var_dump($tempArray);

输出 :

array
  68 => 
    array
      0 => 
        array
          'id' => int 64
          'desc' => string 'test' (length=4)
      1 => 
        array
          'id' => int 65
          'desc' => string 'test' (length=4)
      2 => 
        array
          'id' => int 66
          'desc' => string 'test' (length=4)
      3 => 
        array
          'id' => int 67
          'desc' => string 'test' (length=4)
  69 => 
    array
      0 => 
        array
          'id' => int 64
          'desc' => string 'test' (length=4)
      1 => 
        array
          'id' => int 65
          'desc' => string 'test' (length=4)
      2 => 
        array
          'id' => int 66
          'desc' => string 'test' (length=4)
      3 => 
        array
          'id' => int 67
          'desc' => string 'test' (length=4)
  70 => 
    array
      0 => 
        array
          'id' => int 64
          'desc' => string 'test' (length=4)
      1 => 
        array
          'id' => int 65
          'desc' => string 'test' (length=4)
      2 => 
        array
          'id' => int 66
          'desc' => string 'test' (length=4)
      3 => 
        array
          'id' => int 67
          'desc' => string 'test' (length=4)
于 2012-09-20T14:56:06.073 回答