Home > OS >  How to get data for specific condtion in php curl
How to get data for specific condtion in php curl

Time:08-29

'url here' which contains a data key and the value is a string which contains items in the format: key=STRING, age=INTEGER. My goal is to count how many items exist that have an age equal to or greater than 50, and print this final value.

$ch = curl_init('url here');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  $data = curl_exec($ch);
  curl_close($ch);

  print_r(json_decode($data, true)); 

$data getting data as follows:

Array
(
    [data] =>   key=IAfpK, age=58, 
                key=WNVdi, age=64, 
                key=jp9zt, age=47, 
                key=0Sr4C, age=68, 
                key=CGEqo, age=76, 
                key=IxKVQ, age=79, key=eD221, age=29, key=XZbHV, age=32, key=k1SN5, age=88, key=4SCsU, age=65, key=q3kG6, age=33, key=MGQpf, age=13, key=Kj6xW, age=14, key=tg2VM, age=30, key=WSnCU, age=24, key=f1Vvz, age=46, key=dOS7A, age=72, key=tDojg, age=82, key=nZyJA, age=48, key=R8JTk, age=29, key=005Ot, age=66, key=HHROm, age=12, key=5yzG8, age=51, key=xMJ5D, age=38, key=TXtVu, age=82, key=Hz38B, age=84, key=WfObU, age=27, key=mmqYB, age=14, key=4Z3Ay, age=62, key=x3B0i, age=55, key=QCiQB, age=72, key=zGtmR, age=66, key=nlIN9, age=8, key=hKalB, age=50, key=Na33O, age=17, key=jMeXm, age=15, key=OO2Mc, age=32, key=hhowx, age=32 )

Not getting how to match value and get count.

Example Input
{"data":"key=IAfpK, age=58, key=WNVdi, age=64, key=jp9zt, age=47"}

Example Output
2

If anyone have idea then please let me know

CodePudding user response:

This is rather messy and a REGEX person may be able to clean this up a bit, but it seems to work

function fixTheNonsenseJson($nonsense)
{
    $a = json_decode($nonsense);

    $s = str_replace( ['key=', ', age=', ', "key'], ['"key":"', '","age":"', '", "key'], $a->data);
    $bits = explode(',', $s);
    $usable = [];
    foreach ($bits as $bit) {
        $j = json_decode('{' . $bit. '}');
        if ( isset($j->key) ) {
            $t = new stdClass();
            $t->key = $j->key;
        }
        if ( isset($j->age) ) {
            $t->age = $j->age;
            $usable[] = $t;
            $t = null;
        }
    }
    return $usable;
}


$badJson = '{"data" : "key=IAfpK, age=58, key=WNVdi, age=64, key=jp9zt, age=47, key=0Sr4C, age=68, key=CGEqo, age=76, key=IxKVQ, age=79, key=eD221, age=29, key=XZbHV, age=32, key=k1SN5, age=88, key=4SCsU, age=65, key=q3kG6, age=33, key=MGQpf, age=13, key=Kj6xW, age=14, key=tg2VM, age=30, key=WSnCU, age=24, key=f1Vvz, age=46, key=dOS7A, age=72, key=tDojg, age=82, key=nZyJA, age=48, key=R8JTk, age=29, key=005Ot, age=66, key=HHROm, age=12, key=5yzG8, age=51, key=xMJ5D, age=38, key=TXtVu, age=82, key=Hz38B, age=84, key=WfObU, age=27, key=mmqYB, age=14, key=4Z3Ay, age=62, key=x3B0i, age=55, key=QCiQB, age=72, key=zGtmR, age=66, key=nlIN9, age=8, key=hKalB, age=50, key=Na33O, age=17, key=jMeXm, age=15, key=OO2Mc, age=32, key=hhowx, age=32"}';

$usable = fixTheNonsenseJson($badJson);

// the actual code to do the count
$count = 0;
foreach( $usable as $u){
    if ( $u->age > 50 ) {
        $count  ;
    }
}
echo "Count > 50 = $count";

CodePudding user response:

I originally missed the over 50 part.

If your goal is to just count, and your data is as uniform as you say, you can still just search on age= followed by one or more digits using RegEx, then count the over 50 items:

$data  = 'key=IAfpK, age=58, key=WNVdi, age=64, key=jp9zt, age=47';
$itemsOverFifty = [];
if(preg_match_all('/age=(?<age>\d )/', $data, $matches)) {
    $itemsOverFifty = array_filter($matches['age'], fn($item) => $item >= 50 );
}

echo count($itemsOverFifty);

Demo: https://3v4l.org/8eqnR

  • Related