php - How to create the array with key and value every time? -
i want create array such $animal = array("a" => "horse","b" => "fish")
.
constraint condition create 1 element (key , value) in array 1 time,that say
in first time create key "a" , value "horse" ,to make $animal = array("a" => "horse")
,
in second time create key "b" , value "fish",to make $animal = array("a" => "horse","b" => "fish")
.
i can create array("horse","fish")
in 2 times, in first time make array array("horse")
,
in second time make array array("horse","fish")
.
<?php $animal = array(); $x2 = "horse"; $x4 = "fish"; $animal[] = $x2; $animal[] = $x4; print_r($animal); ?>
how create array("a" => "horse","b" => "fish") in same way?
<?php $animal = array(); $x1 = "a"; $x2 = "horse"; $x3 = "b"; $x4 = "fish"; array_keys($animal[]) = $x1; array_values($animal[]) = $x2; array_keys($animal[]) = $x3; array_values($animal[]) = $x4; print_r($animal); ?>
how fix code job?
when using [] push elements array, can specify key, if no key specified, default values used, therefor:
<?php $animal = array(); $x1 = "a"; $x2 = "horse"; $x3 = "b"; $x4 = "fish"; $animal[$x1] = $x2; $animal[$x3] = $x4; print_r($animal); ?>
Comments
Post a Comment