Home > Back-end >  How to compose an equation for a php array
How to compose an equation for a php array

Time:08-25

my code to output data from a json file has two foreach loops. I will like to sort the inner loop 2 before output. The code extract:

<?php
$json_data = json_decode($json,true);

//var_dump($json_data);
foreach ($json_data['results'] as $key => $value) //loop 1
{           
        // sort the markets array by Key Name using ksort
        $abc=$value['markets'];
        ksort($abc);
        foreach ($abc as $keyy => $val) //loop 2
        {           
           echo $keyy."  ".$val;

        }
}

?>

My objective is to sort the rows of 'markets' before entering loop 2.

The Function is ksort($abc). My question is how will I get the righthand side of the equation,

$abc=array( ........ );

Thank you.

The $json_data is extracted below (top portion due to length. I want to print out the values of 'marketName' key has values such as Match Odds, Both Teams To Score, Odds/Even, Over/Under, etc. I want to output the values of 'marketName' sorted alphabetically.

$json_data

array(2) { ["success"]=> int(1) ["results"]=> array(1) { [0]=> array(5) { ["competitions"]=> array(5) { ["key"]=> string(9) "COMP:2134" ["name"]=> string(27) "English Football League Cup" ["competitionId"]=> int(2134) ["eventTypeId"]=> int(1) ["eventId"]=> int(29143752) } ["event"]=> array(9) { ["eventTypeId"]=> int(1) ["competitionId"]=> int(2134) ["eventId"]=> int(31675059) ["openDate"]=> string(24) "2022-08-24T18:45:00.000Z" ["countryCode"]=> string(2) "GB" ["videoAvailable"]=> bool(false) ["key"]=> string(14) "EVENT:31675059" ["name"]=> string(16) "Leeds v Barnsley" ["timezone"]=> string(3) "GMT" } ["updated_at"]=> string(10) "1661359151" ["markets"]=> array(132) { [0]=> array(17) { ["betDelay"]=> int(3) ["hasSGM"]=> bool(true) ["turnInPlayEnabled"]=> bool(true) ["eachwayAvailable"]=> bool(false) ["hasBPE"]=> bool(false) ["bettingType"]=> string(10) "FIXED_ODDS" ["legTypes"]=> array(1) { [0]=> string(16) "SIMPLE_SELECTION" } ["bspMarket"]=> bool(false) ["inplay"]=> bool(false) ["marketStatus"]=> string(4) "OPEN" ["linkedMarketId"]=> string(11) "1.202280279" ["rule4Deductions"]=> array(0) { } ["guaranteedPriceAvailable"]=> bool(false) ["livePriceAvailable"]=> bool(true) ["runnerDetails"]=> array(2) { [0]=> array(8) { ["runnerOdds"]=> array(3) { ["trueOdds"]=> array(2) { ["decimalOdds"]=> array(1) { ["decimalOdds"]=> int(13) } ["fractionalOdds"]=> array(2) { ["denominator"]=> int(1) ["numerator"]=> int(12) } } ["decimalDisplayOdds"]=> array(1) { ["decimalOdds"]=> int(13) } ["fractionalDisplayOdds"]=> array(2) { ["denominator"]=> int(1) ["numerator"]=> int(12) } } ["previousWinRunnerOdds"]=> array(0) { } ["handicap"]=> int(0) ["previousRunnerOdds"]=> array(0) { } ["runnerOrder"]=> int(10) ["selectionId"]=> int(2542449) ["runnerStatus"]=> string(6) "ACTIVE" ["winRunnerOdds"]=> array(3) { ["trueOdds"]=> array(2) { ["fractionalOdds"]=> array(2) { ["numerator"]=> int(12) ["denominator"]=> int(1) } ["decimalOdds"]=> array(1) { ["decimalOdds"]=> int(13) } } ["fractionalDisplayOdds"]=> array(2) { ["denominator"]=> int(1) ["numerator"]=> int(12) } ["decimalDisplayOdds"]=> array(1) { ["decimalOdds"]=> int(13) } } } [1]=> array(8) { ["previousWinRunnerOdds"]=> array(0) { } ["runnerOdds"]=> array(3) { ["trueOdds"]=> array(2) { ["fractionalOdds"]=> array(2) { ["numerator"]=> int(1) ["denominator"]=> int(100) } ["decimalOdds"]=> array(1) { ["decimalOdds"]=> float(1.01) } } ["fractionalDisplayOdds"]=> array(2) { ["denominator"]=> int(100) ["numerator"]=> int(1) } ["decimalDisplayOdds"]=> array(1) { ["decimalOdds"]=> float(1.01) } } ["handicap"]=> int(0) ["previousRunnerOdds"]=> array(0) { } ["selectionId"]=> int(2542448) ["runnerOrder"]=> int(20) ["runnerStatus"]=> string(6) "ACTIVE" ["winRunnerOdds"]=> array(3) { ["fractionalDisplayOdds"]=> array(2) { ["numerator"]=> int(1) ["denominator"]=> int(100) } ["decimalDisplayOdds"]=> array(1) { ["decimalOdds"]=> float(1.01) } ["trueOdds"]=> array(2) { ["decimalOdds"]=> array(1) { ["decimalOdds"]=> float(1.01) } ["fractionalOdds"]=> array(2) { ["denominator"]=> int(100) ["numerator"]=> int(1) } } } } } ["marketId"]=> string(13) "924.321472563" ["market"]=> array(27) { ["numberOfRunners"]=> int(2) ["marketName"]=> string(20) "Over/Under 6.5 Goals" ["eventId"]=> int(31675059) ["betDelay"]=> int(3) ["productType"]=> string(10) "SPORTSBOOK" ["marketId"]=> string(13) "924.321472563" ["sortPriority"]=> int(-3380) ["inPlay"]=> bool(false) ["associatedMarkets"]=> array(1) { [0]=> array(3) { ["eventId"]=> int(31675059) ["eventTypeId"]=> int(1) ["externalMarketId"]=> string(12) "42.335418020" } } ["topLevelEventId"]=> int(29143752) ["bettingType"]=> string(4) "ODDS" ["bspMarket"]=> bool(false) ["key"]=> string(31) "SPORTSBOOK_MARKET:924.321472563" ["upperLevelEventId"]=> int(29143752) ["marketType"]=> string(13) "OVER_UNDER_65" ["numberOfUpperLevels"]=> int(1) ["sgmMarket"]=> bool(true) ["competitionId"]=> int(2134) ["marketStatus"]=> string(4) "OPEN" ["exchangeMarketId"]=> string(11) "1.202280279" ["numberOfWinners"]=> int(1) ["numberOfActiveRunners"]=> int(2) ["marketLevels"]=> array(1) { [0]=> string(9) "AVB_EVENT" } ["eventTypeId"]=> int(1) ["canTurnInPlay"]=> bool(true) ["marketTime"]=> string(24) "2022-08-24T18:45:00.000Z" ["runners"]=> array(2) { [0]=> array(6) { ["runnerStatus"]=> string(6) "ACTIVE" ["result"]=> array(0) { } ["sortPriority"]=> int(1) ["selectionId"]=> int(2542449) ["runnerName"]=> string(14) "Over 6.5 Goals" ["handicap"]=> int(0) } [1]=> array(6) { ["runnerStatus"]=> string(6) "ACTIVE" ["result"]=> array(0) { } ["selectionId"]=> int(2542448) ["sortPriority"]=> int(2) ["runnerName"]=> string(15) "Under 6.5 Goals" ["handicap"]=> int(0) } } } } [1]=> array(16) { ["livePriceAvailable"]=> bool(true) ["guaranteedPriceAvailable"]=> bool(false) ["rule4Deductions"]=> array(0) { } ["marketId"]=> string(13) "924.321472484" ["runnerDetails"]=> array(3) { [0]=> array(8) { ["handicap"]=> int(0) ["runnerOdds"]=> array(3) { ["decimalDisplayOdds"]=> array(1) { ["decimalOdds"]=> int(6) } ["fractionalDisplayOdds"]=> array(2) { ["numerator"]=> int(5) ["denominator"]=> int(1) } ["trueOdds"]=> array(2) { ["fractionalOdds"]=> array(2) { ["numerator"]=> int(5) ["denominator"]=> int(1) } ["decimalOdds"]=> array(1) { ["decimalOdds"]=> int(6) } } } ["previousWinRunnerOdds"]=> array(0) { } ["winRunnerOdds"]=> array(3) { ["trueOdds"]=> array(2) { ["fractionalOdds"]=> array(2) { ["numerator"]=> int(5) ["denominator"]=> int(1) } ["decimalOdds"]=> array(1) { ["decimalOdds"]=> int(6) } } ["decimalDisplayOdds"]=> array(1) { ["decimalOdds"]=> int(6) } ["fractionalDisplayOdds"]=> array(2) { ["numerator"]=> int(5) ["denominator"]=> int(1) } } ["runnerStatus"]=> string(6) "ACTIVE" ["previousRunnerOdds"]=> array(0) { } ["selectionId"]=> int(17037) ["runnerOrder"]=> int(10) } [1]=> array(8) { ["previousRunnerOdds"]=> array(0) { } ["selectionId"]=> int(12453989) ["runnerOrder"]=> int(20) ["winRunnerOdds"]=> array(3) { ["fractionalDisplayOdds"]=> array(2) { ["numerator"]=> int(11) ["denominator"]=> int(10) } ["decimalDisplayOdds"]=> array(1) { ["decimalOdds"]=> float(2.1) } ["trueOdds"]=> array(2) { ["fractionalOdds"]=> array(2) { ["denominator"]=> int(10) ["numerator"]=> int(11) } ["decimalOdds"]=> array(1) { ["decimalOdds"]=> float(2.1) } } } ["runnerStatus"]=> string(6) "ACTIVE" ["runnerOdds"]=> array(3) { ["trueOdds"]=> array(2) { ["fractionalOdds"]=> array(2) { ["denominator"]=> int(10) ["numerator"]=> int(11) } ["decimalOdds"]=> array(1) { ["decimalOdds"]=> float(2.1) } } ["fractionalDisplayOdds"]=> array(2) { ["denominator"]=> int(10) ["numerator"]=> int(11) } ["decimalDisplayOdds"]=> array(1) { ["decimalOdds"]=> float(2.1) } } ["previousWinRunnerOdds"]=> array(0) { } ["handicap"]=> int(0) } [2]=> array(8) { ["previousRunnerOdds"]=> array(0) { } ["runnerOrder"]=> int(30) ["selectionId"]=> int(1313787) ["runnerStatus"]=> string(6) "ACTIVE" ["winRunnerOdds"]=> array(3) { ["fractionalDisplayOdds"]=> array(2) { ["numerator"]=> int(11) ["denominator"]=> int(10) } ["decimalDisplayOdds"]=> array(1) { ["decimalOdds"]=> float(2.1) } ["trueOdds"]=> array(2) { ["decimalOdds"]=> array(1) { ["decimalOdds"]=> float(2.1) } ["fractionalOdds"]=> array(2) { ["denominator"]=> int(10) ["numerator"]=> int(11) } } } ["previousWinRunnerOdds"]=> array(0) { } ["runnerOdds"]=> array(3) { ["fractionalDisplayOdds"]=> array(2) { ["numerator"]=> int(11) ["denominator"]=> int(10) } ["decimalDisplayOdds"]=> array(1) { ["decimalOdds"]=> float(2.1) } ["trueOdds"]=> array(2) { ["decimalOdds"]=> array(1) { ["decimalOdds"]=> float(2.1) } ["fractionalOdds"]=> array(2) { ["numerator"]=> int(11) ["denominator"]=> int(10) } } } ["handicap"]=> int(0) } } ["eachwayAvailable"]=> bool(false) ["turnInPlayEnabled"]=> bool(true) ["hasSGM"]=> bool(false) ["betDelay"]=> int(3) ["marketStatus"]=> string(4) "OPEN" ["inplay"]=> bool(false) ["bspMarket"]=> bool(false) ["bettingType"]=> string(10) "FIXED_ODDS" ["legTypes"]=> array(1) { [0]=> string(16) "SIMPLE_SELECTION" } ["hasBPE"]=> bool(false) ["market"]=> array(26) { ["marketType"]=> string(17) "EXACT_TOTAL_GOALS" ["associatedMarkets"]=> array(1) { [0]=> array(3) { ["externalMarketId"]=> string(12) "42.335417901" ["eventId"]=> int(31675059) ["eventTypeId"]=> int(1) } } ["bettingType"]=> string(4) "ODDS" ["topLevelEventId"]=> int(29143752) ["upperLevelEventId"]=> int(29143752) ["key"]=> string(31) "SPORTSBOOK_MARKET:924.321472484" ["bspMarket"]=> bool(false) ["marketId"]=> string(13) "924.321472484" ["sortPriority"]=> int(-640) ["inPlay"]=> bool(false) ["numberOfRunners"]=> int(3) ["eventId"]=> int(31675059) ["marketName"]=> string(17) "Exact Total Goals" ["productType"]=> string(10) "SPORTSBOOK" ["betDelay"]=> int(3) ["marketLevels"]=> array(1) { [0]=> string(9) "AVB_EVENT" } ["canTurnInPlay"]=> bool(true) ["eventTypeId"]=> int(1) ["marketTime"]=> string(24) "2022-08-24T18:45:00.000Z" ["runners"]=> array(3) { [0]=> array(6) { ["sortPriority"]=> int(1) ["selectionId"]=> int(17037) ["runnerName"]=> string(6) "0 or 1" ["result"]=> array(0) { } ["runnerStatus"]=> string(6) "ACTIVE" ["handicap"]=> int(0) } [1]=> array(6) { ["handicap"]=> int(0) ["runnerStatus"]=> string(6) "ACTIVE" ["runnerName"]=> string(14) "Exactly 2 or 3" ["sortPriority"]=> int(2) ["selectionId"]=> int(12453989) ["result"]=> array(0) { } } [2]=> array(6) { ["sortPriority"]=> int(3) ["runnerName"]=> string(9) "4 Or More" ["selectionId"]=> int(1313787) ["result"]=> array(0) { } ["runnerStatus"]=> string(6) "ACTIVE" ["handicap"]=> int(0) } } ["marketStatus"]=> string(4) "OPEN" ["numberOfWinners"]=> int(1) ["numberOfActiveRunners"]=> int(3) ["competitionId"]=> int(2134) ["sgmMarket"]=> bool(false) ["numberOfUpperLevels"]=> int(1) } } [2]=> array(17) { ["bettingType"]=> string(10) "FIXED_ODDS" ["hasBPE"]=> bool(false) ["legTypes"]=> array(1) { [0]=> string(16) "SIMPLE_SELECTION" } ["bspMarket"]=> bool(false) ["inplay"]=> bool(false) ["marketStatus"]=> string(4) "OPEN" ["betDelay"]=> int(3) ["hasSGM"]=> bool(true) ["turnInPlayEnabled"]=> bool(true) ["eachwayAvailable"]=> bool(false) ["runnerDetails"]=> array(2) { [0]=> array(8) { ["runnerOdds"]=> array(3) { ["fractionalDisplayOdds"]=> array(2) { ["denominator"]=> int(20) ["numerator"]=> int(19) } ["decimalDisplayOdds"]=> array(1) { ["decimalOdds"]=> float(1.95) } ["trueOdds"]=> array(2) { ["fractionalOdds"]=> array(2) { ["numerator"]=> int(19) ["denominator"]=> int(20) } ["decimalOdds"]=> array(1) { ["decimalOdds"]=> float(1.95) } } } ["previousWinRunnerOdds"]=> array(0) { } ["handicap"]=> int(0) ["runnerOrder"]=> int(10) ["selectionId"]=> int(30246) ["previousRunnerOdds"]=> array(0) { } ["runnerStatus"]=> string(6) "ACTIVE" ["winRunnerOdds"]=> array(3) { ["trueOdds"]=> array(2) { ["decimalOdds"]=> array(1) { ["decimalOdds"]=> float(1.95) } ["fractionalOdds"]=> array(2) { ["denominator"]=> int(20) ["numerator"]=> int(19) } } ["decimalDisplayOdds"]=> array(1) { ["decimalOdds"]=> float(1.95) } ["fractionalDisplayOdds"]=> array(2) { ["denominator"]=> int(20) ["numerator"]=> int(19) } } } [1]=> array(8) { ["previ

CodePudding user response:

ksort is sorting the higher level of the array' but you want to sort by lower level ($abc[*]['market']['marketName']), so you have to implement custom sort and use usort as below instead of ksort:

// sort the markets array by Key Name using ksort
$abc=$value['markets'];
usort($abc, function($el1, $el2){ 
     if ($el1['market']['marketName'] > $el2['market']['marketName']){
         return 1;
     }
     if ($el1['market']['marketName'] < $el2['market']['marketName']){
         return -1;
     }
     return 0;
});
foreach ($abc as $keyy => $val) //loop 2
{           
   echo $keyy."  ".$val;
}
  • Related