PHP – File to Associative Array with 1 key and two values attached

Try this // file path $file=”orderdata.txt”; // REMEMBER TO MENTION THE CORRECT FILE WITH EXTENSION // open the file and get the resource handle with errors suppressed $handle = @fopen($file,’r’); // DONT USE @ while at development since it will suppress errors // array to hold our values $params = array(); if($handle) { // if … Read more

count how many duplicate keys are in array of objects?

You can use array_reduce $arr=”[{“playerId”:3207,”playerName”:”RyanGarbutt”,”playerPos”:”C”,”playerApiId”:”5079″},{“playerId”:3238,”playerName”:”Max Domi”,”playerPos”:”C”,”playerApiId”:”5412″},{“playerId”:3240,”playerName”:”AnthonyDuclair”,”playerPos”:”LW”,”playerApiId”:”5441″}]”; $arr = json_decode( $arr, true ); $result = array_reduce( $arr , function( $c, $v ) { isset( $c[ $v[ “playerPos” ] ] ) ? $c[ $v[ “playerPos” ] ]++ : $c[ $v[ “playerPos” ] ] = 1; return $c; }, array() ); echo “<pre>”; print_r( $result ); echo “</pre>”; … Read more