如何在现有关联数组的内部密钥中创建新的数组键?

时间:2014-01-30 14:06:10

标签: php arrays multidimensional-array associative-array array-key

我按照以下方式关注了两个数组:

$test_array = array('frequencyCapping','images','sizes');

$soap_arr = array(
        'lineItemId' => '',
        'creativeData' => array(
            'name' => '',
            'adType' => '',
            'clickUrl' => '',
            'weight' => '',
            'width' => '',
            'height' => '',
            'landingPageId' => '',
            'text' => '',
            'frequencyCapping' => array(
                'interval' => '',
                'mount' => ''
              ),
            'images' => array(
              'file' => array(
                      'referenceUrl' => '',
                      'binaryContent' => ''
                  ),
              'externalUrl' => '',
              'sizes' => array(
                      'text' => '',
                      'clickUrl' => '',
                      'track' => '',
                      'width' => '',
                      'height' => ''
                  ),
            )
        )
    );

实际上我已经给出了单个数组。实际数组取决于用户填写的表单值。现在我想以递归的方式循环遍历这个数组,并找到匹配的键,如果键匹配,则在其中创建一个新的数组键[0]=>Array,并将所有的remianing内容附加到它。为此,我编写了以下函数,但它没有工作,因为它覆盖了所有创建的数组元素。实际上它并没有产生我想要的阵列。

function update_array($soap_arr, $test_array) {

    foreach ($soap_arr as $key => $value) {

        // if key found then move value to 0th position
        if(in_array($key, $test_array)) {
            $temp = $soap_arr[$key]; 
            unset($soap_arr[$key]);
            $soap_arr[$key][0] = $temp;
        }

        // recursive call 
        if(is_array($value)) {
            update_array($value, $test_array);
        }
    }

    //print_r($soap_arr);
    return $soap_arr;
}

// updation not preserved outside of foreach
$ar = update_array($soap_arr, $test_array);
print_r($ar)

所需的结果数组应如下所示:

Array
(
    [lineItemId] => 
    [creativeData] => Array
        (
            [name] => 
            [adType] => 
            [clickUrl] => 
            [weight] => 
            [width] => 
            [height] => 
            [landingPageId] => 
            [text] => 
            [frequencyCapping] => Array
                (
                    [0] => Array
                        (
                            [interval] => 
                            [amount] => 
                        )

                )

            [images] => Array
                (
                    [0] => Array
                        (
                            [file] => Array
                                (
                                    [referenceUrl] => 
                                    [binaryContent] => 
                                )

                            [externalUrl] => 
                            [sizes] => Array
                                (
                                    [0] => Array
                                        (
                                            [text] => 
                                            [clickUrl] => 
                                            [track] => 
                                            [width] => 
                                            [height] => 
                                        )

                                )
                        )



                )

        )

)

有人可以帮我这方面吗?提前谢谢。

1 个答案:

答案 0 :(得分:1)

Try it。创建新数组,因为如果我们在这个数组中进行更改,我们可以重置soap_arr的迭代器。也请先调用递归。

$test_array = array('frequencyCapping','images','sizes');
$soap_arr = array(
      'lineItemId' => '',
      'creativeData' => array(
          'name' => '',
          'adType' => '',
          'clickUrl' => '',
          'weight' => '',
          'width' => '',
          'height' => '',
          'landingPageId' => '',
          'text' => '',
          'frequencyCapping' => array(
              'interval' => '',
              'mount' => ''
            ),
          'images' => array(
            'file' => array(
                    'referenceUrl' => '',
                    'binaryContent' => ''
                ),
            'externalUrl' => '',
            'sizes' => array(
                    'text' => '',
                    'clickUrl' => '',
                    'track' => '',
                    'width' => '',
                    'height' => ''
                ),
          )
      )
  );

function update_array($soap_arr, $test_array) {
  $new_array = array();

  foreach ($soap_arr as $key => $value) {
      // call recursive first, because we can override changes.
      // recursive call 
      if (is_array($value)) {
          $new_array[$key] = update_array($value, $test_array);
      }
      // if key found then move value to 0th position
      if(in_array($key, $test_array)) {
          if (!isset($new_array[$key])) {
              $new_array[$key] = array();
          }
          $new_array[$key][] = $value;
      }
  }

  return $new_array;
}


// updation not preserved outside of foreach
$ar = update_array($soap_arr, $test_array);
print_r($ar);