Php,使用第一个键和第二个键来连接两个数组

时间:2021-07-26 12:21:12

I have two arrays, one is generated from POST method, and the other one is from database.

我有两个数组,一个是从POST方法生成的,另一个是从数据库生成的。

Post

Array
(
    [browser] => Array
        (
            [0] => Firefox
            [1] => Chrome
        )

    [firstname] => Array
        (
            [0] => John
        )

    [lastname] => Array
        (
            [0] => Doe
        )
)

Database

Array
(
    [0] => stdClass Object
        (
            [title] => Browser Name
            [name] => browser
        )

    [1] => stdClass Object
        (
            [title] => First Name
            [name] => firstname

        )

    [2] => stdClass Object
        (
            [title] => Last Name
            [name] => lastname

        )

)

I would like to be able to join both using keys from POST array and name value from Database array.

我希望能够使用POST数组中的键和Database数组中的名称值来加入。

The result should look like:

结果应如下所示:

Array
(
    [0] => stdClass Object
        (
            [title] => Browser Name
            [name] => browser
            [posted] => Array (
                  [0] => Firefox
                  [1] => Chrome
            )
        )

    [1] => stdClass Object
        (
            [title] => First Name
            [name] => firstname
            [posted] => John

        )

    [2] => stdClass Object
        (
            [title] => Last Name
            [name] => lastname
            [posted] => Doe

        )

)

It's important to keep the same order from Post array.

从Post数组保持相同的顺序非常重要。

1 个解决方案

#1


Try this:

$postArray = array(
    'browser' => array('Firefox', 'Chrome'),
    'firstname' => array('John'),
    'lastname' => array('Doe')
);

$arrayObj1 = new StdClass();
$arrayObj1->title = 'Browser Name';
$arrayObj1->name = 'browser';

$arrayObj2 = new StdClass();
$arrayObj2->title = 'First Name';
$arrayObj2->name = 'firstname';

$arrayObj3 = new StdClass();
$arrayObj3->title = 'Last Name';
$arrayObj3->name = 'lastname';

$dbArray = array($arrayObj1, $arrayObj2);

$finalArray = array();
foreach ($dbArray AS $key => $dbObject) {
    $finalArray[$key] = new StdClass();
    $finalArray[$key]->title = $dbObject->title;
    $finalArray[$key]->name = $dbObject->name;
    if (count($postArray[$dbObject->name]) > 1) {
        $finalArray[$key]->posted = $postArray[$dbObject->name];
    } else {
        $finalArray[$key]->posted = $postArray[$dbObject->name][0];
    }
}

var_dump($finalArray);

Tested there: http://sandbox.onlinephpfunctions.com/code/7bf0fa0fbdb086ae0cc8bb21a78261ca8c05c08e

在那里测试:http://sandbox.onlinephpfunctions.com/code/7bf0fa0fbdb086ae0cc8bb21a78261ca8c05c08e

#1


Try this:

$postArray = array(
    'browser' => array('Firefox', 'Chrome'),
    'firstname' => array('John'),
    'lastname' => array('Doe')
);

$arrayObj1 = new StdClass();
$arrayObj1->title = 'Browser Name';
$arrayObj1->name = 'browser';

$arrayObj2 = new StdClass();
$arrayObj2->title = 'First Name';
$arrayObj2->name = 'firstname';

$arrayObj3 = new StdClass();
$arrayObj3->title = 'Last Name';
$arrayObj3->name = 'lastname';

$dbArray = array($arrayObj1, $arrayObj2);

$finalArray = array();
foreach ($dbArray AS $key => $dbObject) {
    $finalArray[$key] = new StdClass();
    $finalArray[$key]->title = $dbObject->title;
    $finalArray[$key]->name = $dbObject->name;
    if (count($postArray[$dbObject->name]) > 1) {
        $finalArray[$key]->posted = $postArray[$dbObject->name];
    } else {
        $finalArray[$key]->posted = $postArray[$dbObject->name][0];
    }
}

var_dump($finalArray);

Tested there: http://sandbox.onlinephpfunctions.com/code/7bf0fa0fbdb086ae0cc8bb21a78261ca8c05c08e

在那里测试:http://sandbox.onlinephpfunctions.com/code/7bf0fa0fbdb086ae0cc8bb21a78261ca8c05c08e