首页 > 解决方案 > Exploding tier 2 element of a PHP/Typescript array

问题描述

I have this PHP code which is formatting the array for me,

    $data  = $dataQuery->fetchAll(PDO::FETCH_ASSOC);
    if(count($data)){
        $data_arr=array();
        $data_arr["records"]=array();
        $data_arr["records"] = $data;
    return $response
                ->withHeader('Content-Type','application/json')
                ->withJson($data_arr, 200); 
    }

and my array is in format,

"records": [
{
"ID": "1",
"LOCATION": "LOC1#LOC2#LOC3",
"TIMEFRAME": "1601-1650",
"TYPE": "mythological#genre#portrait"
},
{
"ID": "2",
"LOCATION": "LOC2",
"TIMEFRAME": "1851-1900",
"TYPE": "landscape"
}
]

And i'm trying to format this like following by exploding all the #

"records": [
{
"ID": "1",
"LOCATION": [{LOC1, LOC2, LOC3}],
"TIMEFRAME": "1601-1650",
"TYPE": [{mythological, genre, portrait}]
},
{
"ID": "2",
"LOCATION": "LOC2",
"TIMEFRAME": "1851-1900",
"TYPE": "landscape"
}
]

Like this,

    //$data  = $dataQuery->fetchAll(PDO::FETCH_ASSOC);
    if($count['COUNT']>0/* &&count($data) */){
        $data_arr=array();
        $data_arr["records"]=array();

        //$data_arr["records"] = $data;
        while ($row = $dataQuery->fetch(PDO::FETCH_ASSOC)){
            extract($row);
            $row_array=array();
            foreach ($row as $row_el){ 
                array_push($row_array, explode('#', $row_el));
            } 
            array_push($data_arr["records"], $row_array);
        }

    return $response
                ->withHeader('Content-Type','application/json')
                ->withJson($data_arr, 200); 
    }

But this is generating following result,

"records":[ 
   [ 
      [ 
         "1"
      ],
      [ 
         "LOC1",
         "LOC2",
         "LOC3"
      ],
      [ 
         "1601-1650"
      ],
      [ 
         "mythological",
         "genre",
         "portrait"
      ]
   ],
   [ 
      [ 
         "2"
      ],
      [ 
         "LOC2"
      ],
      [ 
         "1851-1900"
      ],
      [ 
         "landscape"
      ]
   ]
]

I am catching this response in Typescript/JavaScript so i can format this in client side too.

标签: javascriptphparraystypescriptmultidimensional-array

解决方案


虽然我不确定单独定位特定值而不是处理 who 记录是否更容易。

基本上,您需要确保使用原始密钥以及数据将数据推回。

最后一部分是,如果只有 1 个元素(如 中"ID": "1",),则通过将原始值分配回它来停止它是一个数组......

foreach ($row as $key => $row_el){ 
    $expand = explode('#', $row_el);
    if ( count($expand) == 1 )  {
        $expand = $row_el;
    }
    $row_array[$key] = $expand;
} 

更新了更多编码级别的代码...

$SQL_RE=array(
    "COUNT" => "5",
    "FORM" => "1&#painting",
    "SCHOOL" => "1&#German",
    "LOCATION" => "1&#Alte Pinakothek, Munich##2&#Kunsthistorisches Museum, Vienna##3&#Private collection",
    "TIMEFRAME" => "1&#1601-1650",
    "TYPE" => "1&#mythological##2&#genre##3&#portrait");

$row_array = [];
foreach ($SQL_RE as $key => $row_el){
    $expand2 = [];
    $l2 = [];
    $expand = explode('##', $row_el);
    foreach ( $expand as $key2 => $element2 )    {
        $expand2 = explode("&#", $element2 );
        if ( count($expand2) == 1 )  {
            $l2[$key2] = $element2;
        }
        else {
            $l2[$expand2[0]] = $expand2[1];
        }
    }
    if ( count($l2) == 1 && count($expand2) != 2 )  {
        $l2 = $row_el;
    }
    $row_array[$key] = $l2;
} 

print_r($row_array);

推荐阅读