将一种格式排列到另一种

时间:2016-04-09 11:06:56

标签: php arrays

我有两种格式的数组。我想从

更改数组
Array
(
    [app_id] => Array
        (
            [0] => 2
            [1] => 3
        )

    [fieldval] => Array
        (
            [2] => Array
                (
                    [2] => Array
                        (
                            [0] => 1
                        )

                )

            [3] => Array
                (
                    [3] => Array
                        (
                            [0] => 200
                            [1] => day
                        )

                )

        )

    [title] => new plan
    [feature_plan] => 3
    [plan_type] => free
    [price] => 
    [sell_type] => us
)

这种格式

import RPi.GPIO as GPIO
import time
import cv2
import os

GPIO.setmode(GPIO.BCM)

GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
    input_state = GPIO.input(17)
    if input_state == False:
    print('Button Pressed')
    capture = cv2.VideoCapture(0)
    capture.set(3,1280)
    capture.set(4,1024)
    ret, img = capture.read()
    cv2.imshow("input", img)
    cv2.waitKey(1500)
    del(capture)
    cv2.VideoCapture(0).release()

这些是一个数组为两种格式。我有数据到第一阵列格式,我想将该格式更改为第二阵列类型格式。 请告诉我我如何尝试2天但没有成功。

1 个答案:

答案 0 :(得分:1)

这是一个可用于产生转换的函数:

function convert_formdata($input) {    
    $output = array();
    foreach($input['formdata'] as $data) {
        $keys = preg_split("#[\[\]]+#", $data['name']);
        $value = $data['value'];
        $target = &$output;
        foreach($keys as $key) {
            // Get index for "[]" reference
            if ($key == '') $key = count($target);
            // Create the key in the parent array if not there yet
            if (!isset($target[$key])) $target[$key] = array();
            // Move pointer one level down the hierarchy
            $target = &$target[$key];
        }
        // Write the value at the pointer location
        $target = $value;
    }
    return $output;
}

你会这样称呼:

$output = convert_formdata($input);

在给定输入的eval.in上查看它。输出是:

array (
  'app_id' => 
  array (
    0 => 2,
    1 => 3,
  ),
  'fieldval' => 
  array (
    2 => 
    array (
      2 => 
      array (
        0 => 1,
      ),
    ),
    3 => 
    array (
      3 => 
      array (
        0 => 200,
        1 => 'day',
      ),
    ),
  ),
  'title' => 'new plan,',
  'feature_plan' => 3,
  'plan_type' => 'free',
  'price' => NULL,
  'sell_type' => 'us',
)