2013-02-04 4 views
1

표시 순서로 정렬 된 제품 목록이 포함 된 제품 배열이 있습니다.제품 배열의 범주 배열

$products = array(
[0] = array(
"productID" => 189736, 
"title" => "Spice Girls Album", 
"category" => "CD", 
"order" => "0" 
), 
[1] = array(
"productID" => 23087, 
"title" => "Snakes on a plane", 
"category" => "DVD", 
"order" => "0" 
), 
[2] = array(
"productID" => 9874, 
"title" => "The Beatles Album", 
"category" => "CD", 
"order" => "1" 
), ... etc etc 

내가 이런 종류의 배열로 돌려의 논리를 이해하려고 노력 해요 : 그래서

$categories = array(
    [0] => array(
     "title" => "CD", 
     "products" => array (
      [0] => "Spice Girls Album", 
      [1] => "The Beatles Album" 
     ) 
    ), 
    [1] => array(
     "title" => "DVD", 
     "products" => array (
      [0] => "Snakes on a plane" 
     ) 
) 

각 제품에 대한 내가 가진 :

if (!in_array($product['cateogry'], $categories)){ 
    $categories[] = $product['cateogry']; 
    $categories[$product['category']][] = $product; 
} else { 
    $categories[$product['category']][]; 
} 

그러나이 ISN을 왜냐하면 나는 in_array가 카테고리 배열을 충분히 깊이 생각하고 있다고 생각하지 않기 때문이다. 누구든지이 문제를 해결할 수있는 가장 좋은 방법에 대한 조언이 있습니까? 많은 감사

답변

1

당신은 $categories[$product['category']][] = $product과 올바른 생각을 가지고에 오타가있어 http://php.net/manual/en/function.array-key-exists.php

.

$categories = array(
    "CD" => array(
     "title" => "CD", 
     "products" => array (
      [0] => "Spice Girls Album", 
      [1] => "The Beatles Album" 
     ) 
    ), 
    "DVD" => array(
     "title" => "DVD", 
     "products" => array (
      [0] => "Snakes on a plane" 
     ) 
) 
+0

감사합니다. – Lars

0

아마 in_array() 대신 array_key_exists()를 사용해야합니다. $ 제품 [ 'cateogry']

0
$categories = array(); 
foreach($products as $product){ 
    $categories[$product['category']]['title']       = $product['category']; 
    $categories[$product['category']]['products'][$product['productID']] = $product['title']; 
} 

print_r($categories); 
+0

죄송합니다. 코드를 편집 할 때 문제가 발생했습니다 : D –

+0

'$ categories [$ category [] '] ['products '] $ product [['productID ']] = $ product ['title ']; $ 제품 [ '제품']] = $ product [ '제목']; ' –

+0

그래, 나는 그것을 편집, 감사합니다 :) –

0

당신은이 같은 것을 사용할 수 있습니다 :

if (array_key_exists($product['category'], $categories)) { 
    $categories[$product['category']]['products'][] = $product['title']; 
} else { 
    // initialize category data with first product 
    $categories[$product['category']] = array(
     'title' => $product['category'], 
     'products' => array($product) 
    ); 
} 

이 당신에게 형태의 배열을 줄 것이다 : 키 $product['category']$categories에있는 경우 당신이 체크해야 할 것은

$new_products = array(); 
foreach ($products as $product) { 
    $new_products[$product['category']][] = $product['title']; 
} 

이렇게하면 원하는대로 배열에 넣을 수 있습니다.

0
<pre> 
<?php 
$p[] = array(productID => 189736,title => 'Spice Girls Album', category => 'CD', order => 0); 
$p[] = array(productID => 23087, title => 'Snakes on a plane', category => 'DVD', order => 0); 
$p[] = array(productID => 9874, title => 'The Beatles Album', category => 'CD', order => 1); 
foreach($p as $p){ 
    $c[$p['category']]['title'] = $p['category']; 
    $c[$p['category']]['products'][] = $p['title']; 
} 
print_r($c); 
?>