“PHP Dodaj tablicę do tablicy” Kod odpowiedzi

Dołącz PHP do tablicy

$myArr = [1, 2, 3, 4];

array_push($myArr, 5, 8);
print_r($myArr); // [1, 2, 3, 4, 5, 8]

$myArr[] = -1;
print_r($myArr); // [1, 2, 3, 4, 5, 8, -1]
Allen

php array_merge


<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
Array
(
    [color] => green
    [0] => 2
    [1] => 4
    [2] => a
    [3] => b
    [shape] => trapezoid
    [4] => 4
)
Alberto Peripolli

PHP Scal 2 tablice

<?php
  $array1 = [
      "color" => "green"
  ];
  $array2 = [
      "color" => "red", 
      "color" => "blue"
  ];
  $result = array_merge($array1, $array2);
?>

// $result
[
    "color" => "green"
    "color" => "red", 
    "color" => "blue"
]
TheDutchScorpion

PHP Dodaj do tablicy

$fruits = ["apple", "banana"];
// array_push() function inserts one or more elements to the end of an array
array_push($fruits, "orange");

// If you use array_push() to add one element to the array, it's better to use
// $fruits[] = because in that way there is no overhead of calling a function.
$fruits[] = "orange";

// output: Array ( [0] => apple [1] => banana [2] => orange )
Yingfufu

array_push in php

<?php
// Insert "blue" and "yellow" to the end of an array:


$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>
MohammadMark

PHP Dodaj tablicę do tablicy

$a = array('a','b','c');
$b = array('c','d','e');

array_push($a, ...$b);
print_r($a);
/*
notice this is different than array merge as it does not merge
values that the same 
Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => c
    [4] => d
    [5] => e
)
*/
Friendly Hawk

Odpowiedzi podobne do “PHP Dodaj tablicę do tablicy”

Pytania podobne do “PHP Dodaj tablicę do tablicy”

Więcej pokrewnych odpowiedzi na “PHP Dodaj tablicę do tablicy” w PHP

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu