I want to be able to extract the output of [HostName] from this array in PHP. Thank you!
Array
(
[Protocol] => 17
[HostName] => [HG] JAILBREAK #1 - HeLLsGamers.com | 30+ LR's | FastDL
[Map] => ba_jail_hellsgamers_fx6
[ModDir] => cstrike
[ModDesc] => Counter-Strike: Source
[AppID] => 240
[Players] => 12
[MaxPlayers] => 64
[Bots] => 0
[Dedicated] => d
[Os] => l
[Password] =>
[Secure] => 1
[Version] => 1.0.0.75
[GamePort] => 27015
[ServerID] => -682722125
[GameTags] => HLstatsX:CE,alltalk,bunnyhopping,cool,drugs,free,gameme,gang,hellsgamers,hg,increased_maxplayers,jail,jailbr,nostats,startmoney
)
Like this:
echo $Array[HostName]; // output: [HG] JAILBREAK #1 - HeLLsGamers.com | 30+ LR's | FastDL
where $Array is the array you printed and copied here.
I recommend you start reading stuff from php.net - as this is the most basic thing you can do.
Thats the most simplest question .........
Just use this
echo $Your_array[HostName];
Related
I have an array of php like this
Array
(
[0] => Array
(
[Sr_No] => 1
[Asan_Category] => Warm-Up
[Asan_Cat_Val] => 8
[Asan_Sub_Category] => Ankle
[Asan_Sub_Cat_Val] => 35
[Asan_Name] => General Ankle Warm up
[Asan_Name_Val] => 447
[Prescribed_Steps] => 40
[Prescribed_Ratio] => 00
[Actual_Steps] => 12
[Actual_Ratio] => 0
)
[1] => Array
(
[Sr_No] => 2
[Asan_Category] => Warm-Up
[Asan_Cat_Val] => 8
[Asan_Sub_Category] => Knee
[Asan_Sub_Cat_Val] => 111
[Asan_Name] => General knee warm up
[Asan_Name_Val] => 464
[Prescribed_Steps] => 20
[Prescribed_Ratio] => 00
[Actual_Steps] => 14
[Actual_Ratio] => 0
)
[2] => Array
(
[Sr_No] => 1
[Asan_Category] => Warm-Up
[Asan_Cat_Val] => 8
[Asan_Sub_Category] => Ankle
[Asan_Sub_Cat_Val] => 35
[Asan_Name] => General Ankle Warm up
[Asan_Name_Val] => 447
[Prescribed_Steps] => 40
[Prescribed_Ratio] => 00
[Actual_Steps] => 10
[Actual_Ratio] => 0
)
[3] => Array
(
[Sr_No] => 2
[Asan_Category] => Warm-Up
[Asan_Cat_Val] => 8
[Asan_Sub_Category] => Knee
[Asan_Sub_Cat_Val] => 111
[Asan_Name] => General knee warm up
[Asan_Name_Val] => 464
[Prescribed_Steps] => 20
[Prescribed_Ratio] => 00
[Actual_Steps] => 9
[Actual_Ratio] => 0
)
)
The desired output I want
Array
(
[0] => Array
(
[Asan_Id] => 447
[Asan_Category] => Warm-Up
[Asan_Sub_Category] => Ankle
[Asan_Name] => General Ankle Warm up
[Prescribed_Steps] => 40
[Prescribed_Ratio] => 00
[Total_Steps] => 22
)
[1] => Array
(
[Asan_Id] => 464
[Asan_Category] => Warm-Up
[Asan_Sub_Category] => Knee
[Asan_Name] => General knee warm up
[Prescribed_Steps] => 20
[Prescribed_Ratio] => 00
[Total_Steps] => 23
)
)
I want those data who are repeating become one but their different actual steps become total steps with their sum.
Please help me because I have tried some code but did not success like this
$asan=[];
$total_steps=0;
foreach ($aasan_details as $key) {
$total_steps += $key['Actual_Steps'];
if(!in_array($key['Asan_Name_Val'],$asan))
{
$asan[] = $key['Asan_Name_Val'];
$lookup[] = array("Asan_Id"=>$key['Asan_Name_Val'],
"Asan_Category"=>$key['Asan_Category'],
"Asan_Sub_Category"=>$key['Asan_Sub_Category'],
"Asan_Name"=>$key['Asan_Name'],
"Prescribed_Steps"=>$key['Prescribed_Steps'],
"Prescribed_Ratio"=>$key['Prescribed_Ratio'],
'Total_Steps'=>$total_steps);
}
}
It is not working , what should I do guys , please help me out
where $aasan_details is the array which I showed above and the in lookup array I am getting uniqe values but not getting their total
The best solution would be to move this into a database and use a query to perform that exact operation for you.
But if you have to do it in code, you could use a keyed array to basically group them up by however many fields need to match for them to be conjoined:
e.g.
foreach ..
$consolidated[$item['Asan_Cat_Val']][$item['Asan_Sub_Cat_Val']][] = $item;
Then make a couple of nested loops to go across this and sum everything up.
There a few useful techniques to explain here...
You can reduce eye-strain in your looped array declarations by establishing a collection of keys that you know you want to retain in (copy to) the output array. This spares you having to type out each $keyName => $row[$keyName], like this (but ultimately make yourself happy).
Declare temporary associative keys (using $id) while building your output array to allow isset() to make super fast checks for a pre-existing group.
If a group is not yet set, then store the data with the new keys along with the data with the original keys. No arithmetic is necessary. The + symbols in my snippet are not "addition operators", they are "array union operators" -- they are merging the three arrays without a function call (otherwise array_merge() would do the same).
When finished iterating the array, you may choose to re-index the result (remove the temporary associative keys) by calling array_values(). If you are not bothered by the existence of these temporary keys, you can omit the function call.
If a group has been encountered before, you only need to modify the Total_steps. Use an addition-assignment operator (+=) for the briefest syntax.
Code: (Demo)
$keysToKeep = array_flip(['Asan_Category', 'Asan_Sub_Category', 'Asan_Name', 'Prescribed_Steps', 'Prescribed_Ratio']);
foreach ($array as $row) {
$id = $row['Asan_Name_Val'];
if (!isset($result[$id])) {
$result[$id] = ['Asan_Id' => $id] + array_intersect_key($row, $keysToKeep) + ['Total_Steps' => $row['Actual_Steps']];
} else {
$result[$id]['Total_Steps'] += $row['Actual_Steps'];
}
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'Asan_Id' => '447',
'Asan_Category' => 'Warm-Up',
'Asan_Sub_Category' => 'Ankle',
'Asan_Name' => 'General Ankle Warm up',
'Prescribed_Steps' => '40',
'Prescribed_Ratio' => '00',
'Total_Steps' => 22,
),
1 =>
array (
'Asan_Id' => '464',
'Asan_Category' => 'Warm-Up',
'Asan_Sub_Category' => 'Knee',
'Asan_Name' => 'General knee warm up',
'Prescribed_Steps' => '20',
'Prescribed_Ratio' => '00',
'Total_Steps' => 23,
),
)
I have this array built from a function that sorts allowed or valid url slugs and their ids into another array. I can call the current category id by passing the url slug to the category. Now I need to get the children category ids (if any) so i can call the appropriate items from the database for display purpose.
Heres an example of the array:
Array (
[radios] => 1
[radios/motorola] => 2
[radios/motorola/handheld] => 3
[radios/motorola/mobile] => 4
[radios/icom] => 5
[radios/icom/handheld] => 6
[radios/icom/mobile] => 7
[radios/mics-and-speakers] => 8
[radios/mounts] => 9
[radios/radio-other] => 10
[misc] => 11
[misc/led] => 12
[phones] => 13
[phones/samsung] => 14
[phones/lg] => 15
[phones/motorola] => 16
[phones/huawei] => 17
[phones/blackberry] => 18
[phones/flip] => 19
[boosters] => 20
[boosters/standalone] => 21
[boosters/indoor-antenna] => 22
[boosters/outdoor-antenna] => 23
[boosters/connections] => 24
[accessories] => 25
[accessories/cases] => 26
[accessories/other] => 27
[internet] => 28
[internet/fusion] => 29
[internet/point-to-point] => 30
[internet/hotspots] => 31
[internet/gateways] => 32
[internet/switches] => 33
[cameras] => 34
[cameras/complete-kits] => 35
[cameras/additional-cameras] => 36
[cameras/other] => 37
);
As you can see, each result points to the category ID of that group. If i visit the following url:
http://example.com/store/cameras
I can print out that THE PATH CURRENTLY IS: 34 which is correct. Since It has children under it, I also need their ID's and the ID's of any of their children, etc etc. That way on radios, i can show ALL of the sub category items, and on radios/motorola i am only showing the Motorola based items and its children, and such down the line.
If there an easy way, using this array I have now, to sort the children (if any) all the way down and get back just their id's (preferably in a new array) for showing database items?
You might want to create a function like this to filter your array,
function filterArray($array, $term) {
$pattern = "/\b" . str_replace($term, '/', '\/') . "\b/i";
foreach($array as $key => $value) {
if(preg_match($pattern, $key)) {
/* Following condition makes sure that your search
will match starting from the beginning. */
if (stripos(trim($key), $term) === 0){
$filtred[] = $value;
}
}
}
}
Then call the above function with the $array and your search $term.
filterArray($array, 'radios') will give you this,
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 )
filterArray($array, 'radios/motorola') will give you this,
Array ( [0] => 2 [1] => 3 [2] => 4 )
And so on.. I hope this helps.
You can maybe try double layer array, so that you can store values like this,
Array (
[name] => John
[surname] => Smith
[contact] => Array (
[address] => 18 Maple Street
[number] => 555 477 77 77
)
)
This way if you need to print something from "contact" you can use a loop and print all childs of contact field.
I am using PHP WhatsApp Library. As per instructions I got my login and password. Upon using I am getting error:
] Logging in as 'SmartConnect Agent' (12334) tx <stream:features></stream:features>
tx <auth user="12334" mechanism="WAUTH-2"></auth>
rx <from s.whatsapp.net=""></from>
rx <stream:features></stream:features>
rx <challenge>fafa1216bdf3c4c4528e10d80b29c6026493682d</challenge>
tx <response>19d8120136e5897f38fe0904e54611cd8510f029bdd97f077f460faba15bc68cd0644e87ebf4b8288a143dc1914cb61bf4bfa2f17e0d7187afa8fcd18e69bb2b75d4f2da549c3d3c3facedd808bc</response>
rx <failure>
rx <not-authorized></not-authorized>
rx </failure>
Upon running blockchecker.php I get. Even same thing when I am retying to register by using registerTool.php
Wrong identity.
Array
(
[cc] => 92
[in] => 1234566
[lg] => en
[lc] => PK
[id] => >zv?+?????w!l?v?5b?
[mistyped] => 6
[network_radio_type] => 1
[simnum] => 1
[s] =>
[copiedrc] => 1
[hasinrc] => 1
[rcmatch] => 1
[pid] => 579
[extexist] => 1
[extstate] => 1
)
stdClass Object
(
[status] => fail
[reason] => incorrect
[sms_length] => 6
[voice_length] => 6
[sms_wait] => 0
[voice_wait] => 0
)
It generated multiple IDentity file of a same number. I removed all and it generated new password.
While parsing a CSV file(I have used a Class found on Google), I ran into a problem. Here is an example of an array made from a .csv file(print_r):
Array
(
[0] => Array
(
[Site] => ViralNova
[Impressions] => 104719
[CTR] => 0.30%
[Clicks] => 311
[Average CPC] => $ 0.400
[CPM] => $ 1.19
[Conversion Rate] => 1.29%
[Actions] => 4
[CPA] => $ 31.100
[Spent] => $ 124.40
)
[1] => Array
(
[Site] => TMZ - Desktop
[Impressions] => 103276
[CTR] => 0.29%
[Clicks] => 295
[Average CPC] => $ 0.400
[CPM] => $ 1.14
[Conversion Rate] => 0.68%
[Actions] => 2
[CPA] => $ 59.000
[Spent] => $ 118.00
)
)
The Problem is that I cannot use the "Site" index. Whenever I try to, I get this notice:
<b>Notice</b>: Undefined index: Site in <b>C:\xampp\htdocs\panel\update\assets\php\core\core-ajax.php</b> on line <b>104</b><br />
Line 104 being:
print $data[0]['Site'];
Also,
print $data[0]['Impressions'];
or
print_r($data[0]);
works without any problem and I get the correct value/array.
Any ideas?
Thanks!!
Acc. to me your code is running fine.
this problem is occur when in loop in a point your index is missing.
to resolve this issue..
use this..
<?php
if($data[0]['Site']){
print $data[0]['Site'];
}
// Also
if($data[0]['Impressions']){
print print $data[0]['Impressions'];
}
?>
I have a multidimensional array like this:
Array (
[0] => Array ( [ans_id] => 1 [ans_name] => PHP Hypertext Preprocessor [ques_id] => 1 [right_ans] => Yes [ques_name] => What is the acronym of PHP? [section_id] => 1 [section_name] => PHP )
[1] => Array ( [ans_id] => 2 [ans_name] => Preety Home Page [ques_id] => 1 [right_ans] => No [ques_name] => What is the acronym of PHP? [section_id] => 1 [section_name] => PHP )
[2] => Array ( [ans_id] => 3 [ans_name] => Programmed Hypertext Page [ques_id] => 1 [right_ans] => No [ques_name] => What is the acronym of PHP? [section_id] => 1 [section_name] => PHP )
[3] => Array ( [ans_id] => 4 [ans_name] => Programmed Hypertext Preprocessor [ques_id] => 1 [right_ans] => No [ques_name] => What is the acronym of PHP? [section_id] => 1 [section_name] => PHP ) )
My table is like this:
ans_id ans_name ques_id right_ans
1 PHP Hypertext Preprocessor 1 Yes
2 Preety Home Page 1 No
3 Programmed Hypertext Page 1 No
4 Programmed Hypertext Preprocessor 1 No
5 Andy Suraski 12 No
6 Zeev Gutman 12 No
7 Rasmus Lerdorf 12 Yes
8 Perl 12 No
I want to retrieve the answers in sets of common question id's
i.e. one set of all answers for Ques_id='1', 2nd set of all answers for ques_id='12', etc.
I am supposed to use this code:
foreach($all_ans_cat as $r)
{
echo $r['ans_id']." ".$r['ans_name']."<br>";
}
This is retrieving all the values present in the table as one set and again in the 2nd set the same values are being displayed.
Select all answers sorted by ques_id and generate array with structure needed
$questions = array();
$prevQuestionId = null;
foreach ($all_ans_cat as $r) {
if ($r['ques_id'] !== $prevQuestionId) {
$questions[] = array();
}
$questions[sizeof($questions)-1][] = $r;
$prevQuestionId = $r['ques_id'];
}
Result array would be:
0:
- [ans1, ques1]
- [ans2, ques1]
...
1:
- [ans5, ques13]
- [ans6, ques13]
...
...
you can use a query with group by and aggregate function
have a look at
http://www.w3schools.com/sql/sql_groupby.asp