[PHP] [JSON]错误的生成数组

时间:2016-02-20 11:52:12

标签: php arrays json

我有几天无法解决的问题。我需要以这种格式创建以Json编码的数组:

[
  {
"city": "Wroclaw",
"data": [
  {
    "id": "#",
    "name": "Zlote tarasy",
    "city": "Wroclaw",
    "post_code": "50-545",
    "street": "Ko\u015bciuszki",
    "house_no": "2",
    "flat_no": "4",
    "opening_hours": "8:00 - 16:00",
    "latitude": 52.2330649,
    "longitude": 20.9207689,
    "description": "Opis sklepu"
  },
  {
    "id": "#",
    "name": "Arkadia",
    "city": "Wroclaw",
    "post_code": "50-545",
    "street": "Ko\u015bciuszki",
    "house_no": "2",
    "flat_no": "4",
    "opening_hours": "8:00 - 16:00",
    "latitude": 52.2571437,
    "longitude": 20.9822873,
    "description": "Opis sklepu"
  }
   ]
  }]

这是我的生成数组方法:

  public function getCollectionAsArray()
{
    $stores = $this->getStoreCollection();

    $jsonArray = array();

    foreach ($stores as $store) {

        $jsonArray[] = array(
            'city' => $store->getCity(),
            'data' => array(
                [
                    'id'          => $store->getIdentifier(),
                    'name'        => $store->getName(),
                    'post_code'   => $store->getPostCode(),
                    'street'      => $store->getStreet(),
                    'house_no'    => $store->getHouseNo(),
                    'flat_no'     => $store->getFlatNo(),
                    'latitude'    => $store->getLatitude(),
                    'longitude'   => $store->getLongitude(),
                    'description' => $store->getDescription()
                ],
            ),
        );
    }

    return $jsonArray;
}

它正在运作,但是当我有两个相同的城市时,它会创建新的城市,而不是将数据添加到数据'阵列:

array (size=4)
 0 =>
array (size=2)
  'city' => string 'Wrocław' (length=8)
  'data' =>
    array (size=1)
      0 =>
        array (size=9)
          ...
 1 =>
array (size=2)
  'city' => string 'Wrocław' (length=8)
  'data' =>
    array (size=1)
      0 =>
        array (size=9)

我尝试使用array_merge和array_push但没有成功。非常感谢你的帮助:))

2 个答案:

答案 0 :(得分:0)

一种方法可能是

public function getCollectionAsArray()
{
    $stores     = $this->getStoreCollection();    
    $jsonArray  = array();  
    $i = 0;     
    foreach ($stores as $store) 
    {    
        $jsonArray[$store->getCity()][] = array( 
                    'id'          => $store->getIdentifier(),
                    'name'        => $store->getName(),
                    'post_code'   => $store->getPostCode(),
                    'street'      => $store->getStreet(),
                    'house_no'    => $store->getHouseNo(),
                    'flat_no'     => $store->getFlatNo(),
                    'latitude'    => $store->getLatitude(),
                    'longitude'   => $store->getLongitude(),
                    'description' => $store->getDescription()
                );
    } 
    $final = array();
    foreach($jsonArray as $key => $json)
    {
        $getkey = myfunction($final,"city",$key);
        if( $getkey )
        {
            $final[$getkey]["data"][] = $json; 
        }
        else
        {
            $final[] = array("city" => $key , "data" => array( 0 => $json ) );
        }
    }
    return $final;
}

function myfunction($products, $field, $value)
{
   foreach($products as $key => $product)
   {
      if ( $product[$field] === $value )
         return $key;
   }
   return false;
}

答案 1 :(得分:0)

有一种简单的方法可以解决您的问题。一个想法是创建一个PHP数组并使用json_encode($your_array_php)。像那样:

$jsonArray = array(
        'city' => $store->getCity(),
        'data' => array(
                'id'          => $store->getIdentifier(),
                'name'        => $store->getName(),
                'post_code'   => $store->getPostCode(),
                'street'      => $store->getStreet(),
                'house_no'    => $store->getHouseNo(),
                'flat_no'     => $store->getFlatNo(),
                'latitude'    => $store->getLatitude(),
                'longitude'   => $store->getLongitude(),
                'description' => $store->getDescription()
        ),
    );
    $jsonArray = json_encode($jsonArray);