在PHP中如何按键或值拆分关联

时间:2018-02-13 14:00:58

标签: php arrays

我有一个数组,我需要按键(TFvchBBAAA/BDQC1HlAAAA4==)或值(PNQ_NAG)拆分该数组。我怎么能以最好的方式做到这一点?

这是数组:

$arr=array(
    ['TFvchBBAAA/BBQC1HlAAAA1=='] => 'DEL_NAG',
    ['TFvchBBAAA/BBQC1HlAAAA2=='] => 'NAG_GOI',
    ['TFvchBBAAA/BBQC1HlAAAA98=='] => 'GOI_AMD',
    ['TFvchBBAAA/BBQC1HlAAAA981=='] => 'AMD_MAA',
    ['TFvchBBAAA/BBQC1HlAAAA3=='] => 'MAA_PNQ',
    ['TFvchBBAAA/BDQC1HlAAAA4=='] => 'PNQ_NAG',
    ['TFvchBBAAA/BDQC1HlAAAA6=='] => 'NAG_DEL',
  )

我有key:TFvchBBAAA/BDQC1HlAAAA4==及其值PNQ_NAG的值,我需要将其拆分。所需的解决方案应该是这样的:

$arr=array(

    [1]=>array(
        ['TFvchBBAAA/BBQC1HlAAAA1=='] => 'DEL_NAG',
        ['TFvchBBAAA/BBQC1HlAAAA2=='] => 'NAG_GOI',
        ['TFvchBBAAA/BBQC1HlAAAA98=='] => 'GOI_AMD',
        ['TFvchBBAAA/BBQC1HlAAAA981=='] => 'AMD_MAA',
        ['TFvchBBAAA/BBQC1HlAAAA3=='] => 'MAA_PNQ',
     ),
    [2]=>array(
        ['TFvchBBAAA/BDQC1HlAAAA4=='] => 'PNQ_NAG',
        ['TFvchBBAAA/BDQC1HlAAAA6=='] => 'NAG_DEL',
    )
)

感谢。

2 个答案:

答案 0 :(得分:1)

试试这个:

$arr = array(
    'TFvchBBAAA/BBQC1HlAAAA1==' => 'DEL_NAG',
    'TFvchBBAAA/BBQC1HlAAAA2==' => 'NAG_GOI',
    'TFvchBBAAA/BBQC1HlAAAA98==' => 'GOI_AMD',
    'TFvchBBAAA/BBQC1HlAAAA981==' => 'AMD_MAA',
    'TFvchBBAAA/BBQC1HlAAAA3==' => 'MAA_PNQ',
    'TFvchBBAAA/BDQC1HlAAAA4==' => 'PNQ_NAG',
    'TFvchBBAAA/BDQC1HlAAAA6==' => 'NAG_DEL',
  );

$result = array();
$part = 1;

foreach($arr as $key=>$value){
    if ($key == 'TFvchBBAAA/BDQC1HlAAAA4==' || $value == "PNQ_NAG") {
        $part = 2;
    }
    $result[$part][$key] = $value;
}

print_r($result);

结果:

Array ( 
[1] => Array ( 
    [TFvchBBAAA/BBQC1HlAAAA1==] => DEL_NAG 
    [TFvchBBAAA/BBQC1HlAAAA2==] => NAG_GOI 
    [TFvchBBAAA/BBQC1HlAAAA98==] => GOI_AMD 
    [TFvchBBAAA/BBQC1HlAAAA981==] => AMD_MAA 
    [TFvchBBAAA/BBQC1HlAAAA3==] => MAA_PNQ 
  ) 
[2] => Array ( 
    [TFvchBBAAA/BDQC1HlAAAA4==] => PNQ_NAG 
    [TFvchBBAAA/BDQC1HlAAAA6==] => NAG_DEL 
  )
)

答案 1 :(得分:0)

有点长但可能有用

#include "mainwindow.h"
#include <QTest>
MainWindow::MainWindow(QWidget *parent)
 : QMainWindow(parent)
{

m_button = new QPushButton("My Button", this);
m_button->setFixedSize(100,50);


m_menu = new QMenu("&Menu");
m_menu->addAction("&test1");
m_menu->addAction("&test2");
m_menu->addAction("&test3");
m_menu->addAction("&test4");
m_menu->addAction("&test5");
m_menu->addAction("&test6");


 connect(m_button, SIGNAL (released()), this, SLOT (handleButton()));

}

void MainWindow::handleButton()
{
    m_menu->exec(m_button->mapToGlobal(QPoint(20,20)));
    QTest::qWait(2000);

    for(int i = 0 ;i<=5;i++){
      QTest::keyClick(m_menu, Qt::Key::Key_Down);
      QTest::qWait(1000);
      QTest::mouseMove(m_menu, QPoint(0,20));
      QTest::qWait(1000);

    }


}

MainWindow::~MainWindow()
{

}