Array_push

When I địa chỉ a string value into an array through array_push(), it gives me a numeric value, i.e.,

$array = array("one", "two", "three");$array2 = array("test", "test2");foreach ($array as $value) if ($value === "one") $push = array_push($array2, $value); print_r($push);Its đầu ra is 3. I want $array2 = array("test", "test2", "one")


*

*

Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
The array_push is working as it is designed for.

Bạn đang xem: Array_push

It will địa chỉ cửa hàng the value and returns the number of elements in that array.

so it is natural if it is returning 3 your array has 2 elements after array push there are now three elements.

You should print_r($array2) your array và look the elements.


*

This line:

$push = array_push($array2, $value);Should be just

array_push($array2, $value);array_push() uses reference khổng lồ the array for the first parameter. When you print_r(), you print the array $array2, instead of $push.

Xem thêm: How To Enable And Disable The Ipad Split Keyboard In Ios: 7 Steps


*

You are printing the return value of array_push which is the number of items in the array after the push. Try this:


*

Really, you should be using $array2<> = $value; which will put the value in the first available numeric key in the array, rather than array_push().

To get the value of the last element in the array(i.e. What you just added) & keep the array intact, use end($array), or lớn get the last element và remove it from array, use array_pop($array)


array_push takes the array by reference & returns the new number of elements in the array, not the new array as described here. That is why you are getting 3. If you want to see the elements in the array use printr($array2);


In more modern days you could địa chỉ strings or other data types lớn an array with Square Bracket method lượt thích this:

$arr = <"hello">;$arr<> = "world";This approach will add the string "world" khổng lồ the $arr array variable.

Now the array will actually look like this <"hello", "world"> which is pretty neat, và quicker than array_push

array_push would be more suitable if you were khổng lồ push more than one element into the array.

Im pretty confident that Square Bracket Method was introduced in PHP 5.4


Thanks for contributing an answer to Stack Overflow!

Please be sure lớn answer the question. Provide details & share your research!

But avoid

Asking for help, clarification, or responding lớn other answers.Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.


Post Your Answer Discard

By clicking “Post Your Answer”, you agree lớn our terms of service, privacy policy and cookie policy


Site thiết kế / logo sản phẩm © 2022 Stack Exchange Inc; user contributions licensed under CC BY-SA. Rev2022.10.31.43000


Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.