【问题标题】:How to make variable that's in an array format into an array如何将数组格式的变量转换为数组
【发布时间】:2013-05-04 13:59:03
【问题描述】:

这可能是一个简单的问题,但是我如何将如下所示的变量放入数组中。

    $hot = "It","is","hot","outside";

执行以下操作无效:

    $newhot = array($hot);

我实际上调用的 API 如下所示:

    [["P0010001","NAME","state","zip code tabulation area"],
    ["68191","ZCTA5 99301","53","99301"]]

我需要的是第二行的人口(第一个引号)。

执行以下操作会给我 "68191","ZCTA5 99301","53","99301"

    $splitContent = implode("\n",array_slice(explode("\n",$populate),1,2));
    $newContent = str_replace(']','',$splitContent);
    $newContent = str_replace('[','',$newContent);

【问题讨论】:

  • 这看起来像是语法错误。
  • $hot = "It","is","hot","outside";不是变量
  • json_decode($populate); ?
  • 正在做:$NewContent = json_decode($populate);回声 $NewContent[0];返回页面上的数组

标签: php arrays api variables


【解决方案1】:

这个

$hot = "It","is","hot","outside";

会在 PHP 中产生错误。但假设您从 API 中检索到以下内容:

$str='[["P0010001","NAME","state","zip code tabulation area"],["68191","ZCTA5 99301","53","99301"]]';

那么,如果你运行这一行:

$myArray = json_decode($str);

然后

echo "<pre>";
print_r($myArray);
echo"</pre>";

你可以得到这样的结果:

Array
(
    [0] => Array
        (
            [0] => P0010001
            [1] => NAME
            [2] => state
            [3] => zip code tabulation area
        )

    [1] => Array
        (
            [0] => 68191
            [1] => ZCTA5 99301
            [2] => 53
            [3] => 99301
        )

)

第二行数据会存放在

$myArray[1]

【讨论】:

    【解决方案2】:

    定义一个数组类似于...

    $hot = array("It","is","hot","outside");
    

    回复:您的 Api 调用...

    $ApiResponse = '[["P0010001","NAME","state","zip code tabulation area"],["68191","ZCTA5 99301","53","99301"]]';
    
    $Response = json_decode($ApiResponse);
    $Data = $Response[1];
    

    具体来说,api 正在返回一个列表列表。我们正在使用第二个(0 索引)列表。 $Data 现在将与您声明的相同...

    $Data = array("68191","ZCTA5 99301","53","99301");
    

    编辑:测试代码...

    $Key = '[Your Key]';
    $ApiResponse = file_get_contents("http://api.census.gov/data/2010/sf1?key={$Key}&get=P0010001,NAME&for=zip+code+tabulation+area:99301&in=state:53");
    
    print "Raw: " . print_r($ApiResponse, true) . "<hr/>";
    
    $Response = json_decode($ApiResponse);
    $Data = $Response[1];
    print "Extracted Data: " . print_r($Data, true) . "<br/>";
    
    print "First bit of data: {$Data[0]}.<br/>";
    print "Second bit of data: {$Data[1]}.<br/>";
    

    【讨论】:

    • 我做不到第一个。结果已经在一个变量中。我需要把变量中的结果变成一个数组。
    • 第一个(已编辑的)代码 sn-p 中的语法无效。如果 all 结果在一个变量中,它要么是一个数组(问题已解决),要么是一个 json 数组的字符串表示形式 - 在这种情况下,请使用 json_decode。不存在不是数组(或对象/列表/等)的具有多个值的变量
    • $populate = file_get_contents('apisite.com'); $NewContent = json_decode($populate)[1];回声 $NewContent[1]; -- 不工作.. 得到服务器错误。我不确定我做错了什么。
    • 你有两个[1]。如果您想查看整个对象,请使用print_r。像...file_get_contents('apisite.com'); $NewContent = json_decode($populate)[1]; print_r($NewContent);。您没有说明它是否有效或有问题?
    • 没有。它不工作。如果我将 [1] 放在 ($populate) 之后,我会收到服务器错误。如果我省略 [1] 并执行“echo $NewContent”,那么它会打印出“Array”这个词。如果我尝试执行“echo $NewContent[1]”,它也会返回服务器错误。
    猜你喜欢
    • 2013-01-24
    • 2011-08-23
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-23
    相关资源
    最近更新 更多