douyanqu9722
2018-05-07 00:42
浏览 114
已采纳

将带有foreach的数组放在另一个数组中

I've just achieved a code where I make an array with foreach and in the same code I already have an existing array. What I want to do is to make only one array with those two results. Here is my code for the first array :

$sql = "SELECT photoprofile,username from photo WHERE username IN ('somearray')";


$resol = array();
$resulol = mysqli_query($con,$sql);

$photos = mysqli_fetch_all($resulol, MYSQLI_ASSOC);
$photos = array_column($photos, "photoprofile", "username");
foreach ( $restest as $user ) {
  if ( isset($photos[$user])) {
     $res[] = $photos[$user];
 }
 else    {
     $res[] = '';
 };
}

And here is my second array :

while($row = mysqli_fetch_array($result)){    
  array_push($res2, array(
  "name"=>$row['name'],
  "publisher"=>$row['username'],
  "image"=>$row['photo'],
 )
);}

If you have any tips, any comment or even a question (if I wasn't clear enough for you), just ask me ! Thanks !

Edit : What I want to make is an array of this type :

[{"name":"usera","publisher":"Jeana","image":"urla",""photouser","url2a"},{"name":"userb","publisher":"Jeanb","image":"urlb","photouser","url2b"}]

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongtan9253 2018-05-07 10:34
    已采纳

    I finally achieve what I wanted to do, by using a replacement technic : With the foreach loop, I've created an array with the parameter profilepic, on the other array, I've created another parameter with a default value, then I replaced it. My code : First my foreach loop :

    foreach ( $restest as $user ) {
      if ( isset($photos[$user])) {
         array_push($res1, array(
         "profilepic"=>$photos[$user]));
     }
     else    {
       array_push($res1, array(
       "profilepic"=>'defaultvalue'));;
     };
    }
    

    and then I changed :

    while($row = mysqli_fetch_array($result)){    
      array_push($res2, array(
      "name"=>$row['name'],
      "publisher"=>$row['username'],
      "image"=>$row['photo'],
      "profilepic"=>'defaultvalue'
     )
    );}
    

    And finally I replaced it by :

    $res = array_replace_recursive($res2,$res1);
    

    So my final array with echo json_encode($res) is:

    [{"name":"usera","publisher":"Jeana","image":"urla",""profilepic","url2a"},{"name":"userb","publisher":"Jeanb","image":"urlb","profilepic","defaultvalue"}]

    点赞 打赏 评论
  • douzai6337 2018-05-07 04:32

    You need to use the key you have in the first portion:

    $sql = "SELECT photoprofile,username from photo WHERE username IN ('somearray')";
    $resol = array();
    $resulol = mysqli_query($con,$sql);
    $photos = mysqli_fetch_all($resulol, MYSQLI_ASSOC);
    $photos = array_column($photos, "photoprofile", "username");
    # Set the username--vvvevvvvv
    foreach($restest as $username => $user) {
        # Store the username as the key here for reference later
        $res[$username] = (isset($photos[$user]))? $photos[$user] : '';
    }
    

    In this array, reference the username from the other array:

    while($row = mysqli_fetch_array($result)){    
        $res2[] = array(
            "name"=>$row['name'],
            "publisher"=>$row['username'],
            "image"=>$row['photo'],
            # Using the username as the key name, see if it $res has a saved value
            "photouser" => (isset($res[$row['username']]))? $res[$row['username']] : false
        );
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题