2011-05-06 5 views
1

문제 : print_r의 경우 PHP 다차원 배열 (비틀림 일부 뇌)

$productArray[] = array(); foreach ($order->getAllItems() as $item) { $productArray[] = array( "product" => $item->getName(), "qty" => $item->getQtyOrdered(), "amount" => $item->getPrice(), ); } 

이 값만큼
내가 모델 클래스에 저장 될 수 있습니다 젠토에서 변수를 얻을 $ productArray []
출력 예제 1 :

array(1) { 
    [0]=> 
    array(3) { 
    ["product_name"]=> 
    string(12) "Test Product" 
    ["product_qty"]=> 
    string(6) "2.0000" 
    ["product_price"]=> 
    string(7) "12.0000" 
    } 
} 


012,351 6,샘플 출력 2 :

array(2) { 
    [0]=> 
    array(3) { 
    ["product_name"]=> 
    string(12) "Test Product" 
    ["product_qty"]=> 
    string(6) "2.0000" 
    ["product_price"]=> 
    string(7) "12.0000" 
    } 
    [1]=> 
    array(3) { 
    ["product_name"]=> 
    string(6) "Test 2" 
    ["product_qty"]=> 
    string(6) "5.0000" 
    ["product_price"]=> 
    string(7) "22.0000" 
    } 
} 

그리고이처럼 만들 수있는 방법 (이런 식으로 인쇄되어야한다)
경우, 출력 1 : 최종 출력 1

<input type="hidden" name="product" value="Test Product" /> 
<input type="hidden" name="amount" value="24.00" /> 


출력 2 : 최종 출력 2

<input type="hidden" name="product1" value="Test Product" /> 
<input type="hidden" name="amount1" value="24.00" /> 
<input type="hidden" name="product2" value="Test 2" /> 
<input type="hidden" name="amount2" value="110.00" /> 

값은 PRODUCT_PRICE * product_qty에서 얻을 수 있습니다.

는 재미 :)이는 더미 문제가 을 가지고 있지만, 다른 사람

이 같이

답변

2

에 도움이 될 수 있습니다 :이 도움이

<?php 
foreach($productArray as $i => $product){ 
    $index = count($productArray) == 1 ? "" : $i; //So we don't have index when only 1 element 
    $amount = $product['product_price'] * $product["product_qty"]; 
    $name = $product['product_name']; 
?> 
    <input type="hidden" name="product<?php echo $index; ?>" value="<?php echo $name;?>" /> 
    <input type="hidden" name="amount<?php echo $index;?>" value="<?php echo $amount;?>" /> 
<?php 
} 
?> 

희망. 건배

+1

+1 당신은 저를 때려 눕 힙니다. ;) – DondeEstaMiCulo

+0

hahahaha ... 이것은 도움이된다! – Jorge

1
젠토에 대한

하지 않도록하지만 정상에서 PHP가 될 것이다 :

<?php 
    $productArray = array(
    array(
     "product_name" => "Test Product", 
     "product_qty" => "2.0000", 
     "product_price" => "12.0000" 
), 
    array(
    "product_name"=> "Test 2", 
     "product_qty"=>"5.0000", 
     "product_price"=>"22.0000" 
    ) 
); 

    foreach($productArray as $v) { 
    echo '<input type="hidden" name="product" value="'.$v["product_name"].'" />'; 
    echo '<input type="hidden" name="amount" value="'.($v["product_qty"]*$v["product_price"]).'" />'; 
    } 
?> 
0

PHP에서 양식 필드의 배열을 사용하는 "제품 [$ 아이디]"이름으로, 다음의 PHP는 역류합니다 $ _POST에 깔끔한 배열. 당신은 name = "product [$ id] [price]"라고 할 수 있습니다. 그러면 2D 배열을 얻을 수 있습니다.

체크 아웃하는 동안 숨겨진 양식 데이터가 장바구니 주위로 이동한다고 믿는다면 (웹 사이트는 거칠게 보안 구멍이 생기므로 웹 주소를 게시하십시오. 무료로 물건을 주문하고 싶습니다. !

+0

hahahaha .. 내가 말했듯이 "이것은 단지 문제"입니다. 나는 그의 세션 결과에 호기심 때문에이 문제를 만들었다. 다차원 배열에 대해 탐구합니다. :) – Jorge