2017-06-09 18 views
0

これは、ありがたいことにユーザーから回答を受けた後の質問です。PHP配列子要素を英数字配列ブロックにグループ化する方法

次の配列には、ネストされた子文字列のブロックを持つ親文字列があります。私がしようとしているのは、英数字の子文字列キーが付加された数字でグループ化されている新しい配列を作成することです。私はまた、それは 'タイトル、desc、価格、画像、その後、何らかの理由で逆転して、3番目の子ループの元の順序に戻りますので、子キーの順序に問題があります。しかし、私は今、アレイ上横断しににpreg_replaceを使用しようとしているキャストステートメントを使用してために、私はグループにそれらを管理し、元のMySQLを修正することによって

[ 
    { 
     "hair-and-beauty": { 
      "_business_title_0": "service 1", 
      "_business_description_0": "service 1", 
      "_business_price_0": "22", 
      "_business_image_id_0": "2367", 
      "_business_price_1": "66", 
      "_business_image_id_1": "2367", 
      "_business_title_1": "service 2", 
      "_business_description_1": "service 2", 
      "_business_title_2": "service 3", 
      "_business_description_2": "service 3", 
      "_business_price_2": "33", 
      "_business_image_id_2": "2367" 
     } 
    }, 
    { 
     "hair-colouring": { 
      "_business_title_0": "service 1", 
      "_business_description_0": "service 1", 
      "_business_price_0": "22", 
      "_business_image_id_0": "2367", 
      "_business_price_1": "66", 
      "_business_image_id_1": "2367", 
      "_business_title_1": "service 2", 
      "_business_description_1": "service 2", 
      "_business_title_2": "service 3", 
      "_business_description_2": "service 3", 
      "_business_price_2": "33", 
      "_business_image_id_2": "2367" 
     } 
    } 
] 

相続人は、私がこれまで持っているもの数字を削除してintとしてグループ化してください。

私はこの構造が必要です。

[ 
    { 
     "hair-and-beauty": { 
      { 
      "_business_title_0": "service 1", 
      "_business_description_0": "service 1", 
      "_business_price_0": "22", 
      "_business_image_id_0": "2367", 
      }, 
      { 
      "_business_title_1": "66",   //Correctly ordered 2nd loop 
      "_business_description_1": "2367", //Correctly ordered 2nd loop 
      "_business_price_1": "service 2",  //Correctly ordered 2nd loop 
      "_business_images_id_1": "service 2", //Correctly ordered 2nd loop 
      }, 
      { 
      "_business_title_2": "service 3", 
      "_business_description_2": "service 3", 
      "_business_price_2": "33", 
      "_business_image_id_2": "2367" 
      } 
     } 
    }, 
    { 
     "hair-colouring": { 
      { 
      "_business_title_0": "service 1", 
      "_business_description_0": "service 1", 
      "_business_price_0": "22", 
      "_business_image_id_0": "2367" 
      } 
      "_business_title_1": "66",   //Correctly ordered 2nd loop 
      "_business_description_1": "2367", //Correctly ordered 2nd loop 
      "_business_price_1": "service 2", //Correctly ordered 2nd loop 
      "_business_image_id_1": "service 2" //Correctly ordered 2nd loop 
      }, 
      { 
      "_business_title_2": "service 3", 
      "_business_description_2": "service 3", 
      "_business_price_2": "33", 
      "_business_image_id_2": "2367" 
      } 
     } 
    } 
] 

Iを介してループし、配列を並べ替えが、私はこの1つの壁に当たっていると私はそれをすべてのやり直しや変更の点でだ別のページの配列に類似したループを使用フロントエンドの入力、データベースストア、そして10ページ後のこのページの出力を示します。あなたが正しい方向に私を向けることによって助けることができれば/これを達成するために使用する正しいPHP配列関数は、それは私を助けてくれるでしょう。次のように

私は今、失敗した変更しようとしている他の機能のためにしたコードは次のとおりです。

 foreach($info5 as $row){ 
      $int = preg_replace("/[^0-9,.]/", "", $row["meta_key"]); 
      $items[$int][$row["meta_key"]] = $row["meta_value"]; 
      if(strpos('_menu_cat_', $row["meta_key"]) === true){ 
       $menu[$row["meta_value"]] = array(); 
      } 
     } 

上記の問題は、私はすべての子供たちがそれぞれの親のように上に存在する必要があるということです両親はカテゴリーであり、子供は複数であることができる。フォームを使用してデータベースに挿入すると、ドロップダウンから複数のカテゴリを選択し、製品情報を入力します。エントリが列挙されているテーブルにカテゴリIDがなく、追加の列を追加すると、列などの数をカウントする既存のページに問題があります。これを設定する方法はありませんが、それは私が持っているものです一緒に働く 上記の例では、最初のカテゴリの子のみが検索され、次に連続する親カテゴリには子ストリングがないため、nullになります。私はすべての両親のために同じ子供を使用するために結果を偽造する必要があります。なぜ、私が上に投稿した最後の配列を変更することによって結果をぼかしているのですか?私はこの説明であまり役に立たないとは思わない。

ありがとうございます。

答えて

0

これは、あなたがあなたの配列で操作するためのコードです:

$json = '[ 
    { 
     "hair-and-beauty": { 
      "_business_title_0": "service 1", 
      "_business_description_0": "service 1", 
      "_business_price_0": "22", 
      "_business_image_id_0": "2367", 
      "_business_price_1": "66", 
      "_business_image_id_1": "2367", 
      "_business_title_1": "service 2", 
      "_business_description_1": "service 2", 
      "_business_title_2": "service 3", 
      "_business_description_2": "service 3", 
      "_business_price_2": "33", 
      "_business_image_id_2": "2367" 
     } 
    }, 
    { 
     "hair-colouring": { 
      "_business_title_0": "service 1", 
      "_business_description_0": "service 1", 
      "_business_price_0": "22", 
      "_business_image_id_0": "2367", 
      "_business_price_1": "66", 
      "_business_image_id_1": "2367", 
      "_business_title_1": "service 2", 
      "_business_description_1": "service 2", 
      "_business_title_2": "service 3", 
      "_business_description_2": "service 3", 
      "_business_price_2": "33", 
      "_business_image_id_2": "2367" 
     } 
    } 
]'; 

$resultArray = []; 
$arrayFromJson = json_decode($json, 1); 

foreach ($arrayFromJson as $ind => $group) { 
    foreach ($group as $key => $array) { 
    asort($array); 
    foreach ($array as $arrayKey => $value) { 
     preg_match('/^_.*_(\d+)$/', $arrayKey, $match); 
     $resultArray[$ind][$key][$match[1]][$arrayKey] = $value; 
    } 
    } 
} 

echo json_encode($resultArray, JSON_PRETTY_PRINT); 

そして結果:

[ 
    { 
     "hair-and-beauty": [ 
      { 
       "_business_title_0": "service 1", 
       "_business_description_0": "service 1", 
       "_business_price_0": "22", 
       "_business_image_id_0": "2367" 
      }, 
      { 
       "_business_price_1": "66", 
       "_business_image_id_1": "2367", 
       "_business_title_1": "service 2", 
       "_business_description_1": "service 2" 
      }, 
      { 
       "_business_title_2": "service 3", 
       "_business_description_2": "service 3", 
       "_business_price_2": "33", 
       "_business_image_id_2": "2367" 
      } 
     ] 
    }, 
    { 
     "hair-colouring": [ 
      { 
       "_business_title_0": "service 1", 
       "_business_description_0": "service 1", 
       "_business_price_0": "22", 
       "_business_image_id_0": "2367" 
      }, 
      { 
       "_business_price_1": "66", 
       "_business_image_id_1": "2367", 
       "_business_title_1": "service 2", 
       "_business_description_1": "service 2" 
      }, 
      { 
       "_business_title_2": "service 3", 
       "_business_description_2": "service 3", 
       "_business_price_2": "33", 
       "_business_image_id_2": "2367" 
      } 
     ] 
    } 
] 
+0

これは素晴らしいことであり、私に多くの時間を救ってくれました。 – Diego

+0

@Diego問題なし) – kRicha

+0

2番目の子ループは、依然として正しく構成されていません。それを並べ替えるために追加できるものがあることをご存知ですか? – Diego

1

このコードを見てください、それはあなたの最終的な結果

<?php 
$array = array (
    0 => 
    array (
    'hair-and-beauty' => 
    array (
     '_business_title_0' => 'service 1', 
     '_business_description_0' => 'service 1', 
     '_business_price_0' => '22', 
     '_business_image_id_0' => '2367', 
     '_business_price_1' => '66', 
     '_business_image_id_1' => '2367', 
     '_business_title_1' => 'service 2', 
     '_business_description_1' => 'service 2', 
     '_business_title_2' => 'service 3', 
     '_business_description_2' => 'service 3', 
     '_business_price_2' => '33', 
     '_business_image_id_2' => '2367', 
    ), 
), 
    1 => 
    array (
    'hair-colouring' => 
    array (
     '_business_title_0' => 'service 1', 
     '_business_description_0' => 'service 1', 
     '_business_price_0' => '22', 
     '_business_image_id_0' => '2367', 
     '_business_price_1' => '66', 
     '_business_image_id_1' => '2367', 
     '_business_title_1' => 'service 2', 
     '_business_description_1' => 'service 2', 
     '_business_title_2' => 'service 3', 
     '_business_description_2' => 'service 3', 
     '_business_price_2' => '33', 
     '_business_image_id_2' => '2367', 
    ), 
), 
); 
echo '<pre>'; 
print_R($array); 
$finalResult = []; 
foreach($array as $key => $level1) { 
    foreach($level1 as $level2_key => $level2) { 
     foreach($level2 as $level3_key => $level3) { 
      $lastchar = substr($level3_key, -1); 
      $finalResult[$level2_key][$lastchar][$level3_key] = $level3; 
     } 
    } 
} 
print_R($finalResult); 
?> 

を与えます最終結果:

Array 
(
    [hair-and-beauty] => Array 
     (
      [0] => Array 
       (
        [_business_title_0] => service 1 
        [_business_description_0] => service 1 
        [_business_price_0] => 22 
        [_business_image_id_0] => 2367 
       ) 

      [1] => Array 
       (
        [_business_price_1] => 66 
        [_business_image_id_1] => 2367 
        [_business_title_1] => service 2 
        [_business_description_1] => service 2 
       ) 

      [2] => Array 
       (
        [_business_title_2] => service 3 
        [_business_description_2] => service 3 
        [_business_price_2] => 33 
        [_business_image_id_2] => 2367 
       ) 

     ) 

    [hair-colouring] => Array 
     (
      [0] => Array 
       (
        [_business_title_0] => service 1 
        [_business_description_0] => service 1 
        [_business_price_0] => 22 
        [_business_image_id_0] => 2367 
       ) 

      [1] => Array 
       (
        [_business_price_1] => 66 
        [_business_image_id_1] => 2367 
        [_business_title_1] => service 2 
        [_business_description_1] => service 2 
       ) 

      [2] => Array 
       (
        [_business_title_2] => service 3 
        [_business_description_2] => service 3 
        [_business_price_2] => 33 
        [_business_image_id_2] => 2367 
       ) 

     ) 

) 
関連する問題