返回字符串的php文件json编码没有工作

时间:2017-06-19 14:37:52

标签: php mysql web

我有一个php文件,它给我作为一个字符串需要的响应,我需要它是一个在json中编码的数组,即时通讯不是很好的PHP,任何人都可以帮助吗? 这就是它如何作为一个字符串正常工作   - printf("user: \"%s\" \"%s\" email: \"%s\" \n", $row['firstname'], $row['lastname'],$row['email']);

但我需要它作为json中的数组,这就是我试图做到的 - array_push($mynewArray,array("firstname \%s"=>$row['firstname'],"lastname \%s"=>$row['lastname'],"email \%s"=>$row['email'])); echo json_encode($mynewArray);

this is the screen shot that shows how i tried to do it

2 个答案:

答案 0 :(得分:0)

这可能是你之后的事情:

<?php
$cluster = Cassandra::cluster()
                ->build();
$keyspace = 'msata';
$session = $cluster->connect($keyspace);

$result = $session->execute(new Cassandra\SimpleStatement
            ("SELECT * FROM msata.users")
          );

// Create the variable array here so it's used correctly in the loop
$mynewArray = array();

foreach( $result as $row ){

    // Create an array for each individual user
    $user_array = array( 'firstname' => $row['firstname'], 'lastname' => $row['lastname'], 'email' => $row['lastname'] );

    // Add the array to the main array
    $mynewArray = array_push( $mynewArray, $user_array );

}

echo json_encode( $mynewArray );

答案 1 :(得分:0)

我认为你可以尝试这样做,假设记录集是在var $row

中正确生成的
$mynewArray=array();
foreach($result as $row){
    $mynewArray[]=$row;
}
echo json_encode($mynewArray);


Or

$mynewArray=array();
foreach( $result as $row ){
    $mynewArray[]=array(
        'firstname'=>$row['firstname'],
        'lastname'=>$row['lastname'],
        'email'=>$row['email'],
    );
}
echo json_encode($mynewArray);