简体   繁体   中英

PHP add new array in an Two-dimensional Arrays

i want to create new instance in the $shop array , something like this :

    list1 = array( "rose" , 1.25 , 15);
    $list2 = array("daisy", 0.75 , 25);
    $list3 = array("orchid", 1.15 , 7);
    $list4 = array("orchid1", 2.15 , 9);

    $shop = array( $list1 ,
                   $list2 ,
                   $list3 
                 );

 //something like the line bellow 
    $shop = $shop + array(array($list4));
    echo $shop[3][0];

when i execute this code , i'm facing this error msg :

Notice: Undefined offset: 3 in C:\\xampp\\htdocs\\array.php on line 13

line 13 : $shop = $shop + array(array($list4));

thanks in advance ^^

If $list4 is already an array, then you don't need array(array()) . The simplest and probably fastest way would be to do:

$shop[] = $list4;
//equivalent
$shop[] = array("orchid1", 2.15 , 9);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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