PHP数组问题

时间:2011-08-04 15:04:28

标签: php

array(
   array('codcentrocustos' => 1, 'codparent' => null, 'name' => 'lorem ipsum'),
   array('codcentrocustos' => 2, 'codparent' => 1, 'name' => 'lorem ipsum1'),
   array('codcentrocustos' => 3, 'codparent' => 1, 'name' => 'lorem ipsum2'),
   array('codcentrocustos' => 4, 'codparent' => 2, 'name' => 'lorem ipsum3'),
   array('codcentrocustos' => 5, 'codparent' => 3, 'name' => 'lorem ipsum4'),
   array('codcentrocustos' => 6, 'codparent' => null, 'name' => 'lorem ipsum5'),
);
嘿伙计们,我有这个数组,我需要把它变成一个json,但我现在没有任何问题。这个问题与它在需要时用它制作的php有关。

如果我尝试对他做一个var_dump,他会把它归还给我:

array
  0 => 
    array
      'codcentrocustos' => int 1
      'codparent' => null
      'name' => string 'lorem ipsum' (length=11)
  1 => 
    array
      'codcentrocustos' => int 2
      'codparent' => int 1
      'name' => string 'lorem ipsum1' (length=12)
  2 => 
    array
      'codcentrocustos' => int 3
      'codparent' => int 1
      'name' => string 'lorem ipsum2' (length=12)
  3 => 
    array
      'codcentrocustos' => int 4
      'codparent' => int 2
      'name' => string 'lorem ipsum3' (length=12)
  4 => 
    array
      'codcentrocustos' => int 5
      'codparent' => int 3
      'name' => string 'lorem ipsum4' (length=12)
  5 => 
    array
      'codcentrocustos' => int 6
      'codparent' => null
      'name' => string 'lorem ipsum5' (length=12)

问题是我需要没有这些参考数字的数组

array
  **0 =>** 
    array
      'codcentrocustos' => int 1
      'codparent' => null
      'name' => string 'lorem ipsum' (length=11)
  **1 =>** 

4 个答案:

答案 0 :(得分:1)

PHP需要将项存储在某个键下,默认情况下它使用从0开始的连续整数键。当它显示数组的内容时,它还会打印键以方便使用(因为var_dump是一个调试功能)。

您可以简单地忽略它们并使用数组,就像它没有它们一样并使用函数:array_pusharray_poparray_shiftarray_unshiftnextpreviouscurrent等来遍历和访问数组。

答案 1 :(得分:1)

创建数组时:

array(
   array('codcentrocustos' => 1, 'codparent' => null, 'name' => 'lorem ipsum'),
   array('codcentrocustos' => 2, 'codparent' => 1, 'name' => 'lorem ipsum1'),
   array('codcentrocustos' => 3, 'codparent' => 1, 'name' => 'lorem ipsum2'),
   array('codcentrocustos' => 4, 'codparent' => 2, 'name' => 'lorem ipsum3'),
   array('codcentrocustos' => 5, 'codparent' => 3, 'name' => 'lorem ipsum4'),
   array('codcentrocustos' => 6, 'codparent' => null, 'name' => 'lorem ipsum5'),
);

如果你没有为数组指定一个键(就像你在为第一个维度做的那样),你真正说的是:

array(
   0 => array('codcentrocustos' => 1, 'codparent' => null, 'name' => 'lorem ipsum'),
   1 => array('codcentrocustos' => 2, 'codparent' => 1, 'name' => 'lorem ipsum1'),
   2 => array('codcentrocustos' => 3, 'codparent' => 1, 'name' => 'lorem ipsum2'),
   3 => array('codcentrocustos' => 4, 'codparent' => 2, 'name' => 'lorem ipsum3'),
   4 => array('codcentrocustos' => 5, 'codparent' => 3, 'name' => 'lorem ipsum4'),
   5 => array('codcentrocustos' => 6, 'codparent' => null, 'name' => 'lorem ipsum5'),
);

如果您希望它们与这些数字不同,请为该键指定一个值。如果未指定密钥,则php会分配一个通常为最高数字密钥+1的密钥。

答案 2 :(得分:1)

要将数组转换为JSON,只需使用json_encode即可。您不需要任何转换。

$test = array(
   array('codcentrocustos' => 1, 'codparent' => null, 'name' => 'lorem ipsum'),
   array('codcentrocustos' => 2, 'codparent' => 1, 'name' => 'lorem ipsum1'),
   array('codcentrocustos' => 3, 'codparent' => 1, 'name' => 'lorem ipsum2'),
   array('codcentrocustos' => 4, 'codparent' => 2, 'name' => 'lorem ipsum3'),
   array('codcentrocustos' => 5, 'codparent' => 3, 'name' => 'lorem ipsum4'),
   array('codcentrocustos' => 6, 'codparent' => null, 'name' => 'lorem ipsum5'),
);

print json_encode($test);

<强>输出

[{"codcentrocustos":1,
     "codparent":null,
     "name":"lorem ipsum"},
    {"codcentrocustos":2,
     "codparent":1,
     "name":"lorem ipsum1"},
    {"codcentrocustos":3,
     "codparent":1,
     "name":"lorem ipsum2"},
    {"codcentrocustos":4,
     "codparent":2,
     "name":"lorem ipsum3"},
    {"codcentrocustos":5,
     "codparent":3,
     "name":"lorem ipsum4"},
    {"codcentrocustos":6,
     "codparent":null,
     "name":"lorem ipsum5"}]

答案 3 :(得分:0)

你无法摆脱这些指数。数组中的每个元素都必须有一个键。这是完全不可避免的。