2013-06-17 3 views
0

$ _POST superglobal에서 값을 수집하는 다음 배열로 배열을 작성해야합니다. ca_objets 값은 변수에서 모으고 점 뒤에있는 부분은 $ _POST에서 수집해야하는 부분입니다. 내가 필요로하는 결과를 $의 va_body 배열 내부의 "번들"배열을 채우고 얻기 위해 루프를 코딩하는 방법을 잘 모르겠어요

<?php 
$table="ca_objects"; 
$bund=$_POST['find_bundles']; //This is an array with the values I need. 

$va_body=array("bundles" => array()); //I declare the array outside the foreach loop. 

:

$va_body=array(
     "bundles" => array(
      "ca_objects.description" => array("convertCodesToDisplayText" => true), 
      "ca_objects.description" => array("convertCodesToDisplayText" => true), 
      "ca_object.representations.caption" => array("convertCodesToDisplayText" => true) 
     ) 
    ); 

내가 사용하는 코드입니다 . 어떤 도움이라도 대단히 감사하겠습니다.

foreach ($bund as $elem) { 
    // code here 
     } 

    ?> 

답변

1

그냥 같이, 그 연관 키 배열에 액세스 할 수 있습니다

foreach ($bund as $elem) { 
    // code here 
    // The empty square brackets tells php to push the $elem value into the next available array key 
    $va_body['bundles'][] = $elem; 
} 
+0

감사 값을 분할! 그것은 나를 위해 정말로 유용합니다. –

0

새로운 키와 도트 후 값을 얻으려면, 당신은 문자열을 분할해야합니다. 양식의 foreach 루프를 사용한다면 : foreach($array as $key => $value) 당신이 당신의 대답 점 후

foreach ($bund as $elem => $value) { 
     $strings = explode('.', $elem); // Split the strings at the dot 
     $ca_objects[$bundles][$strings[count(strings) - 1]] = $value; // Add to array with $elem => $value 
    } 
+0

종합적인 답변을 제공해 주셔서 감사합니다. 나는 그걸로 성공할 것이라고 확신한다. –