如何排列包含键值的数组结果

wbgh16ku  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(249)

我使用下面的代码从数据库中获取数据,它的工作很好,但我的问题是结果包含重复值,没有按我想要的方式排列。

$sql=mysqli_query($con,"select user_id, meta_key, meta_value FROM mohamiusermeta  where meta_key IN ('websiteurl', 'profile_photo', 'Office', 'address_user', 'agentarea', 'offertext' , 'officename', 'cover_photo', 'membertype') group by user_id");
$newarray = array();

while($row=mysqli_fetch_assoc($sql))
{

 $subArray=array();

    $output[$row['meta_key']] = $row['meta_value'];

    $subArray['user_id']=$row['user_id'];
        $subArray['Office']=$output['Office']; 

        $subArray['officename']=$output['officename']; 
        $subArray['address_user']=$output['address_user']; 
        $subArray['profile_photo']=$output['profile_photo']; 
        $subArray['cover_photo']=$output['cover_photo']; 
        $subArray['agentarea']=$output['agentarea']; 
            $subArray['offertext']=$output['offertext']; 
            $subArray['websiteurl']=$output['websiteurl']; 
            $subArray['membertype']=$output['membertype']; 

    $newarray[]= array_filter($subArray);

    }

     json_encode($newarray);

     echo(json_encode($newarray,JSON_UNESCAPED_UNICODE));

     mysqli_close();

结果呢

1
 user_id    "1"
 Office "مؤسسة السنان للعسل الطبيعي ( عمان )"
address_user    "سلطنة عمان - بانوراما مول"
2   
  user_id   "1"
 Office "مؤسسة السنان للعسل الطبيعي ( عمان )"
address_user    "سلطنة عمان - بانوراما مول"
profile_photo   "profile_photo.jpg"
3   
 user_id    "1"
Office  "مؤسسة السنان للعسل الطبيعي ( عمان )"
address_user    "سلطنة عمان - بانوراما مول"
profile_photo   "profile_photo.jpg"
cover_photo "cover_photo.jpg"

但我想得到

1
 user_id: "1"
 Office: "officename"
 profile_photo: "profile.jpg
 user_address: "address"
 websiteurl: "url"
 ........

 2
 user_id: "2"
 Office: "officename"
 profile_photo: "profile.jpg
 user_address: "address"
 websiteurl: "url"

提前谢谢

anauzrmj

anauzrmj1#

首先,您的代码将出现sql错误。这是因为您选择了字段 user_id, meta_key, meta_value 但是你把结果按 user_id . 不允许直接选择未分组的字段。要解决此问题,请删除 GROUP BY 条款。您需要在php中以编程方式对结果进行分组。
在代码中遇到的问题是 $output 数组同时访问 while 循环。

while($row=mysqli_fetch_assoc($sql)) {
    $subArray=array();

    $output[$row['meta_key']] = $row['meta_value'];

    $subArray['user_id']=$row['user_id'];

    $subArray['Office']=$output['Office']; 
    ...
    $subArray['membertype']=$output['membertype']; 

    $newarray[]= array_filter($subArray);
}

如您所见,您只填充了 $output 数组,同时访问每行的所有字段。
相反,您要做的是将所有行按它们的名称分组 user_id . 为此,您可以使用 user_id 作为输出数组中的键,并将所有元数据添加到它引用的内部数组中:

$newarray = [];
while($row=mysqli_fetch_assoc($sql)) {
    if (!array_key_exists($row['user_id'], $newarray) {
        // Adds a new inner array for the specific user and initializes it with the user_id
        $newarray[$row['user_id']] = ['user_id' => $row['user_id']];
    }
    if (!empty($row['meta_value']) || is_numeric($row['meta_value'])) {
        // Adds a specific meta value to an inner user array
        $newarray[$row['user_id']][$row['meta_key']] = $row['meta_value'];
    }
}

使用此代码,您将获得所需的输出。通过使用 empty() 结合 is_numeric() 作为向内部数组添加元值的一个条件,您不需要使用 array_filter() ,因为不会存储错误值。如果你的元值可能也是布尔值,你应该加上 || is_boolean() 这种情况。

相关问题