在字符串中返回foreach值

时间:2018-12-11 04:24:45

标签: php arrays

我有下面的代码。我试图遍历字符串中的数组并将结果返回为字符串的子部分。

480px

我的预期结果将与此类似

    $household = '{
                "data": {
                    "attributes": {
                        "name":"'.rgar( $entry, '1.6' ).'"
                    },
                    "relationships": {
                        "people":{
                            "data":[
                                '.
                                foreach ($family_ids[] as $fam_member){
                                    return '{"type":"Person","id":"'.$fam_member.'"}';
                                }.'
                            ]
                        },
                        "primary_contact":{
                            "data":{"type":"Person","id":"1"}
                        }
                    }
                }
            }';

            echo($household);

我认为我的问题与引号引起来,该引号用于插入和输出字符串。但是我也不确定如何处理最后一个结果被删除的逗号。

3 个答案:

答案 0 :(得分:3)

您可以以这种方式连接$household变量。尽管@AliveToDie的解决方案是最好的,但是对于初学者来说,该解决方案将很容易理解

$household = '';
    $household .= '{
           "data": {
               "attributes": {
                   "name":"'.rgar( $entry, '1.6' ).'"
               },
               "relationships": {
                   "people":{
                       "data":[
                           ';

                           foreach ($family_ids[] as $key => $fam_member){
                            $household .= '{"type":"Person","id":"'.$fam_member.'"}';
                                if((count($family_ids)-1)!=$key)// if foreach loop reaches the last element then it won't print the comma
                                     $household .= ',';
                            }

        $household .='
                       ]
                   },
                   "primary_contact":{
                       "data":{"type":"Person","id":"1"}
                   }
               }
           }
       }';
 echo($household);

答案 1 :(得分:0)

您需要先将foreach值分配给变量,然后再将此变量分配给json。

$data = [];

foreach ($family_ids as $fam_member){
    $data[]= ["type"=>"Person","id"=> $fam_member]
}


$household = '{
                "data": {
                    "attributes": {
                        "name":"'.rgar( $entry, '1.6' ).'"
                    },
                    "relationships": {
                        "people":{
                            "data":'. json_encode($data) . '
                        },
                        "primary_contact":{
                            "data":{"type":"Person","id":"1"}
                        }
                    }
                }
            }';

echo($household);

一个硬编码的示例示例输出:-https://3v4l.org/enrGP

答案 2 :(得分:0)

是否有通过转义引号来构建json字符串的原因?

也许这种解决方案(我认为它将为您提供更多的自由,并且您不必担心转义引号等问题)对您会有所帮助:

$household['data']['attributes']['name']              = rgar( $entry, '1.6' );
$household['data']['relationships']['people']['data'] = array(); // In case $family_ids is empty

foreach ($family_ids[] as $fam_member):
  $household['data']['relationships']['people']['data'][] = array(
    'type' => 'Person',
    'id'   => $fam_member
  );
endforeach;

$household['data']['relationships']['primary_contact']['data'] = array(
  'type' => 'Person',
  'id'   => '1'
);

$json       = json_encode($household);
$prettyJson = json_encode($household, JSON_PRETTY_PRINT);

echo $prettyJson;

然后,您可以将$json用于json,或者,如果需要对其进行格式化且美观,可以使用$prettyJson;