PHP-Foreach对象
我试图在视图中循环遍历这个数据对象。但是,我得到了以下错误:PHP-Foreach对象,php,arrays,api,twitter,laravel-5,Php,Arrays,Api,Twitter,Laravel 5,我试图在视图中循环遍历这个数据对象。但是,我得到了以下错误:尝试获取非对象的属性。这是我第一次真正遇到渲染数组/对象的问题…可能只是星期天的情况…但我无法将此foreach输出。帮忙编辑:我将键/值“search\u type”添加到返回的数组中-我认为这可能是导致问题的原因 $results = $twitter->get("users/search", ["q" => $term, "count" => $num, "include_entities" => 'fal
尝试获取非对象的属性
。这是我第一次真正遇到渲染数组/对象的问题…可能只是星期天的情况…但我无法将此foreach
输出。帮忙编辑:我将键/值“search\u type”添加到返回的数组中-我认为这可能是导致问题的原因
$results = $twitter->get("users/search", ["q" => $term, "count" => $num, "include_entities" => 'false']);
$results['search_type'] = 'people';
返回:
{
"0": {
"id": 1108217089,
"id_str": "1108217089",
"name": "Marketing",
"screen_name": "SocialMedia246",
"location": "Canada",
"description": "Marketing|PR|Brand Consultant.Lover of Music|Fashion & life.A dynamic young entrepreneur with an unconventional out of the box approach to marketing & business.",
},
"1": {
...
},
search_type: "people"
Foreach:
@foreach ($search_results as $result => $profile)
<div class="large-3 columns">
{{ $profile->id }}
</div>
@endforeach
我相信你没有正确阅读文件 请尝试以下循环:
<?php
$sInFile = 'in3.json';
$sOutFile = 'out3.json';
$sRaw = file_get_contents( $sInFile );
$aData = json_decode( $sRaw, 1 );
// $aData = array();
// $aTmp = array();
// $aTmp[ 'id' ] = '1108217089';
// $aTmp[ 'id_str' ] = '1108217089';
// $aTmp[ 'name' ] = 'Marketing';
// $aTmp[ 'screen_name' ] = 'SocialMedia246';
// $aTmp[ 'location' ] = 'Canada';
// $aTmp[ 'description' ] = 'Marketing';
// $aData[] = ( object ) $aTmp;
// $aTmp = array();
// $aTmp[ 'id' ] = '1108217089';
// $aTmp[ 'id_str' ] = '1108217089';
// $aTmp[ 'name' ] = 'Marketing';
// $aTmp[ 'screen_name' ] = 'SocialMedia246';
// $aTmp[ 'location' ] = 'Canada';
// $aTmp[ 'description' ] = 'Marketing';
// $aData[] = ( object ) $aTmp;
foreach ($aData as $result => $profile)
{
var_dump( $result );
var_dump( $profile );
var_dump( $profile->id );
}
var_dump( $aData );
$sNewJson = json_encode( $aData );
var_dump( $sNewJson );
file_put_contents( $sOutFile, $sNewJson );
?>
我相信您没有正确读取文件 请尝试以下循环:
<?php
$sInFile = 'in3.json';
$sOutFile = 'out3.json';
$sRaw = file_get_contents( $sInFile );
$aData = json_decode( $sRaw, 1 );
// $aData = array();
// $aTmp = array();
// $aTmp[ 'id' ] = '1108217089';
// $aTmp[ 'id_str' ] = '1108217089';
// $aTmp[ 'name' ] = 'Marketing';
// $aTmp[ 'screen_name' ] = 'SocialMedia246';
// $aTmp[ 'location' ] = 'Canada';
// $aTmp[ 'description' ] = 'Marketing';
// $aData[] = ( object ) $aTmp;
// $aTmp = array();
// $aTmp[ 'id' ] = '1108217089';
// $aTmp[ 'id_str' ] = '1108217089';
// $aTmp[ 'name' ] = 'Marketing';
// $aTmp[ 'screen_name' ] = 'SocialMedia246';
// $aTmp[ 'location' ] = 'Canada';
// $aTmp[ 'description' ] = 'Marketing';
// $aData[] = ( object ) $aTmp;
foreach ($aData as $result => $profile)
{
var_dump( $result );
var_dump( $profile );
var_dump( $profile->id );
}
var_dump( $aData );
$sNewJson = json_encode( $aData );
var_dump( $sNewJson );
file_put_contents( $sOutFile, $sNewJson );
?>
你的问题是你试图从string人那里获得id
属性你的问题是你试图从string人那里获得id
属性好吧,所以@potfur在他的评论中提出了一个很好的观点。我不再添加键search\u type
,而是将结果保存到people
数组中,如下所示。省省我的麻烦和头痛吧。谢谢你们的帮助,伙计们。这是一个我本该意识到的错误
$results['people'] = $twitter->get("users/search", ["q" => $term, "count" => $num, "include_entities" => 'false']);
vardump现在是(这更有意义):
好的,@potfur在他的评论中提出了一个很好的观点。我不再添加键search\u type
,而是将结果保存到people
数组中,如下所示。省省我的麻烦和头痛吧。谢谢你们的帮助,伙计们。这是一个我本该意识到的错误
$results['people'] = $twitter->get("users/search", ["q" => $term, "count" => $num, "include_entities" => 'false']);
vardump现在是(这更有意义):
你能在$search\u结果上做一个var\u转储并确保它是一个对象而不是原始的json字符串吗?刚刚做了-它是一个数组更改$profile->id
到$profile['id']
做到了这一点,致命错误:不能将stdClass类型的对象作为数组使用
“search\u type”=>“people”与数组类型的结构混合-我建议将其上移一级您可以对$search\u结果执行var_转储并确保它是一个对象而不是原始json字符串吗?刚刚做了-它是一个数组更改$profile->id
到$profile['id']
做到了这一点,致命错误:无法将stdClass类型的对象用作数组
“搜索类型”=>“人”“与数组类型的结构混合-我建议将其上移一级是的,我正要写…这没有意义。我将从这个数组中删除search_type键-省去我的麻烦。用我的逻辑在其他地方工作。是的,我正要写……这没有意义。我将从这个数组中删除search_type键-省去我的麻烦。在我的逻辑中,在别处工作。