How can i identify position of MAX value in array? [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to get 112 position [01] and use this position[01] to compare something ? Can i do that ? Sorry about stupid question. I'm just a new guy really want to known. Thank for any help.
<?
$sql "SELECT * FROM truck WHERE t_ID = '001'";
$arry = odbc_fetch_array($DataExec);
echo "MAX value position's".$arry;
?>
echo $arry. This 1 i want to show [ MAX value position's 01.]. Or someone have better way.

<?php
$arr_numeric= array('41', '112', '25','54');
$max_position = array_search(max($arr_numeric), $arr_numeric);
echo $max_position;
?>
position start from 0.
OUTPUT
1

$arr = array('41', '112', '25','54');
$index = array_search(max($arr),$arr);
echo "MAX value position's".$index;
Similar Question here

I get it with a google search.
$maxs = array_keys($array, max($array))
Note:
this way you can retrieve every key related to a given max value.
If you are interested only in one key among all simply use $maxs[0]

Related

Echo JSON -> Arrays in PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I've been trying to read a JSON string in PHP since yesterday, but I always fail. Can you help me?
{"WEAPON_PISTOL":{"ammo":227}}
TO
ECHO: WEAPON_PISTOL WITH 227 Ammunition
I've tried this way up until now:
$json_string = "{"WEAPON_PISTOL":{"ammo":227}}";
$obj = json_decode($json_string,true);
for($i=0;$i<count($obj);$i++) {
echo $obj[$i];
echo $obj[$i]["ammo];
}
The way to access the value of "ammo" is:
$json_string = '{"WEAPON_PISTOL":{"ammo":227}}';
$obj = json_decode($json_string);
echo $obj->WEAPON_PISTOL->ammo;
This returns:
227
If you want to loop through several weapons, even though your example only contains one weapon, you can do:
$json_string ='{"WEAPON_PISTOL":{"ammo":227},
"WEAPON_GUN":{"ammo":6}}';
$weapons = json_decode($json_string);
foreach ($weapons as $weaponName => $weapon) {
echo "$weaponName has $weapon->ammo rounds.\n";
}
This returns:
WEAPON_PISTOL has 227 rounds.
WEAPON_GUN has 6 rounds.

I cannot decode or extract any information from this string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How to decode the following string so I can extract readable data from it:
"H4sIAAAAAAAAAEWRz3LaMBDG…HXHYhZxEsNvDIH3B4ACAAA="
The only thing that I know about this string is that it may be random generated bytes, that's all I know, unfortunately.
I tried base64_decode() but that didn't work it just gave me this string:
?E??r?0?q??D??? }???
echo base64_decode("H4sIAAAAAAAAAEWRz3LaMBDG…HXHYhZxEsNvDIH3B4ACAAA=");
This string should give me really useful information for the API project I'm working on, also if there is an alternative way to do this using javascript please let me know, Thanks.
I have searched for H4sIAAA and found gzdecode() which decoded this string, thank you for your help Pavan Kumar.
$string = "H4sIAAAAAAAAABWPQU+DQBCFH7RVICaeTDyZ7cGrB5PGM4XVNhLiofXaTGGkG2Fp2MXIL+J/8MOM27m9b95M3ouAEJ6KAHg+fFV6bx4WSdtr60WYWapCzFkXJ1xmhnCjSn6tqTJO/kW4LpU51zQ4V9Z2HDi6wMM0vmx10TEZNsKeWJgzcynaLzG0fYel2zdKq1aL4yCmkZ5Xjw49ib2uVaMsl7hxOu07ss60dE+jaVxNYy0/tkmAeU4N494h6ZKRdgciox8S6774ZosIt/LXdhRb26ljb9kEl2q4k3myifOdTA9Z/Bkf1vvkXe4AH1cpNVTxpeM/Fr19kRIBAAA=";
echo gzdecode(base64_decode($string))
//Result i idGCount tag ench HideFlags� display Lore §7Increases the speed of your!§7minion by §a25%§7. Unlimited§7Duration! §5§lEPICName§5Enchanted Lava Bucket ExtraAttributesidENCHANTED_LAVA_BUCKETDamage
You can get quite close using the following:
function inspect( $s ){
$b=rawurldecode( base64_decode( trim( $s ) ) );
$g=gzdecode( $b );
return sprintf('<pre>%s</pre>',print_r($g,1));
}
$i=0;
$max=10;
$url='https://api.hypixel.net/skyblock/auctions?key=09828659-42c5-4360-9203-d93bcb5df79d';
$data=file_get_contents( $url );
$json=json_decode( $data );
foreach( $json->auctions as $obj ){
echo inspect( $obj->item_bytes );
if( $i >= $max )break;
$i++;
}
sample output:
i
idGCount
tag ench HideFlags�
display Lore§7Increases the speed of your!§7minion by §a25%§7. Unlimited§7Duration!
§5§lEPICName§5Enchanted Lava Bucket
ExtraAttributesidENCHANTED_LAVA_BUCKETDamage

unable to a decode_json in php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have A Json data But I am unable to get desired response.
$content='{"success":true,"results":[{"id":"21390fb46e92","msisdncountrycode":"DE","msisdn":"+491788735000","statuscode":"HLRSTATUS_UNDELIVERED","hlrerrorcodeid":9,"subscriberstatus":"SUBSCRIBERSTATUS_ABSENT","imsi":null,"mccmnc":"26203","mcc":"262","mnc":"03","msin":null,"servingmsc":null,"servinghlr":null,"originalnetworkname":"E-Plus","originalcountryname":"Germany","originalcountrycode":"DE","originalcountryprefix":"+49","originalnetworkprefix":"178","roamingnetworkname":null,"roamingcountryname":null,"roamingcountrycode":null,"roamingcountryprefix":null,"roamingnetworkprefix":null,"portednetworkname":null,"portedcountryname":null,"portedcountrycode":null,"portedcountryprefix":null,"portednetworkprefix":null,"isvalid":"Yes","isroaming":"No","isported":"No","usercharge":"0.0100","inserttime":"2015-11-24 18:56:42.048693+08","storage":"CURL-TEST","route":"IP1"}]}';
i want to capture some contents like
id , msisdncountrycode , msisdncountrycode , statuscode , hlrerrorcodeid
And Other Values As Well Please Help To Solve This problem .
check out this:
$result = json_decode($content);
var_dump($result->results[0]->id);//will get id
var_dump($result->results[0]->msisdncountrycode);//will get Country code
use this(works with multiple results).
$d = json_decode($content);
foreach($d->results as $row){
echo $row->id; //echoes 21390fb46e92
echo $row->msisdncountrycode; //echoes DE
echo $row->statuscode; //echoes HLRSTATUS_UNDELIVERED
echo $row->hlrerrorcodeid; //echoes 9
}
Echoes are based on current input. Since its in loop, it will print all values.
You can try this. Its working fine for me.
json_decode($content)
Try this:
<?php
$content='{"success":true,"results":[{"id":"21390fb46e92","msisdncountrycode":"DE","msisdn":"+491788735000","statuscode":"HLRSTATUS_UNDELIVERED","hlrerrorcodeid":9,"subscriberstatus":"SUBSCRIBERSTATUS_ABSENT","imsi":null,"mccmnc":"26203","mcc":"262","mnc":"03","msin":null,"servingmsc":null,"servinghlr":null,"originalnetworkname":"E-Plus","originalcountryname":"Germany","originalcountrycode":"DE","originalcountryprefix":"+49","originalnetworkprefix":"178","roamingnetworkname":null,"roamingcountryname":null,"roamingcountrycode":null,"roamingcountryprefix":null,"roamingnetworkprefix":null,"portednetworkname":null,"portedcountryname":null,"portedcountrycode":null,"portedcountryprefix":null,"portednetworkprefix":null,"isvalid":"Yes","isroaming":"No","isported":"No","usercharge":"0.0100","inserttime":"2015-11-24 18:56:42.048693+08","storage":"CURL-TEST","route":"IP1"}]}';
echo '<pre>';
$jsonArr = json_decode($content, TRUE);
$outputArr = $jsonArr['results'][0];
print_r($outputArr);
$id = $outputArr['id'];
$msisdncountrycode = $outputArr['msisdncountrycode'];
$statuscode = $outputArr['statuscode'];
$hlrerrorcodeid = $outputArr['hlrerrorcodeid'];
?>

How to do maximum array intersection PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I ask a candidate : What do you like ?
His answer is an array [Math,Physics,Chemistry,IT,Sports]
I do have a list :
Software Analyst : [Math,IT,Programing,Stress,Computer]
Truck drive : [Sports,Driving,Traveling,Exchange]
Physic-chemistry teacher :[Math,Physics,Chemistry,Teaching]
What is the best way in PHP to get the jobs with the maximum common tags with the user input?
You use array_intersect() to find the common tags, then compare among the returned tags amount.
$jobs = array(
'software' => array('Math','IT','Programing','Stress','Computer')
'truck' => array('Sports','Driving','Traveling','Exchange')
'physic' => array('Math','Physics','Chemistry','Teaching')
);
$user_tags= array('Math','Physics','Chemistry','IT','Sports');
$max_common = 0;
$desired_job = '';
foreach($jobs as $key=>$job) {
$common = count(array_intersect($job,$user_tags);
if(max_common < $common) {
$desired_job = $key;
$max_common = $common;
}
}

condition statement doesn't work [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
i'm trying to do a simple condition check like:
$test = 1;
$test_ = $test==-1?"Test--1":$test==1?"Test-1":$test==0?"Test-0":"Test";
echo $test_;
The output is Test-0 but must be Test-1.
Which could be the problem?
Try this : Put brackets for conditions.
$test = 1;
$test_ = ($test==-1)?"Test--1":($test==1?"Test-1":($test==0?"Test-0":"Test"));
echo $test_;
output :
Test-1
change this
$test_ = $test==-1?"Test--1":$test==1?"Test-1":$test==0?"Test-0":"Test";
to
$test_ = $test==-1?"Test--1":($test==1?"Test-1":($test==0?"Test-0":"Test"));
working example http://viper-7.com/msuCyk
// output Test-1
Ternary operator in PHP is left to right, therefore, its executed like this:
$test_ = (( ( $test==-1 ? "Test--1" :$test==1) ? "Test-1":$test==0)?"Test-0":"Test");

Categories