1

I've following two arrays as follows:

$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' => ''
                  ),
            )
        )
    );

Actually I've given above the single array. The actual array depends upon the form values fillied in by user. Now I want to loop over this array recursively and find out the matching keys if the keys are matched then create a new array key as [0]=>Array inside it and append all the remianing contents to it. For it I've written following function but it's not working as it is overwriting all the created array elemets. Actually it's not generating the desired array I wanted.

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)

The desired resulting array should be as follows:

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] => 
                                        )

                                )
                        )



                )

        )

)

Can anyone help me in this regard please? Thanks in advance.

1 Answer 1

1

Try it. Create new array, because we can reset iterator for soap_arr if we will make changes in this array. Also call recursive the first.

$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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.