2017-04-22 98 views
2

我使用的是angularJS控制器這樣PHP數組JSON

app.controller('customersCtrl', function($scope, $http) { 
    $scope.element = function(num){ 
     var element_id = num;//num; 
     $http.get("customers.php",{params:{"id":element_id}}).then(function (response) { 
      $scope.myData = response.data; 
     } 
     ,function errorCallback(response){ 
      $scope.e=response.statustext; 
      console.log(e); 

     }); 

    }; 

}); 

這是我的PHP陣列

$id = $_GET[id]; 
    $customer_array = array(); 

    $customer1 = array(
     'name' => 'LoneStar', 
     'city' => 'Houston' 
    ); 

    $customer_array[] = $customer1; 

    $customer2 = array(
     'name' => 'SNHU', 
     'city' => 'Manchester' 
    ); 

    $customer_array[] = $customer2; 

    $customer3 = array(
     'name' => "Regis", 
     'city' => "Boulder" 
    ); 

客戶1陣列,我怎麼能只返回的只是城市名那一個元素。

+0

你爲什麼要使用許多陣列,而不是使用一個單一的陣列,並嘗試應用過濾條件並返回該對象單獨 – Aravind

+0

回聲json_encode($ customer_array);並得到迴應 – JYoThI

+0

echo $ customer_array [0] ['city']; – JYoThI

回答

0

您可以使用php的json_encode()函數完成此操作。

$id = $_GET[id];// Assuming this is the customer's name. If not??? 
$customer_array = array(); 

$customer1 = array(
    'name' => 'LoneStar', 
    'city' => 'Houston' 
); 

$customer_array[] = $customer1; 

$customer2 = array(
    'name' => 'SNHU', 
    'city' => 'Manchester' 
); 

$customer_array[] = $customer2; 

$customer3 = array(
    'name' => "Regis", 
    'city' => "Boulder" 
); 
$customer_rquired = array(); 
// Traverse the array to find the customer data 
foreach($customer_array as $key=>$customer_instance){ 
    if($customer_instance["name"] === $id){ 
     $customer_required["customer"] = $customer_instance; 
     echo json_encode($customer_required); 
     break; 
    } 
} 
//What you'd do if no customer is found? 

這呼應了可以在你的AngularJS腳本response.data訪問。在那裏,您將擁有一個包含客戶實例的JSON對象。 但恕我直言,你的數據結構存儲客戶信息不是很好。如果有兩個客戶同名怎麼辦?

0

如果$id包含元素在customer_array的INDEX,它會像

header("Content-type:application/json; charset=UTF-8"); 
echo json_decode($customer_array[$id]["city"]); 

如果$id包含在customer_array NAME元素,它會像

$customer = array(); 
$response = array(); 

foreach ($customer_array as $customer_element) { 
    if ($id == $customer_element["name"]) { 
     $customer = $customer_element; 
    } 
} 
if (count($customer)) { 
    $response = array("city" => $customer["city"]); 
}else{ 
    $response = array("error" => "element not found") 
} 


header("Content-type:application/json; charset=UTF-8"); 
echo json_decode($response); 
0
$id = $_GET[id]; // $id = 0 to get the value from the first array 
    $customer_array = array(); 

    $customer1 = array(
     'name' => 'LoneStar', 
     'city' => 'Houston' 
    ); 

    $customer_array[] = $customer1; 

    $customer2 = array(
     'name' => 'SNHU', 
     'city' => 'Manchester' 
    ); 

    $customer_array[] = $customer2; 

    $customer3 = array(
     'name' => "Regis", 
     'city' => "Boulder" 
    ); 

    echo $customer_array[$id]['city']; //the city value of the first array 
0

「爲客戶1陣列我怎麼才能返回只有一個元素的城市名稱

您的代碼:

$customer_array = array(); 

    $customer1 = array(
     'name' => 'LoneStar', 
     'city' => 'Houston' 
    ); 

    $customer_array[] = $customer1; 

    $customer2 = array(
     'name' => 'SNHU', 
     'city' => 'Manchester' 
    ); 

    $customer_array[] = $customer2; 

    $customer3 = array(
     'name' => "Regis", 
     'city' => "Boulder" 
    ); 

以上代碼使得$ customer_array存在三個顧客的:

$customer_array = array(
    array(//First item in array $customer_array 
     'name' => 'LoneStar', 
     'city' => 'Houston' 
    ), 
    array(//Second item in array $customer_array 
     'name' => 'SNHU', 
     'city' => 'Manchester' 
    ), 
    array(//Third item in array $customer_array 
     'name' => "Regis", 
     'city' => "Boulder" 
    ) 
); 

如果數組沒有定義鍵,在0和向上計數開始,例如。 0,1,2.3 ... 爲此$customer_array[0]包含數組:

array(//First item in array $customer_array 
      'name' => 'LoneStar', 
      'city' => 'Houston' 
     ) 

$customers_array[1]包含數組:

array(//Second item in array $customer_array 
      'name' => 'SNHU', 
      'city' => 'Manchester' 
     ) 

$customers_array[2]包含數組:

array(//Third item in array $customer_array 
     'name' => "Regis", 
     'city' => "Boulder" 
    ) 

我怎樣才能返回這一個元素的城市名稱?

例子:

$customers[0]['city'] would return value "Houston" 
$customers[1]['city'] would return value "Manchester" 
$customers[2]['city'] would return value "Boulder"