显示以逗号分隔的特定值

时间:2018-09-22 08:16:55

标签: php arrays comma

我一直试图显示用逗号分隔的数组的特定值(最后一个除外)。

这是我的数组:

data class TokenState(val owner: Party, val amount: Int, val issuingHash: SecureHash?) : ContractState {
    override val participants: List<AbstractParty> = listOf()
}

interface TokenCommands : CommandData {
    class Issue : TokenCommands
    class SetIssuingHash : TokenCommands
    class Transfer : TokenCommands
}

class TokenContract : Contract {
    override fun verify(tx: LedgerTransaction) {
        val tokenCommand = tx.commandsOfType<TokenCommands>().singleOrNull() ?: throw IllegalArgumentException()
        val tokenInputs = tx.inputsOfType<TokenState>()
        val tokenOutputs = tx.outputsOfType<TokenState>()

        when (tokenCommand.value) {
            is TokenCommands.Issue -> {
                if (tokenInputs.isNotEmpty()) throw IllegalArgumentException()
                val tokenOutput = tokenOutputs.singleOrNull() ?: throw IllegalArgumentException()
                if (tokenOutput.issuingHash != null) throw IllegalArgumentException()
            }

            is TokenCommands.SetIssuingHash -> {
                val tokenInput = tokenInputs.singleOrNull() ?: throw IllegalArgumentException()
                val tokenOutput = tokenOutputs.singleOrNull() ?: throw IllegalArgumentException()
                if (tokenInput.issuingHash != null) throw IllegalArgumentException()
                if (tokenOutput.issuingHash != tx.inputs[0].ref.txhash) throw IllegalArgumentException()
                if (tokenOutput.copy(issuingHash = null) != tokenInput) throw IllegalArgumentException()
            }

            // Extend this logic to allow tokens with different `issuingHash`s to be used in the same transaction.
            is TokenCommands.Transfer -> {
                if ((tokenInputs + tokenOutputs).any { it.issuingHash == null }) throw IllegalArgumentException()
                if ((tokenInputs + tokenOutputs).map { it.issuingHash }.toSet().size != 1) throw IllegalArgumentException()
                if (tokenInputs.sumBy { it.amount } != tokenOutputs.sumBy { it.amount }) throw IllegalArgumentException()
            }
        }
    }
}

我想显示每个用逗号分隔的名称。 我尝试过的:

   "pure_taxonomies": {
       "domaine": [
                   {
                    "term_id": 36,
                    "name": "Domaine TEST",
                    "slug": "domaine-test",
                   },
                   {
                    "term_id": 40,
                    "name": "DomaineTEST2",
                    "slug": "domainetest2",
                   }
                ],

我想要的是Domaine TEST,DomaineTEST2

谢谢

2 个答案:

答案 0 :(得分:1)

您的输入对象是JSON。

  • 您首先需要使用json_decode,并将assoc标志(第二个参数)设置为 true ,将其转换为数组。这将为您提供一个关联数组。
  • 现在,您可以使用array_column函数将所有names放在一个单独的数组中。
  • 最后,使用implode函数将数组转换为逗号分隔的字符串。

尝试以下操作:

if( !is_wp_error( $response ) && $response['response']['code'] == 200 ) {
    // convert into multi-dimensional associative array
    $remote_posts = json_decode( $response['body'], true );

    foreach( $remote_posts as $remote_post ) {
        // Use array_column function to get all the names
        $names = array_column($remote_post['pure_taxonomies']['domaine'], 'name');

        // use implode to convert into comma separated string
        echo implode(', ', $names);
    }
 }

答案 1 :(得分:0)

请检查以下代码:

// ...
$domaine_names = array();
foreach( $remote_post->pure_taxonomies->domaine as $pure_taxonomies_domaine ) { 
    $domaine_names[] = $pure_taxonomies_domaine->name;
}
echo implode(', ', $domaine_names);
// ...