【问题标题】:How to transform array object into array string如何将数组对象转换为数组字符串
【发布时间】:2023-03-28 03:23:02
【问题描述】:

我有一个这样的结果控制台:

[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]

我想要这样的结果

[{value: '1', text: 'one'}, {value: '2', text: 'two'}]

this is full of code

【问题讨论】:

标签: php jquery arrays json object


【解决方案1】:

像这样:

$array = json_decode(json_encode($nested_object), true);

或者只是对它进行类型转换:

$array =  (array) $yourObject;

【讨论】:

    【解决方案2】:

    如果你确实想通过循环来做,我认为你可以使用它

    $array = array();
    $counter = 0;
    foreach(Object as $val)
    {
       $array[$counter]['value'] = $val->value;
       $array[$counter]['text']  = $val->text; 
       $counter++;
    }
    

    【讨论】:

      【解决方案3】:

      使用JSON.stringify()

      var obj = [{value: '1', text: 'one'}, {value: '2', text: 'two'}]
      console.log(JSON.stringify(obj));
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

      【讨论】:

        猜你喜欢
        • 2022-10-07
        • 2011-12-27
        • 2021-05-09
        • 1970-01-01
        • 2020-07-15
        相关资源
        最近更新 更多