简体   繁体   English

在 PHP 中将数组元素添加到子数组

[英]Adding an array element to a subarray in PHP

I have我有

$statement = Array
(
    [0] => Array
        (
            [red] => 06-01-2012
            [green] => 436
            [blue] => MEDIA
            [black] => 2006
        )

    [1] => Array
        (
            [red] => 06-01-2012
            [green] => 430
            [blue] => MEDIA
            [black] => 2007
        )

);

And I want to add [flex] => 1 into array 1 by using something like $statement[1].我想通过使用 $statement[1] 之类的东西将[flex] => 1添加到数组 1 中。 I have tried with array merge but they have combined array 0 and 1. Basically I want to add to the latest one.我尝试过数组合并,但它们组合了数组 0 和 1。基本上我想添加到最新的一个。

if i understood you, try this:如果我理解你,试试这个:

$statement[count($statement)-1]['flex'] = 1;
<?php
$statement = array(
    array(
            "red" => 06-01-2012,
            "green" => 436,
            "blue" => "MEDIA",
            "black" => 2006
        )

    ,array(
            "red" => 06-01-2012,
            "green" => 436,
            "blue" => "MEDIA",
            "black" => 2006
        )

);

echo "<pre>";
print_r($statement); //first

$statement[1]["flex"] = 1;

print_r($statement); //second
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM