Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.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 array\u push创建的array运行异常。尝试输出关联数组时,值返回数组的第一个字母或;A「;_Php_Arrays - Fatal编程技术网

Php array\u push创建的array运行异常。尝试输出关联数组时,值返回数组的第一个字母或;A「;

Php array\u push创建的array运行异常。尝试输出关联数组时,值返回数组的第一个字母或;A「;,php,arrays,Php,Arrays,我试图使用foreach来输出下面的数组。我通过基于preg\u match if/else的array\u push()创建了这个数组 Array ( [0] => Array ( [date] => [clickurl] => some data [url] => some data [dispurl] => some Data... [title] => Trans

我试图使用foreach来输出下面的数组。我通过基于preg\u match if/else的array\u push()创建了这个数组

Array (
    [0] => Array (
        [date] => 
        [clickurl] => some data
        [url] => some data
        [dispurl] => some Data...
        [title] => Transformers: Revenge of the Fallen : Reviews
        [abstract] => "Transformers: Revenge of the Fallen" is a horrible experience of unbearable length, briefly punctuated by three or four amusing moments.
    )

    [1] => Array (
        [date] => 
        [clickurl] => some data
        [url] => some data
        [dispurl] => some Data...
        [title] => Transformers : Reviews
        [abstract] => After a string of bad-to-mediocre films, director Michael Bay scores with blockbuster battling robots in "Transformers."
    )
)
尝试输出阵列时:

foreach ($reviewArr as $review) {
    echo($review['clickurl']. '<br/><br/>');
}
输出为:

Array ( 
    [date] => 
    [clickurl] => some data 
    [url] => some data
    [dispurl] => some data... 
    [title] => Transformers : Reviews 
    [abstract] => After a string of bad-to-mediocre films, director Michael Bay scores with blockbuster battling robots in "Transformers." 
) 
我不知道为什么会这样。任何帮助都将不胜感激

谢谢

更新=

这是我在下面解析的原始数组,将其拆分为两个不同的数组

Array
    (
[bossresponse] => Array
    (
        [responsecode] => 200
        [web] => Array
            (
                [start] => 0
                [count] => 14
                [totalresults] => 14
                [results] => Array
                    (
                        [0] => Array
                            (
                                [date] => 
                                [clickurl] => http://url.com/1
                                [url] => http://url.com/1
                                [dispurl] => http://url.com/1...
                                [title] => Title of Content 1
                                [abstract] => This is the summary, This is the summary,    This is the summary, ...
                            )

                        [1] => Array
                            (
                                [date] => 
                                [clickurl] => http://url.com/2
                                [url] => http://url.com/2
                                [dispurl] => http://url.com/2...
                                [title] => Title of Content 2
                                [abstract] => This is the summary, This is the summary,  This is the summary, ...
                            )
                    )

              )

       )

)
这就是我设置$reviewArr[]的方式

foreach ($results['bossresponse']['web']['results'] as $key => $result) {
                $url = $result['clickurl'];
                $title = $result['title'];
                $abstract = $result['abstract'];
                $resultItem = print_r($results['bossresponse']['web']['results'][$key], true);
                if (preg_match ("/reviews/i", "$url")) {
                    array_push($reviewArr, "$resultItem");
                } else {
                    array_push($resultsArr, "$resultItem");
                }
            }
更新#2-

多亏了@fabio,我才用上面的
$resultItem
设置了一个字符串。如何创建多维数组?如何将其构建为一个数组-我的所有尝试都返回了错误或字符串

 Array
                    (
                        [0] => Array
                            (
                                [date] => 
                                [clickurl] => http://url.com/1
                                [url] => http://url.com/1
                                [dispurl] => http://url.com/1...
                                [title] => Title of Content 1
                                [abstract] => This is the summary, This is the summary,    This is the summary, ...
                            )

                        [1] => Array
                            (
                                [date] => 
                                [clickurl] => http://url.com/2
                                [url] => http://url.com/2
                                [dispurl] => http://url.com/2...
                                [title] => Title of Content 2
                                [abstract] => This is the summary, This is the summary,  This is the summary, ...
                            )
                    )

我不知道你是如何在你的阵列上推送物品的,但我认为这可以做到:

foreach ($reviewArr as $index=>$review) {
    echo($review['clickurl']. '<br/><br/>');
}
foreach($revieweras$index=>$review){
echo($review['clickurl'].

); }
更改:

foreach ($results['bossresponse']['web']['results'] as $key => $result) {
    $url = $result['clickurl'];
    $title = $result['title'];
    $abstract = $result['abstract'];
    $resultItem = print_r($results['bossresponse']['web']['results'][$key], true);
    if (preg_match ("/reviews/i", "$url")) {
        array_push($reviewArr, "$resultItem");
    } else {
        array_push($resultsArr, "$resultItem");
    }
}
致:


为什么要用print\u r的返回值填充数组

// this will return a string
$resultItem = print_r($results['bossresponse']['web']['results'][$key], true);
这就是您行为的原因:您正在打印返回值的第一个字符,它是
$results[…][…]
数组的字符串表示形式

你自己试试看

$foo = "Hello world";
echo $foo[0]; // output H

附则
echo($review.

将输出整个字符串,即打印输出。

您可以添加从
var\u dump($reviewArr)获得的输出吗$reviewArr
的开头从数组中输出单个“A”我更新了原始问题,说明了我如何设置数组……我假设我使用的是print\r??我做了很多尝试,但都失败了。谢谢@Fabio的回复。我试图使用
$results['bossresponse']['web']['results'][$key]
来构建新的多维数组,但没有成功。你能提供一些关于如何分配给新数组的建议吗?你在foreach中,可以使用
$result
作为整个子数组。您的
preg\u match
需要针对
$result['url']
运行,您应该使用
array\u push($reviews,$result)
将其分配给另一个数组。您应该阅读一些关于数组的PHP教程;-)
// this will return a string
$resultItem = print_r($results['bossresponse']['web']['results'][$key], true);
$foo = "Hello world";
echo $foo[0]; // output H