Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel Input::old是一个字符串形式的数组_Php_Laravel_Laravel 4 - Fatal编程技术网

Php Laravel Input::old是一个字符串形式的数组

Php Laravel Input::old是一个字符串形式的数组,php,laravel,laravel-4,Php,Laravel,Laravel 4,在我的表单中,我从Input::old('tags')输出一些json或一个数组数据字符串。我遇到的问题是能够测试它,看看它是否是json,是否是do foreach循环,并将标记属性放入数组,然后将数组内爆并输出到输入字段中 [{"id":112,"tag":"corrupti","tagFriendly":"corrupti","created_at":"2015-07-20 00:05:28","updated_at":"2015-07-20 00:05:28","pivot":{"que

在我的表单中,我从Input::old('tags')输出一些json或一个数组数据字符串。我遇到的问题是能够测试它,看看它是否是json,是否是do foreach循环,并将标记属性放入数组,然后将数组内爆并输出到输入字段中

[{"id":112,"tag":"corrupti","tagFriendly":"corrupti","created_at":"2015-07-20 00:05:28","updated_at":"2015-07-20 00:05:28","pivot":{"question_id":60,"tag_id":112,"created_at":"2015-07-20 00:05:28","updated_at":"2015-07-20 00:05:28"}},{"id":9,"tag":"placeat","tagFriendly":"placeat","created_at":"2015-07-20 00:05:23","updated_at":"2015-07-20 00:05:23","pivot":{"question_id":60,"tag_id":9,"created_at":"2015-07-20 00:05:28","updated_at":"2015-07-20 00:05:28"}}]
所以我的输出应该是这样的

'corrupti, placeat, etc...'
有人能帮我按照我刚才概述的步骤得到想要的结果吗

这个代码不起作用,但我需要这样做。is_数组始终为false

<?php
    $tags = Input::old('tags');
    if(is_array($tags)) {
        foreach ($tags as $tag) {
            $tags[] = $tag->tag;
        }
        $tags = implode(', ', $tags);   
    }                       
?>

您可以使用数组映射从对象中提取标记属性

<?php
$jsonObject = json_decode(Input::old('tags'));
if(is_array($jsonObject)) {
    $onlyTags = array_map(function($item) { return $item->tag; },$jsonObject);
    print implode(",", $onlyTags);
}

您可以使用array\u map从对象中提取标记属性

<?php
$jsonObject = json_decode(Input::old('tags'));
if(is_array($jsonObject)) {
    $onlyTags = array_map(function($item) { return $item->tag; },$jsonObject);
    print implode(",", $onlyTags);
}

尝试将true添加到json\u解码中

json_decode($tags, true);
根据应该返回关联数组的文档。

is_数组可能无法与具有键/值对的关联数组一起使用。尝试用此函数替换is_阵列:

function is_associate_array($array)
{
    return $array === array_values($array);
}
因此:


尝试将true添加到json_解码

json_decode($tags, true);
根据应该返回关联数组的文档。

is_数组可能无法与具有键/值对的关联数组一起使用。尝试用此函数替换is_阵列:

function is_associate_array($array)
{
    return $array === array_values($array);
}
因此:

给你

$tags = json_decode(Input::old('tags'), true); // decode json input as an array

if (is_array($tags)) {
    foreach ($tags as $key=>$value) {
        $tags[$key] = $value['tag'];
    }
    $tags = implode(', ', $tags);
    echo $tags;
}
给你

$tags = json_decode(Input::old('tags'), true); // decode json input as an array

if (is_array($tags)) {
    foreach ($tags as $key=>$value) {
        $tags[$key] = $value['tag'];
    }
    $tags = implode(', ', $tags);
    echo $tags;
}

json_decode将返回一个包含数组的对象,因此现在的数组应该返回false。即使我没有解码它,它也会返回false是的,因为它没有看到数组。您需要从对象中提取数组。尝试$obj=json\u解码($tags);打印($obj[0]);看看它显示了什么。json_decode将返回一个包含数组的对象,所以现在的数组应该返回false。即使我没有解码它也会返回false是的,因为它没有看到数组。您需要从对象中提取数组。尝试$obj=json\u解码($tags);打印($obj[0]);看看它显示了什么。它所在的部分是_array($jsonObject)返回false,这样代码就不会通过if语句显示这个东西的输出:
dd(Input::old('tags')它所在的部分是_array($jsonObject)返回false,这样代码就不会通过if语句显示这个东西的输出:
dd(Input::old('tags')
当您测试它是否为arraydon't believe is_数组时,它仍然返回false。请看我的编辑。你的函数
是\u associate\u array
做相反的事情。它应该命名为
is\u not\u associate\u array
。顺便说一句,is_数组
可以正常工作。
is_array(json_decode(Input::old('tags'),true))
不起作用的唯一原因是
Input::old('tags')
-不是json字符串当您测试它是否是数组时仍然返回false不相信is_array会起作用。请看我的编辑。你的函数
是\u associate\u array
做相反的事情。它应该命名为
is\u not\u associate\u array
。顺便说一句,is_数组
可以正常工作。
is_array(json_decode(Input::old('tags'),true))
不起作用的唯一原因是
Input::old('tags')
-不是json字符串