-1

'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

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Praff
  • 41
  • 5
  • Hold on a minute, this array you show `[data] => key=IAfpK, age=58, key=WNVdi, age=64, ......` Is not a valid array – RiggsFolly Aug 28 '22 at 14:18
  • @RiggsFolly Yes i know, ot getting how to resolved this one – Praff Aug 28 '22 at 14:19
  • Did you also write the code (API) that is returning this data? – RiggsFolly Aug 28 '22 at 14:21
  • @RiggsFolly No, I am not returning this data. I need to just show result – Praff Aug 28 '22 at 14:23
  • Can you check whats in `$data` please using a `var_dump($data);` – RiggsFolly Aug 28 '22 at 14:24
  • @RiggsFolly . its given in string. ........... – Praff Aug 28 '22 at 14:27
  • @RiggsFolly................data like............ string(5671) "{"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, – Praff Aug 28 '22 at 14:28
  • If your data is uniform like that, can you just count how many times `age=` is found in it? https://3v4l.org/3qdQE. It feels hacky, but the data being returned from the API in unstructured format is suspect to begin with. – Chris Haas Aug 28 '22 at 14:39
  • If at all possible could you ask the site that produces this data to do it sensibly? Like send the data as usable JSON – RiggsFolly Aug 28 '22 at 15:01
  • @RiggsFolly its not possible. I need to manage in this only – Praff Aug 28 '22 at 15:47
  • @ChrisHaas its only applicable for = to condition. If i need to age greater than 50 then how it will be manage? – Praff Aug 28 '22 at 15:55

2 Answers2

1

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

Chris Haas
  • 53,986
  • 12
  • 141
  • 274
0

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";
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149