fork download
  1. <?php
  2.  
  3. $test_array = array('frequencyCapping','images','sizes');
  4. $soap_arr = array(
  5. 'lineItemId' => '',
  6. 'creativeData' => array(
  7. 'name' => '',
  8. 'adType' => '',
  9. 'clickUrl' => '',
  10. 'weight' => '',
  11. 'width' => '',
  12. 'height' => '',
  13. 'landingPageId' => '',
  14. 'text' => '',
  15. 'frequencyCapping' => array(
  16. 'interval' => '',
  17. 'mount' => ''
  18. ),
  19. 'images' => array(
  20. 'file' => array(
  21. 'referenceUrl' => '',
  22. 'binaryContent' => ''
  23. ),
  24. 'externalUrl' => '',
  25. 'sizes' => array(
  26. 'text' => '',
  27. 'clickUrl' => '',
  28. 'track' => '',
  29. 'width' => '',
  30. 'height' => ''
  31. ),
  32. )
  33. )
  34. );
  35. // 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.
  36.  
  37. function update_array($soap_arr, $test_array) {
  38. $new_array = array();
  39.  
  40. foreach ($soap_arr as $key => $value) {
  41. // call recursive first, because we can override changes.
  42. // recursive call
  43. if (is_array($value)) {
  44. $new_array[$key] = update_array($value, $test_array);
  45. }
  46. // if key found then move value to 0th position
  47. if(in_array($key, $test_array)) {
  48. if (!isset($new_array[$key])) {
  49. $new_array[$key] = array();
  50. }
  51. $new_array[$key][] = $value;
  52. }
  53. }
  54.  
  55. return $new_array;
  56. }
  57.  
  58.  
  59. // updation not preserved outside of foreach
  60. $ar = update_array($soap_arr, $test_array);
  61. print_r($ar);
  62.  
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
Array
(
    [creativeData] => Array
        (
            [frequencyCapping] => Array
                (
                    [0] => Array
                        (
                            [interval] => 
                            [mount] => 
                        )

                )

            [images] => Array
                (
                    [file] => Array
                        (
                        )

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

                        )

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

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

                        )

                )

        )

)