gpt4 book ai didi

php - 如果我用第 n 个索引在 PHP 中启动一个数组,它将占用多少内存?

转载 作者:可可西里 更新时间:2023-11-01 00:28:28 25 4
gpt4 key购买 nike

我需要使用索引作为第 n 个数字在 Php 中创建一个数组。例如:

<?php
$A = array();
$A['1000']='Some value';

这个数组是否会占用剩余的999个索引的内存?

最佳答案

php 中的数组将值与键相关联。它就像您可以在官方文档中找到的有序 map 。

An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

这意味着 php 不会为剩余的 999 个索引分配内存。另外,您可以通过将数组转换为 json 来测试它:

echo json_encode($A);

这将返回

{"1000":"Some value"}

关于php - 如果我用第 n 个索引在 PHP 中启动一个数组,它将占用多少内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56126811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com