【问题标题】:php array with json带有json的php数组
【发布时间】:2017-04-22 05:28:05
【问题描述】:

我正在使用这样的 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 数组,我怎样才能只返回那个元素的城市名称。

【问题讨论】:

  • 为什么要使用多个数组而不是使用单个数组并尝试应用过滤条件并单独返回该对象
  • 回显 json_encode($customer_array);并得到响应
  • echo $customer_array[0]['city'];

标签: php angularjs arrays


【解决方案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 

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      您可以使用 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 对象,其中包含客户实例。 但是恕我直言,您存储客户信息的数据结构不是很好。如果有两个客户同名怎么办?

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-17
          • 2012-03-25
          • 2018-09-06
          相关资源
          最近更新 更多