Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.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 联合数组_Php_Mysql - Fatal编程技术网

Php 联合数组

Php 联合数组,php,mysql,Php,Mysql,我需要按照[id](oper-%%%)在数组2中的显示顺序重新排列数组1,我不太确定如何处理这个问题 数组1 Array ( [0] => Array ( [id] => oper-4c597644-402490ee [amount] => 17498.5 ) [1] => Array ( [id] => oper-4f30019a-27f27473 [amount] => 10383.5 ) [2] => Array ( [id] => oper

我需要按照[id](oper-%%%)在数组2中的显示顺序重新排列数组1,我不太确定如何处理这个问题

数组1

Array ( [0] => Array ( [id] => oper-4c597644-402490ee [amount] => 17498.5 ) [1] => Array ( [id] => oper-4f30019a-27f27473 [amount] => 10383.5 ) [2] => Array ( [id] => oper-4bceffd1-21e0af5b [amount] => 6277 ) [3] => Array ( [id] => oper-4f300d33-de9592e3 [amount] => 11382 ) [4] => Array ( [id] => oper-4c236420-0b11e945 [amount] => 14759 ) [5] => Array ( [id] => oper-50f6e4ad-9effbec7 [amount] => 3058 ) [6] => Array ( [id] => oper-4f05a90b-03b379f9 [amount] => 12112.5 ) [7] => Array ( [id] => oper-qtgjvw8y-1uqtw058 [amount] => 10023 ) [8] => Array ( [id] => oper-52657816-3d6516e2 [amount] => 3495 ) )
阵列2

Array ( [0] => Array ( [0] => Bob [1] => oper-4bceffd1-21e0af5b ) [1] => Array ( [0] => Bob [1] => oper-4c236420-0b11e945 ) [2] => Array ( [0] => Bob [1] => oper-4c597644-402490ee ) [3] => Array ( [0] => Bob [1] => oper-4f05a90b-03b379f9 ) [4] => Array ( [0] => Bob [1] => oper-4f30019a-27f27473 ) [5] => Array ( [0] => Bob [1] => oper-4f300d33-de9592e3 ) [6] => Array ( [0] => Bob [1] => oper-50f6e4ad-9effbec7 ) [7] => Array ( [0] => Bob [1] => oper-52657816-3d6516e2 ) [8] => Array ( [0] => Bob [1] => oper-qtgjvw8y-1uqtw058 ) [9] => Array ( [0] => Empty [1] => ) [10] => Array ( [0] => Upg. [1] => ) [11] => Array ( [0] => Ren. [1] => ) )
鲍勃只是一个“名字”的例子 这是我到目前为止的代码(我知道这是不干净的,因为这不是用PDO完成的,我现在只是想让它工作起来):

非常感谢您的帮助。关联数组不是我喜欢的

基本上,我正在寻找一个如下所示的阵列:

Array ( [name] => Bob [id] => oper-%%%%%%%%%%%%%% [amount] => $$$$$$$$$$$ )...ect...ect...
原油,但应该有效:

$toSort = array(
    array ('id' => 'oper-4c597644-402490ee', 'amount' => 17498.5,),
    array ('id' => 'oper-4f30019a-27f27473', 'amount' => 10383.5,),
    array ('id' => 'oper-4bceffd1-21e0af5b', 'amount' => 6277,),
    array ('id' => 'oper-4f300d33-de9592e3', 'amount' => 11382,),
    array ('id' => 'oper-4c236420-0b11e945', 'amount' => 14759,),
    array ('id' => 'oper-50f6e4ad-9effbec7', 'amount' => 3058,),
    array ('id' => 'oper-4f05a90b-03b379f9', 'amount' => 12112.5,),
    array ('id' => 'oper-qtgjvw8y-1uqtw058', 'amount' => 10023,),
    array ('id' => 'oper-52657816-3d6516e2', 'amount' => 3495,),
);

$assigned = array (
    array ('Bob','oper-4bceffd1-21e0af5b'),
    array ('Bob','oper-4c236420-0b11e945'),
    array ('Bob','oper-4c597644-402490ee'),
    array ('Bob','oper-4f05a90b-03b379f9'),
    array ('Bob','oper-4f30019a-27f27473'),
    array ('Bob','oper-4f300d33-de9592e3'),
    array ('Bob','oper-50f6e4ad-9effbec7'),
    array ('Bob','oper-52657816-3d6516e2'),
    array ('Bob','oper-qtgjvw8y-1uqtw058'),
);

$sorted = assignData($toSort, $assigned);

var_dump($sorted);

function assignData($raw, $order)
{
    $returnArray = array();
    foreach($order as $entry):
        foreach($raw as $rawEntry):
            if ($rawEntry['id'] == $entry[1]):
                $temp = array(
                    'name' => $entry[0],
                    'oper' => $rawEntry['id'],
                    'amount' => $rawEntry['amount'],
                );
                $returnArray[] = $temp;
            endif;
        endforeach;
    endforeach;
    return $returnArray;
}
试试这个

<?php

$data = [
    ['id' => 'oper-4c597644-402490ee', 'amount' => 17498.5],
    ['id' => 'oper-4f30019a-27f27473', 'amount' => 10383.5],
    ['id' => 'oper-4bceffd1-21e0af5b', 'amount' => 6277],
    ['id' => 'oper-4f300d33-de9592e3', 'amount' => 11382],
    ['id' => 'oper-4c236420-0b11e945', 'amount' => 14759],
    ['id' => 'oper-50f6e4ad-9effbec7', 'amount' => 3058],
    ['id' => 'oper-4f05a90b-03b379f9', 'amount' => 12112.5],
    ['id' => 'oper-qtgjvw8y-1uqtw058', 'amount' => 10023],
    ['id' => 'oper-52657816-3d6516e2', 'amount' => 3495]
];

$names = [
    ['BobA', 'oper-4bceffd1-21e0af5b'],
    ['BobB', 'oper-4c236420-0b11e945'],
    ['BobC', 'oper-4c597644-402490ee'],
    ['BobD', 'oper-4f05a90b-03b379f9'],
    ['BobE', 'oper-4f30019a-27f27473'],
    ['BobF', 'oper-4f300d33-de9592e3'],
    ['BobG', 'oper-50f6e4ad-9effbec7'],
    ['BobH', 'oper-52657816-3d6516e2'],
    ['BobI', 'oper-qtgjvw8y-1uqtw058'],
    ['Empty', ''],
    ['Upg.', ''],
    ['Ren.', '']
];

$idMappedNames = array_column($names, 0, 1);

$summary = array_reduce($data, function($carry, $item) use(&$idMappedNames) {
    if (!array_key_exists($item['id'], $carry)) {
        $carry[$item['id']] = [
            'name' => $idMappedNames[$item['id']],
            'amount' => 0
        ];
    }

    $carry[$item['id']]['amount'] += $item['amount'];

    return $carry;
}, []);

您可以在一个SQL查询中连接这些信息

$result = mysql_query("SELECT a.*,u.OperatorName,u.MonthlyGoal
   FROM tblUserPayments a LEFT JOIN tblOperatorGoals u ON a.OperatorID = u.OperatorID
   WHERE a.ChargeAmount IS NOT NULL 
   AND a.PaymentStatus='OK' 
   AND a.PaymentDate LIKE '$currentDate%' 
   AND u.MonthlyGoal LIKE '$currentDate%' " );
while ($row = mysql_fetch_assoc($result)) {
  $wantedArray[] = array(
      'OperatorName' => $row['OperatorName'], 
      'MonthlyGoal'=>$row['MonthlyGoal'],
      'ChargeAmount' => $row['ChargeAmount'], 
      'OperatorID' => $row['OperatorID']);
}

但正如最初提出的问题所说:

$desiredArray = array();
$id_to_amount = array();
foreach($array1 as $x)
   $id_to_amount[$x['id']] = $x['amount'];
foreach($array2 as $x)
{
   $id = $x[1];
   $name = $x[0];
   $amount = -1;
   if(isset($id_to_amount[$id]))
      $amount = $id_to_amount[$id];
   $desiredArray[] = array('id'=>$id,'name'=>$name,'amount'=>$amount );
}

我刚刚编辑了我的帖子,但似乎仍无法使其正常工作。我已编辑了我的帖子,但似乎仍无法使其正常工作。我刚刚编辑了我的帖子,以包含需要提取信息的附加表。操作员名称是从tblOperatorGoals提取的。很抱歉我遗漏了这一点。@Juan好的,我将我的联接表解决方案与tblOperatorGoals表和MonthlyGoal字段合并。我感谢您的帮助。唯一的问题是“ChargeAmount”位于表“tblUserPayments”中,因此我必须同时使用“tblOperatorGoals”和“tblUserPayments”来获取必要的信息。我不担心包含“MonthlyGoal”变量。是的,这就是为什么要将它们连接在一起。将所有需要的信息放在一个地方,而不是以后需要另外合并的两个地方。连接两个SQL表将创建一个(虚拟)表,其中包含两个SQL表的数据。
$desiredArray = array();
$id_to_amount = array();
foreach($array1 as $x)
   $id_to_amount[$x['id']] = $x['amount'];
foreach($array2 as $x)
{
   $id = $x[1];
   $name = $x[0];
   $amount = -1;
   if(isset($id_to_amount[$id]))
      $amount = $id_to_amount[$id];
   $desiredArray[] = array('id'=>$id,'name'=>$name,'amount'=>$amount );
}