将键和值对推入多维数组

时间:2022-05-05 21:37:36

Please note that this is not a duplicate - but an extension of these questions below)

请注意,这不是重复 - 但下面是这些问题的扩展)

PHP array_push one array into another

PHP array_push将一个数组转换为另一个数组

array_push into a multi-dimensional array

array_push成为一个多维数组

I am trying to array_push into a multidimensional array but want to keep the array key of the second array.

我正在尝试将array_push转换为多维数组,但希望保留第二个数组的数组键。

Example:

<?php
$samplearray = array(
array('name' => "Joe Bloggs", 'age' => "30", 'sex' => "Male", 'title' => "Mr" ),
array('name' => "Jane Bloggs", 'age' => "30", 'sex' => "Female", 'title' => "Mrs" ),
array('name' => "Little Bloggs", 'age' => "10", 'sex' => "Male", 'title' => "Master" ),    
);

array_push ($samplearray[0],"Inserted Value");

print_r($samplearray);
?>

The output for this is:

这个输出是:

array(3) {
  [0]=>
  array(5) {
    ["name"]=>
    string(10) "Joe Bloggs"
    ["age"]=>
    string(2) "30"
    ["sex"]=>
    string(4) "Male"
    ["title"]=>
    string(2) "Mr"
    [0]=>
    string(14) "Inserted Value" <-- INSERTED VALUE
  }
  [1]=>
  array(4) {
    ["name"]=>
    string(11) "Jane Bloggs"
    ["age"]=>
    string(2) "30"
    ["sex"]=>
    string(6) "Female"
    ["title"]=>
    string(3) "Mrs"
  }
  [2]=>
  array(4) {
    ["name"]=>
    string(13) "Little Bloggs"
    ["age"]=>
    string(2) "10"
    ["sex"]=>
    string(4) "Male"
    ["title"]=>
    string(6) "Master"
  }
}

I want to insert a key along with the value but when I try that - it fails. Can you please advice

我想插入一个键和值,但是当我尝试它时 - 它失败了。能告诉你吗?

array_push ($samplearray[0]['insertedvalue'],"Inserted Value");

returns a NULL value for the inserted key on local server but fails on PHPfiddle.

为本地服务器上的插入键返回NULL值,但在PHPfiddle上失败。

2 个解决方案

#1


1  

is this what you are looking for?

这是你想要的?

        $samplearray = array(
        array('name' => "Joe Bloggs", 'age' => "30", 'sex' => "Male", 'title' => "Mr" ),
        array('name' => "Jane Bloggs", 'age' => "30", 'sex' => "Female", 'title' => "Mrs" ),
        array('name' => "Little Bloggs", 'age' => "10", 'sex' => "Male", 'title' => "Master" ),
    );

    $samplearray[0]['othername'] = 'lalala';
    echo '<pre>';
    print_r($samplearray);

and this should print:

这应该打印:

Array
(
[0] => Array
    (
        [name] => Joe Bloggs
        [age] => 30
        [sex] => Male
        [title] => Mr
        [othername] => lalala
    )

[1] => Array
    (
        [name] => Jane Bloggs
        [age] => 30
        [sex] => Female
        [title] => Mrs
    )

[2] => Array
    (
        [name] => Little Bloggs
        [age] => 10
        [sex] => Male
        [title] => Master
    )

)

#2


1  

This must be what you need.

这一定是你需要的。

$samplearray[0]['insertedvalue'] = "Inserted Value";

#1


1  

is this what you are looking for?

这是你想要的?

        $samplearray = array(
        array('name' => "Joe Bloggs", 'age' => "30", 'sex' => "Male", 'title' => "Mr" ),
        array('name' => "Jane Bloggs", 'age' => "30", 'sex' => "Female", 'title' => "Mrs" ),
        array('name' => "Little Bloggs", 'age' => "10", 'sex' => "Male", 'title' => "Master" ),
    );

    $samplearray[0]['othername'] = 'lalala';
    echo '<pre>';
    print_r($samplearray);

and this should print:

这应该打印:

Array
(
[0] => Array
    (
        [name] => Joe Bloggs
        [age] => 30
        [sex] => Male
        [title] => Mr
        [othername] => lalala
    )

[1] => Array
    (
        [name] => Jane Bloggs
        [age] => 30
        [sex] => Female
        [title] => Mrs
    )

[2] => Array
    (
        [name] => Little Bloggs
        [age] => 10
        [sex] => Male
        [title] => Master
    )

)

#2


1  

This must be what you need.

这一定是你需要的。

$samplearray[0]['insertedvalue'] = "Inserted Value";