Home > Back-end >  How to create if statement in foreach in php
How to create if statement in foreach in php

Time:08-03

How to create if function in foreach in php

I want something like this if(currency==id) script should exclude it

Here is the php code

<?php 
$curl = curl_init(); 
 
curl_setopt_array($curl, array( 
    CURLOPT_URL => "https://api.jobians.top/coinbase/accounts.php?api=N7696pIFgkF7vagl&key=AF3RO2AIrhuLpw40kyEUnZcc975dRfQq", 
    CURLOPT_RETURNTRANSFER => true, 
    CURLOPT_ENCODING => "", 
    CURLOPT_MAXREDIRS => 2, 
    CURLOPT_TIMEOUT => 10, 
    CURLOPT_FOLLOWLOCATION => true, 
    CURLOPT_CUSTOMREQUEST => "GET" 
    )); 
$response = curl_exec($curl); 
 
curl_close($curl); 

header('Content-Type: application/json');
$json2 = json_decode($response, true);

$id_array = array_map(function($el) { return $el['id']; }, $json2['data']);
$outputObject = [];
foreach ($json2['data'] as $el) {
    $outputObject[$el['id']] = $el['balance']['currency'];
}
echo json_encode( $outputObject );
?>

CodePudding user response:

Assumptions:

  • You want to exclude all items that have the same id and currency.
  • Each item in the result should have the keys 'id' and 'currency'.

You may use the functions array_map and array_filter to implement that.

echo json_encode(
    array_map(
        function($item) {
            return [
                'id' => $item['id'],
                'currency' => $item['balance']['currency'],
            ];
        },
        array_filter(
            $json2['data'],
            function($item) {
                return $item['id'] !== $item['balance']['currency'];
            }
        ),
        []
    )
);

array_filter is used to return an array with only the items that follow the condition $item['id'] !== $item['balance']['currency'].

array_map is used to return an array with items that have the keys 'id' and 'currency'. There's a [] as the last argument because, according to the documentation:

The returned array will preserve the keys of the array argument if and only if exactly one array is passed. If more than one array is passed, the returned array will have sequential integer keys.

If you want to preserve the keys, don't use []. In that case, the output will be a JSON with numbers as strings like this:

{"1":{"id":"B","currency":"C"}}

That was the functional programming approach.

If you still want to use foreach and if, you need to declare another array to put there only the items you want:

$result = [];
foreach ($json2['data'] as $item) {
    if ($item['id'] === $item['balance']['currency']) {
        continue;
    }
    $result [] = [
        'id' => $item['id'],
        'currency' => $item['balance']['currency'],
    ];
}

echo json_encode($result);

CodePudding user response:

Basically if you want to exclude from the list some specific currency, you should do something like:

$ignoredCurrency = 'DASH';

foreach ($json2['data'] as $el) {
    if($ignoredCurrency !== $el['balance']['currency']) {
        $outputObject[$el['id']] = $el['balance']['currency'];
    }
}

Alternatively if you have an array with currencies you want to exclude, it should go like this:

$ignoredCurrencies = ['DASH', 'USDC'];

foreach ($json2['data'] as $el) {
    if(!in_array($ignoredCurrencies, $el['balance']['currency'])) {
        $outputObject[$el['id']] = $el['balance']['currency'];
    }
}
  • Related