php array with json



我正在使用像这样的angularJS控制器

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阵列,我只能返回该元素的城市名称。

    $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 

如果 $id包含 customer_array中的元素索引,则就像

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

如果 $id包含 customer_array中的名称元素,则就像

$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);

您可以使用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?

使用response.data,可以在您的AngularJS脚本中访问此回声。在那里,您将拥有一个包含客户实例的JSON对象。但是,恕我直言,您存储客户信息的数据结构不是很好。如果有两个客户相同名称 ??

"对于客户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"

最新更新