2014-02-15 217 views
-1

更新:所有值现在都返回“A”时,他们应该都是不同的值。 我想要做的是,通过邮寄请求发送一个值表到我的网站,该网站将表格数据保存到一个文本文件。我终于有了它,所以整个表打印到文本文件,但所有的值是A.我做错了什么?顺便说一下,我尝试了很多方法和许多其他“类似问题”,但都没有为我工作。提前致谢。PHP json_encode()返回空值

CODE:

<?php 
$foo = file_get_contents("php://input"); 
$stuff = json_decode($foo, true); 

function createtxt($cttext, $location,$stuff) 
{ 
    $ccontent = $cttext; 
    if (file_exists($location)) { 
     echo "The file $ffilename exists"; 
    } else { 
     $fp = fopen($location, "wb"); 
     fclose($fp); 
       $formdata = array(
       'user'=> $cttext['user'], 
       'secretkey'=> $cttext['secretkey'], 
       'isanadmin'=> $cttext['isa'], 
       'firsttime'=> $cttext['firsttime'], 
       'display'=> $cttext['dis'], 
       'test'=> $cttext['test'], 
       'test2'=> $cttext['test2'], 
       'type'=> $cttext['type'] 
       ); 
       $jsondata = json_encode($formdata); 
       file_put_contents($location, $jsondata); 
     echo 'Created User Text File Named "data.txt" '; 
    } 
} 

function lookforuser($NAME,$stuff) 
{ 
    if (!file_exists('users/' . $NAME)) 
     mkdir('users/' . $NAME); 
    echo 'User Folder Created '; 
    createtxt($NAME, 'users/' . $NAME . '/data.txt',$stuff); 
} 

lookforuser($stuff['user'],$stuff); 
?> 

输出文本文件:

{"user":"A","secretkey":"A","isanadmin":"A","firsttime":"A","display":"A","test":"A","test2":"A","type":"A"} 

回答

0

您传递$cttext,但获得$stuff

由于$stuff未定义,因此数组解引用也为null。

您可能想要通过$stuff作为第三个参数。

+0

解决! 现在输出为: {“user”:“ASCORE”,“secretkey”:“secretkeytheyentered”,“isanadmin”:“0”,“firsttime”:“1”,“display”:“0” test“:”Works!“,”test2“:”Works!“,”type“:”0“} 更新所有$ cttext []为$ stuff [] n更新脚本 – ASCORE

0

$stuff是从来没有自己的函数中定义。这是一个全局变量,但是你不会将它传递给你的函数;该函数仅知道其参数,内部初始化的变量以及通过global明确引用的变量。但是,由于您拨打lookforuser然后createtxt,您确实将内容$stuff更改为createtxt

所以,最好的办法解决这一问题:$cttext每次在功能createtxt使用$stuff及时更换$stuff。由于您编写代码的方式,您已经将$stuff的值传递给createtxt,所以这将起作用。

全码:

function createtxt($cttext, $location) 
{ 
    $ccontent = $cttext; 
    if (file_exists($location)) { 
     echo "The file $ffilename exists"; 
    } else { 
     $fp = fopen($location, "wb"); 
     fclose($fp); 
       $formdata = array(
       'user'=> $cttext['user'], 
       'secretkey'=> $cttext['secretkey'], 
       'isanadmin'=> $cttext['isa'], 
       'firsttime'=> $cttext['firsttime'], 
       'display'=> $cttext['dis'], 
       'test'=> $cttext['test'], 
       'test2'=> $cttext['test2'], 
       'type'=> $cttext['type'] 
       ); 
       $jsondata = json_encode($formdata); 
       file_put_contents($location, $jsondata); 
     echo 'Created User Text File Named "data.txt" '; 
    } 
} 

function lookforuser($NAME) 
{ 
    if (!file_exists('users/' . $NAME)) 
     mkdir('users/' . $NAME); 
    echo 'User Folder Created '; 
    createtxt($NAME, 'users/' . $NAME . '/data.txt'); 
} 

lookforuser($stuff['user']); 
?> 
+0

$ stuff是全局的,但它作为arg传递给'lookforuser()',它将它传递给另外两个函数。当他们通过调用链获得变量名时,我会被OP的完全混淆。 $ NAME - > $ cttext和whatnot。 –

+0

@MarcB不,它传递'$ stuff'的*值*,而不是变量本身。 '$ stuff'在'lookforuser'或'createtxt'中是未定义的,但$ stuff中的值可以分别通过$ NAME和$ cttext来访问。 –

+0

'lookforuser($ stuff ['name'],$ stuff)'。看起来像它传递给我...... –