douqin2108 2013-11-16 21:00
浏览 76
已采纳

来自php数组的json_encode:如何调整我的php以便我的数组是正确的?

I am trying to create a php array from posted values and then json_encode to achieve this:

[{"network_type":"facebook","network_url":"fb.com/name"},{"network_type":"twitter","network_url":"@name"},{"network_type":"instagram","network_url":"#name"}]

which after json_decode looks like:

array(
 [0] => stdClass(

 network_type = 'facebook'
 network_url = 'fb.com/name'
 )

 [1] => stdClass(

 network_type = 'twitter'
 network_url = '@name'
 )

 [2] => stdClass(

 network_type = 'instagram'
 network_url = '#name'
 )
)

My php looks like this:

$social_data = array(

          'network_type'    => $this->input->post('network_type'),
          'network_url'     => $this->input->post('network_url')
    );

and so the array is not grouped the way I want it, but rather by the field name:

array(
['network_type'] => array(
  [0] => 'facebook'
  [1] => 'twitter'
  [2] => 'instagram'
)
['network_url'] => array(
  [0] => 'fb.com/name'
  [1] => '@name'
  [2] => '#name'
)

and therefore the result of the json_encode isn't grouped how I want it:

 {"network_type":["facebook","twitter","instagram"],"network_url":["fb.com/name","@name","#name"]}

)

So the question is...how do I adjust my php so the array is correct?

--- here's the input fields:

<?php
        foreach ($social as $key => $value) {?>
         <p>
              <label for="network_type"><input type="text" size="20" name="network_type[]" value="<?php echo $value -> network_type; ?>" placeholder="Social Network" /></label>
              <label for="network_url"><input type="text" size="20" name="network_url[]" value="<?php echo $value -> network_url; ?>" placeholder="URL or Handle" /></label>
              <a class="remNetwork" href="#">Remove</a>
          </p>
      <?php } ?>

----latest update: this is sooooo close!----- Ok, for some reason (probably me botching things one way or another), both suggested methods below didn't quite get me there....but, a mix of both methods has gotten me close:

This if/loop/array setup:

 $network_type    = (array)$this->input->post('network_type', true);
$network_url     = (array)$this->input->post('network_url', true);
$social_data = array();


if (($counter = count($network_type)) == count($network_url)){
for($i = 0;$i < $counter; $i++) {
    $social_data[$i] = array(
            'network_type'    => $this->input->post('network_type[$i]'),
            'network_url'     => $this->input->post('network_url[$i]'),
        );
  }
}

paired with this input loop:

<?php
        foreach ($social as $key => $value) {
      ?>
         <p>
              <label for="network_type"><input type="text" size="20" name="network_type[]" value="<?php echo $value -> network_type; ?>" placeholder="Social Network" /></label>
              <label for="network_url"><input type="text" size="20" name="network_url[]" value="<?php echo $value -> network_url; ?>" placeholder="URL or Handle" /></label>
              <a class="remNetwork" href="#">Remove</a>
          </p>
      <?php } ?>

is yielding the following:

array(
  [0] =>  array(
    ['network_type'] => FALSE
    ['network_url'] => FALSE
  )
  [1] => array(
    ['network_type'] => FALSE
    ['network_url'] => FALSE
  )
  [2] => array(
    ['network_type'] => FALSE
    ['network_url'] => FALSE
  )
)

So, I think if I can figure out why these values are false, then we've done it! Thanks and appreciation for your patience and help in advance...

-- update again ---- To my dismay, I've left out a piece that's perhaps critical...the coffee script that's dynamically creating the form fields when there's more than one social:

$ ->


socialDiv = $("#social")
  i = $("#social p").size() + 1
  index = 0
  $("#addNetwork").click ->
$("<p><label for=\"network_type[]\"><input type=\"text\" id=\"network_type[]\" size=\"20\" name=\"network_type[]\" value=\"\" placeholder=\"Social Network\" /></label><label for=\"network_url[]\"><input type=\"text\" id=\"network_url[]\" size=\"20\" name=\"network_url[]\" value=\"\" placeholder=\"URL or Handle\" /></label> <a href=\"#\" class=\"remNetwork\">Remove</a></p>").appendTo socialDiv
i++
false
$(document).on "click", ".remNetwork", ->
      #$(".remNetwork").bind "click", ->
        if i > 1
          $(this).parent("p").remove()
          i--
        false
  • 写回答

2条回答 默认 最新

  • douwen4125 2013-11-16 21:29
    关注

    In your example, i can asume that network_type and network_url are inputs like:
    <input type="text" name="network_type[]" /> and <input type="text" name="network_url[]" />

    so in order to accomplish what you want you would:

    <?php
    $networkType = (array)$this->input->post('network_type', true);
    $networkUrl = (array)$this->input->post('network_url', true);
    
    $networkData = array();
    if (($counter = count($networkType)) == count($networkUrl)) {
        for ($i = 0; $i < $counter; ++$i) {
            $networkData[] = array(
                'network_type' => $networkType[$i],
                'network_url' => $networkUrl[$i]
            );
        }
    }
    ?>
    

    and should give you the desired json.
    However, this approach is a bit clunky and unreliable.

    Instead, i would define the input fields like:

    <input type="text" name="network[0][network_type]" /> and <input type="text" name="network[0][network_url]" />

    <input type="text" name="network[1][network_type]" /> and <input type="text" name="network[1][network_url]" />

    and i would generate the php array like:

    <?php
    $networkData = (array)$this->input->post('network', true);
    ?>
    

    Just my two cents :)

    L.E:
    To make it detailed and create the std objects from your array, you would:

    $networkData = array();
    if (($counter = count($networkType)) == count($networkUrl)) {
        for ($i = 0; $i < $counter; ++$i) {
            $obj = new StdClass();
            $obj->network_type = $networkType[$i];
            $obj->network_url = $networkUrl[$i];
            $networkData[] = $obj;
        }
    }
    print_r($networkData);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算