将php数组转换为javascript对象

时间:2013-09-12 18:29:48

标签: javascript php jquery

**我的prevoius问题改变了**

我在php中的prevoius代码

 Array
    (
        [0] => Array
            (
                [id] => 2000
                [title] => TIERE, DIANA
            )
        [1]  => Array
            (
               [id] => 1500
               [title] =>fhg , DIANA 
            )
    )

并且在javascript中,JSON.parse(obj)被唤醒了,它给了我一个对象数组 喜欢

  [
    0:object
    1:object
  ]

然后我将我的php数组改为

 Array
    (
        [2000] => Array
            (
                [id] => 2000
                [title] => TIERE, DIANA
            )
        [1500]  => Array
            (
               [id] => 1500
               [title] =>fhg , DIANA 
            )
    )

但JSON.parse(obj)只给出数组中的一个对象,如下所示

[object]

我的问题是如何在javascrip中构建json数组,如下所示

   [
    2000: Object
    1500: Object
    800: Object
    500: Object
   ]

来自像这样的php数组

Array
(
    [2000] => Array
        (
            [id] => 2000
            [title] => TIERE, DIANA
        )
    [1500]  => Array
        (
           [id] => 1500
           [title] =>fhg , DIANA 
        )
)

1 个答案:

答案 0 :(得分:2)

我认为您可能找到比使用CSS尝试更好的解决方案,但我认为这就是您所要求的:

var positions = new Array();
$('.icon').each(function() {
  var $icon = $(this);
  var left = $icon.css('left');
  if ($.inArray(left, positions)) {
    // yes, an icon already exists with this CSS left value.
    // I'm not sure what you intended to do about it.
    // maybe increment the alignment of this one?
    left = left + 2;
    $icon.css('left', left);
  }
  positions.push(left);
});