PHP JSON array issue - php

I have tryed almost everything to get the data in observations in a foreach sentence in php but can get it to output any data.
I Want to get the observations rows listed so i can insert it into a MySQL DB
{
"version":"2.0","secret":"meraki","type":"DevicesSeen","data":{
"apMac":"88:15:44:84:4c:90","apFloors":[],"apTags":["","recently-added",""]
,"observations":[{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":0.2907945825483442,"x":[],"y":[]},"seenTime":"2017-05-20T12:24:43Z","ssid":null,"os":"Cisco/Linksys Router","clientMac":"94:9f:3e:06:c1:6c","seenEpoch":1495283083,"rssi":47,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":"/10.226.198.65","location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":5.374873752610671,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:30Z","ssid":"DanfossConnect","os":"Slingbox","clientMac":"00:11:f6:f3:43:06","seenEpoch":1495283130,"rssi":46,"ipv6":null,"manufacturer":"Asia Pacific Microsystems"},{
"ipv4":"/172.16.0.25","location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":33.788906850703945,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:29Z","ssid":"Wireless WiFi","os":"HP Printer","clientMac":"c4:34:6b:08:dc:6d","seenEpoch":1495283129,"rssi":13,"ipv6":null,"manufacturer":"Hewlett-Packard"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":14.90854452921782,"x":[],"y":[]},"seenTime":"2017-05-20T12:24:45Z","ssid":null,"os":null,"clientMac":"54:60:09:0e:08:d2","seenEpoch":1495283085,"rssi":31,"ipv6":null,"manufacturer":"Google"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":0.287722117087862,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:54Z","ssid":null,"os":null,"clientMac":"94:9f:3e:06:c1:6d","seenEpoch":1495283154,"rssi":46,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":4.189862222398972,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:49Z","ssid":null,"os":"Cisco/Linksys Router","clientMac":"5c:aa:fd:40:29:78","seenEpoch":1495283149,"rssi":41,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":38.279358673491906,"x":[],"y":[]},"seenTime":"2017-05-20T12:23:18Z","ssid":null,"os":null,"clientMac":"f4:42:8f:c3:84:8b","seenEpoch":1495282998,"rssi":16,"ipv6":null,"manufacturer":"Samsung"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":39.49058481097055,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:28Z","ssid":null,"os":null,"clientMac":"a0:8c:fd:06:ec:17","seenEpoch":1495283128,"rssi":9,"ipv6":null,"manufacturer":"Hewlett-Packard"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":4.966494178173178,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:49Z","ssid":null,"os":null,"clientMac":"5c:aa:fd:40:29:79","seenEpoch":1495283149,"rssi":40,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46452206894476,"lng":9.39005605565066,"unc":28.907921961158962,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:22Z","ssid":null,"os":"iOS","clientMac":"b0:70:2d:84:38:cf","seenEpoch":1495283122,"rssi":13,"ipv6":null,"manufacturer":"Apple"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":40.978157267912586,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:34Z","ssid":null,"os":null,"clientMac":"60:6d:c7:85:91:17","seenEpoch":1495283134,"rssi":20,"ipv6":null,"manufacturer":"Hon Hai/Foxconn"}
]
}
}

https://jsonlint.com/ Your json code has syntax problem! one } was missing! Then go ahead with the other answers :)

<?php
$something = json_decode('{
"version":"2.0",
"secret":"meraki",
"type":"DevicesSeen",
"data":{
"apMac":"88:15:44:84:4c:90",
"apFloors":[],
"apTags":["","recently-added",""],
"observations":[{
"ipv4":null,
"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":0.2907945825483442,"x":[],"y":[]},
"seenTime":"2017-05-20T12:24:43Z",
"ssid":null,
"os":"Cisco/Linksys Router",
"clientMac":"94:9f:3e:06:c1:6c",
"seenEpoch":1495283083,
"rssi":47,
"ipv6":null,
"manufacturer":"Sonos"
},{
"ipv4":"/10.226.198.65","location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":5.374873752610671,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:30Z","ssid":"DanfossConnect","os":"Slingbox","clientMac":"00:11:f6:f3:43:06","seenEpoch":1495283130,"rssi":46,"ipv6":null,"manufacturer":"Asia Pacific Microsystems"},{
"ipv4":"/172.16.0.25","location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":33.788906850703945,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:29Z","ssid":"Wireless WiFi","os":"HP Printer","clientMac":"c4:34:6b:08:dc:6d","seenEpoch":1495283129,"rssi":13,"ipv6":null,"manufacturer":"Hewlett-Packard"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":14.90854452921782,"x":[],"y":[]},"seenTime":"2017-05-20T12:24:45Z","ssid":null,"os":null,"clientMac":"54:60:09:0e:08:d2","seenEpoch":1495283085,"rssi":31,"ipv6":null,"manufacturer":"Google"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":0.287722117087862,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:54Z","ssid":null,"os":null,"clientMac":"94:9f:3e:06:c1:6d","seenEpoch":1495283154,"rssi":46,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":4.189862222398972,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:49Z","ssid":null,"os":"Cisco/Linksys Router","clientMac":"5c:aa:fd:40:29:78","seenEpoch":1495283149,"rssi":41,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":38.279358673491906,"x":[],"y":[]},"seenTime":"2017-05-20T12:23:18Z","ssid":null,"os":null,"clientMac":"f4:42:8f:c3:84:8b","seenEpoch":1495282998,"rssi":16,"ipv6":null,"manufacturer":"Samsung"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":39.49058481097055,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:28Z","ssid":null,"os":null,"clientMac":"a0:8c:fd:06:ec:17","seenEpoch":1495283128,"rssi":9,"ipv6":null,"manufacturer":"Hewlett-Packard"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":4.966494178173178,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:49Z","ssid":null,"os":null,"clientMac":"5c:aa:fd:40:29:79","seenEpoch":1495283149,"rssi":40,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46452206894476,"lng":9.39005605565066,"unc":28.907921961158962,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:22Z","ssid":null,"os":"iOS","clientMac":"b0:70:2d:84:38:cf","seenEpoch":1495283122,"rssi":13,"ipv6":null,"manufacturer":"Apple"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":40.978157267912586,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:34Z","ssid":null,"os":null,"clientMac":"60:6d:c7:85:91:17","seenEpoch":1495283134,"rssi":20,"ipv6":null,"manufacturer":"Hon Hai/Foxconn"}]
}
}');
foreach($something->data->observations[0] as $key => $val){
if($key === "location"){
echo '<hr><h4>here is the location object</h4>';
foreach($something->data->observations[0]->$key as $item => $data){
print_r(['item'=>$item,'data'=>$data]);
}
echo '<hr>';
}
print_r($something->data->observations[0]->$key);
foreach($something->data->observations[0]->$key as $item => $data){
if($item === '')
print_r(['item'=>$item,'data'=>$data]);
}
print_r(['key'=>$key,'val'=>$val]);
echo '<br>';
}
echo '<hr>';
print_r($something->data->observations[0]);
echo '<hr>';
print_r($something);
echo '<hr>';
exit;

<?php
// you have to use json_decode for sure
$something = json_decode('{
"version":"2.0","secret":"meraki","type":"DevicesSeen","data":{
"apMac":"88:15:44:84:4c:90","apFloors":[],"apTags":["","recently-added",""]
,"observations":[{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":0.2907945825483442,"x":[],"y":[]},"seenTime":"2017-05-20T12:24:43Z","ssid":null,"os":"Cisco/Linksys Router","clientMac":"94:9f:3e:06:c1:6c","seenEpoch":1495283083,"rssi":47,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":"/10.226.198.65","location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":5.374873752610671,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:30Z","ssid":"DanfossConnect","os":"Slingbox","clientMac":"00:11:f6:f3:43:06","seenEpoch":1495283130,"rssi":46,"ipv6":null,"manufacturer":"Asia Pacific Microsystems"},{
"ipv4":"/172.16.0.25","location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":33.788906850703945,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:29Z","ssid":"Wireless WiFi","os":"HP Printer","clientMac":"c4:34:6b:08:dc:6d","seenEpoch":1495283129,"rssi":13,"ipv6":null,"manufacturer":"Hewlett-Packard"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":14.90854452921782,"x":[],"y":[]},"seenTime":"2017-05-20T12:24:45Z","ssid":null,"os":null,"clientMac":"54:60:09:0e:08:d2","seenEpoch":1495283085,"rssi":31,"ipv6":null,"manufacturer":"Google"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":0.287722117087862,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:54Z","ssid":null,"os":null,"clientMac":"94:9f:3e:06:c1:6d","seenEpoch":1495283154,"rssi":46,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":4.189862222398972,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:49Z","ssid":null,"os":"Cisco/Linksys Router","clientMac":"5c:aa:fd:40:29:78","seenEpoch":1495283149,"rssi":41,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":38.279358673491906,"x":[],"y":[]},"seenTime":"2017-05-20T12:23:18Z","ssid":null,"os":null,"clientMac":"f4:42:8f:c3:84:8b","seenEpoch":1495282998,"rssi":16,"ipv6":null,"manufacturer":"Samsung"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":39.49058481097055,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:28Z","ssid":null,"os":null,"clientMac":"a0:8c:fd:06:ec:17","seenEpoch":1495283128,"rssi":9,"ipv6":null,"manufacturer":"Hewlett-Packard"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":4.966494178173178,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:49Z","ssid":null,"os":null,"clientMac":"5c:aa:fd:40:29:79","seenEpoch":1495283149,"rssi":40,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46452206894476,"lng":9.39005605565066,"unc":28.907921961158962,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:22Z","ssid":null,"os":"iOS","clientMac":"b0:70:2d:84:38:cf","seenEpoch":1495283122,"rssi":13,"ipv6":null,"manufacturer":"Apple"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":40.978157267912586,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:34Z","ssid":null,"os":null,"clientMac":"60:6d:c7:85:91:17","seenEpoch":1495283134,"rssi":20,"ipv6":null,"manufacturer":"Hon Hai/Foxconn"}
]
}
}',true);
// first of all you miss an {
// use the second argument "true" to make it return an array
foreach ( $something['data']['observations'] as $item ){
$location_lng = $item['location']['lng'];
$location_lat = $item['location']['lat'];
echo $location_lng;
echo '<hr>';
echo $location_lat;
// Everything is good :)
}
?>

Related

PHP Array to return a URL

I am trying to return a specific iframe URL depending ont he input of a specific number of zip codes.
Example- zip code x returns url x
zip code y returns url y
I have a list of several zip codes per URL. The URL purpose is to redirect to a specific (3rd party) page based on the location input from the user.
Here is what I have so far:
<?php
$userzip = $_POST['ZipCode'];
echo $userzip;
$array = array(
'22942' => 'URL1',
'22701' => 'URL2');
// print_r($array);
foreach( $array as $key => $value ){
// echo "Output of Key=>Value pair:\r\n";
// echo $key . "->" . $value . "\r\n";
// echo "Testing $key...\r\n\r\n";
if(preg_match('/23456/',$key)){
echo "Service exists in: $value\r\n";
break;
} else {
echo "No Match for $key.\r\n\r\n";
}
}
?>
So, my first mistake is that only the zip code entered is returned for the moment. I can comment that out but left it in to show my thinking. Help?
If I have understood your request correctly then this code should work:
<?php
$userzip = $_POST['ZipCode'];
echo $userzip;
$array = array(
'22942' => 'URL1',
'22701' => 'URL2');
// print_r($array);
foreach( $array as $key => $value ){
// echo "Output of Key=>Value pair:\r\n";
// echo $key . "->" . $value . "\r\n";
// echo "Testing $key...\r\n\r\n";
if(strstr($key, $userzip)) {
echo "Service exists in: $value\r\n";
break;
} else {
echo "No Match for $key.\r\n\r\n";
}
}
?>
To actually handle the redirect then after echo "Service exists in: $value\r\n"; you can use one of these options:
header("Location: " . $value);
Or if you want to use the iFrame approach you have mentioned in the comment then:
echo '<iframe src="'.$value.'" border="0" frameborder="0" style="width:500px; height:700px;"></iframe>';
You should try to avoid loops as much as possible. You can easily check if a key/value exists in an array with isset:
$userzip = $_POST['ZipCode'];
echo $userzip;
if( isset($zipcodesA[$userzip]) ){
echo "Service exists in A: ".$array[$userzip]."\n";
} elseif( isset($zipcodesB[$userzip]) ){
echo "Service exists in B: ".$array[$userzip]."\n";
}elseif( isset($zipcodesC[$userzip]) ){
echo "Service exists in C: ".$array[$userzip]."\n";
} else {
echo "No Match for $userzip.\n\n";
}
In this case, you have no need to check every value in the array, just if one specific exists.

Wrong output in php program

Following is my code here actually o/p should be hi..but it is giving no
<?php
$arr=array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$c='xyz,ccc';
if(in_array(isset($c) && $c,$arr))
{
echo 'hi';
}
else
{
echo 'no';
}
?>
output:hi
actual result should be 'no'.
Side note, this is bad code:
in_array(isset($weekendArr) && $weekendArr,$arr)
do it like
isset($weekendArr) && in_array($weekendArr,$arr)
and in_array is not strict so this
in_array(true,array('w','s'))
will be allways TRUE
do it with:
in_array(true,array('w','s'),true)
and you see.
And you can't check an array with an array the $needle be an STRING here.
The only solution is to do splitt your STRING into two values and then check two times for TRUE
$c='Sunday,Monday';
foreach(explode(',',$c) as $check){
if(in_array($c,$arr,true))
{
echo $check.' is in array';
}
else
{
echo $check.' is NOT in array';
}
}
Hope that helps a little.
<?php
$listDays=array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$day='Sunday'; //You cant test both days ! Just one value at a time
if(true === in_array($day, $listDays))
{
echo 'hi';
}
else
{
echo 'no';
}
?>
Or option two if you want to test different days
<?php
$listDays=array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$dayToTest='Sunday, Monday'; //Here we have multiple days
$tabTest = preg_split(',', $day); //split into an array
//Then test for each string in tabTest
foreach($tabTest as $string)
{
if(true === in_array($string, $listDays))
{
echo $string.' is OK';
}
else
{
echo 'no';
}
}
?>
Change your code to:
<?php
$arr=array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$c='Sunday,Monday';
if(in_array(isset($c) && $c,$arr))
{
echo 'hi';
}
else
{
echo 'no';
}

Checking for undefined variables in a function php

So I've been trying to devise a function that will echo a session variable only if it is set, so that it wont create the 'Notice' about an undefined variable. I am aware that one could use:
if(isset($_SESSION['i'])){ echo $_SESSION['i'];}
But it starts to get a bit messy when there are loads (As you may have guessed, it's for bringing data back into a form ... For whatever reason). Some of my values are also only required to be echoed back if it equals something, echo something else which makes it even more messy:
if(isset($_SESSION['i'])){if($_SESSION['i']=='value'){ echo 'Something';}}
So to try and be lazy, and tidy things up, I have tried making these functions:
function ifsetecho($variable) {
if(!empty($variable)) {
echo $variable;
}
}
function ifseteqecho($variable,$eq,$output) {
if(isset($variable)) {
if($variable==$eq) {
echo $output;
}
}
}
Which wont work, because for it to go through the function, the variable has to be declared ...
Has anyone found a way to make something similar to this work?
maybe you can achieve this with a foreach?
foreach ($_SESSION as $variable)
{function ifseteqecho($variable,$eq,$output) {
if($variable==$eq) {
echo $output;
}
else echo $variable;
}
}
now this will all check for the same $eq, but with an array of corresponding $eq to $variables:
$equiv = array
('1'=>'foo',
'blue'=>'bar',);
you can check them all:
foreach ($_SESSION as $variable)
{function ifseteqecho($variable,$equiv) {
if(isset($equiv[$variable])) {
echo $equiv[$variable];
}
else {
echo $variable;
}
}
}
Something like this?, you could extend it to fit your precise needs...
function echoIfSet($varName, array $fromArray=null){
if(isset($fromArray)){
if(isset($fromArray[$varName])&&!empty($fromArray[$varName])){
echo $fromArray[$varName];
}
}elseif(isset($$varName)&&!empty($$varName)){
echo $$varName;
}
}
You may use variable variables:
$cat = "beautiful";
$dog = "lovely";
function ifsetecho($variable) {
global $$variable;
if(!empty($$variable)){
echo $$variable;
}
}
ifsetecho("cat");
echo "<br/>";
ifsetecho("dog");
echo "<br/>";
ifsetecho("elephant");
UPDATE: With a rather complex code I’ve managed to meet your requirements:
session_start();
$cat = "beautiful";
$dog = "lovely";
$_SESSION['person']['fname'] = "Irene";
function ifsetecho($variable){
$pattern = "/([_a-zA-Z][_a-zA-Z0-9]+)".str_repeat("(?:\\['([_a-zA-Z0-9]+)'\\])?", 6)."/";
if(preg_match($pattern, $variable, $matches)){
global ${$matches[1]};
if(empty(${$matches[1]})){
return false;
}
$plush = ${$matches[1]};
for($i = 2; $i < sizeof($matches); $i++){
if(empty($plush[$matches[$i]])){
return false;
}
$plush = $plush[$matches[$i]];
}
echo $plush;
return true;
}
return false;
}
ifsetecho("cat");
echo "<br/>";
ifsetecho("dog");
echo "<br/>";
ifsetecho("elephant");
echo "<br/>";
ifsetecho("_SESSION['person']['fname']");
echo "<br/>";
ifsetecho("_SESSION['person']['uname']");
echo "<br/>";

php continue to get rest value in a loop

I have some json data, i want re-install it after match some condition.
$mm='a';
$nn='104';
$jn=array();
$j2='[
{"a":"c","n":"103","t":"rfg"},
{"a":"a","n":"104","t":"bmf"},// <- find the data, re-install from the next line
{"a":"b","n":"105","t":"tit"},
{"a":"a","n":"106","t":"iou"},
{"a":"b","n":"107","t":"wdf"}
]';
$t2=json_decode($j2);
foreach($t2 as $d2){
if($mm==$d2->a&&$nn==$d2->n){
continue;
}
$jn['a']=$d2->a;
$jn['n']=$d2->n;
$jn['t']=$d2->t;
$p.=json_encode($jn).',';
}
echo '['.substr($p,0,-1).']';
I need return json data as [{"a":"b","n":"105","t":"tit"},{"a":"a","n":"106","t":"iou"},{"a":"b","n":"107","t":"wdf"}]
I hope you understand that.
<?php
$mm='a';
$nn='104';
$jn=array();
$j2='[
{"a":"c","n":"103","t":"rfg"},
{"a":"a","n":"104","t":"bmf"},// <- find the data, re-install from the next line
{"a":"b","n":"105","t":"tit"},
{"a":"a","n":"106","t":"iou"},
{"a":"b","n":"107","t":"wdf"}
]';
$t2=json_decode($j2);
$i=0;
foreach($t2 as $d2)
{
if($mm==$d2->a && $nn==$d2->n)
{
$jn['a']=$d2->a;
$jn['n']=$d2->n;
$jn['t']=$d2->t;
if($i==1)
{
$p=json_encode($jn).',';
}
else
{
$p.=json_encode($jn).',';
}
}
$i++;
}
echo '['.substr($p,0,-1).']';
The following code will do the job
Append the string only after finding a match.
$mm='a';
$nn='104';
$jn=array();
$j2='[{"a":"c","n":"103","t":"rfg"},{"a":"a","n":"104","t":"bmf"},{"a":"b","n":"105","t":"tit"},{"a":"a","n":"106","t":"iou"},{"a":"b","n":"107","t":"wdf"}]';
$t2=json_decode($j2);
//echo "<pre>";
//print_r($t2);
//exit();
$append = false;
$p='';
foreach($t2 as $key=>$d2){
$jn['a']=$d2->a;
$jn['n']=$d2->n;
$jn['t']=$d2->t;
if($append){
$p.=json_encode($jn).',';
}
if($mm==$d2->a&&$nn==$d2->n){
$append=true;
}
}
echo '['.substr($p,0,-1).']';

why can't array_search find the value if the code of the key is "gb2312"?

my questions:
$state=array("你"=>1);
if(array_key_exists("你",$state))
{
$result = array_search("你",$state);echo $result;
}else
{
echo "No Exists";
}
i expect the result of "1", however the output is "No Exists", i don't know why the program can't get the value of the key "你".
array_search will search the given array by value. Try the following:
$state = array("你"=>1);
if(array_key_exists("你", $state)) {
echo $state["你"];
} else {
echo "No Exists";
}
// => 1
» demo
Hope below function will help.
<?php
$array = array('arr1'=>array('find_me'=>'yes you did.'));
function get_value_by_key($array,$key)
{
foreach($array as $k=>$each)
{
if($k==$key)
{
return $each;
}
if(is_array($each))
{
if($return = get_value_by_key($each,$key))
{
return $return;
}
}
}
}
echo get_value_by_key($array,'find_me');
?>
the encoding type of the show paper and the store paper is GB2312.
$state=array("你"=>1);
if(array_key_exists("你",$state))
{
$result1 = $state["你"];
echo $result1; // can get the value 111
}else
{
echo "No Exists";
}
the code above can be executed rightly. i can't show my problems accurately. Now i paste out my code , there is some questions.
<?php
$file = file("GB2312-HanZiBianMa.txt"); // file encoding type ANSI
foreach ($file as $line_num => $line)
{
list($no,$hex,$dec) = preg_split('[\t]',htmlspecialchars($line));;
$result[$hex] = $dec;
}
$result_2 = array_flip($result);
if(array_key_exists("你",$result_2)) // **can't find the value** 222
{
$state= $result_2["你"];
echo $state."<br/>";
}else
{
echo "No Exists<br/>";
}
foreach($result_2 as $k=>$each) //can get the value using the preg_match
{
if(preg_match('/你/', $k))
echo $k."\t".$each."<br/>";
}
?>
the format of GB2312-HanZiBianMa.txt is as follows:
1947 c4e3 你
1948 c4e4 匿
1949 c4e5 腻
1950 c4e6 逆
if your want to test the code , you can save the php code and save the GB2312.. file.
the question is:
why can't the following function get the right value ? the data comes from file and one stores together.
if(array_key_exists("你",$result_2)) // **can't find the value** 222
{
$state= $result_2["你"];
echo $state."<br/>";
}else
{
echo "No Exists<br/>";
}

Categories