2017-06-19 23 views
-1

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

但我需要它作为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

+0

使用表单'的数组键姓名\%s'可能不会做出最有意义... – CBroe

回答

0

我想你可以这样尝试,假设记录集生成正确,并在var 0中可用

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

你救了我thannks你RamRaider – raddaoui

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