Php array\u push创建的array运行异常。尝试输出关联数组时,值返回数组的第一个字母或;A「;
我试图使用foreach来输出下面的数组。我通过基于preg\u match if/else的array\u push()创建了这个数组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
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)获得的输出吗你的问题是什么?不幸的是,我不能,我在一个恶劣的环境中,这是通过WSH。我可以做print\r,因为它有返回结果的true,所以我必须将它设置为一个变量,然后回显该变量。@NullUserException:我们会相互取消,因为这几乎得到了我的赞成票。print\r看起来像什么?这就是我在第一个问题/帖子页面顶部的内容。这是打印输出拷贝并粘贴。这将执行完全相同的操作。在不使用键的情况下对键和值进行迭代与只对值进行迭代是一样的。我可以同意@sberry2A的观点,上面是我最初使用的。这也会在$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