搜索解码的json结果,如果找到则为字符串和echo

时间:2015-05-22 08:58:59

标签: php json

我目前正在尝试搜索一些json结果(SKU和ID),如果找到我输入的字符串(SKU),我希望它显示产品ID。

我是新手,所以也许我完全走错了路。

这是我到目前为止所做的:

<?php

// Establish connection with TG
$context = stream_context_create(array(
    'http' => array(
        'header'  => "Authorization: Bearer 872ed5e72dfc7e4e0adaa7c663cab7d81415078fdbe4f486d085b718c93b3eb3")
    )
);

// API URL for TG
$url = "http://api.tradegecko.com/variants";

// Get the JSON information from TG
$data = file_get_contents($url, false, $context);

// Decode that motherfucker
$json = json_decode($data);
// Now for each item do something
foreach ($json->variants as $row) {
    $getsku = $row->sku;
    $getid = $row->id;  
    $check_sku = $getsku." - ".$getid;
    // show decoded results
    //echo $check_sku."<br>";

}               

// Check to see if the SKU is there
$checking = array($check_sku);
// Search results for the string
$string = 'gini-gear';
// if the SKU matches the string, echo the product ID
foreach ($checking as $product_info) {
    if (strpos($string, $product_info) !== FALSE) {
        echo "The SKU was found, the ID is ".$getid; 
        return true;
    }
}
echo "The SKU was not found, try again";
return false;

?>

解码的json结果如下。所以我正在寻找它以获取&#39; 6337393&#39;。但我得到的只是&#34;找不到SKU,再试一次&#34;。有人可以帮忙吗?

编辑:下面解码结果的完整列表。最初粘贴了一些以节省空间但添加了完整列表以帮助理解var_dump。

VK7i-S-SHX7 - 6852445
ELE-FLWDSLR - 6852388
AO-ATOMSUN001 - 6838367
AO-ATOMCAS003 - 6838366
665/s - 6837409
shr-01-no-ff - 6830668
ELE-OFLY - 6797386
Birdy-case - 6775341
C-G-10 - 6773506
AM-NX1 - 6762963
Foam-asc02 - 6761234
sr-head-parts - 6760813
cap-5000 - 6759821
EL15P-01-XLR - 6745144
E8P-01-XLR - 6745142
E6P-01-XLR - 6745141
K5-80-KCHK - 6743911
V800-BH35 - 6722062
5d-ii/O/P-LP6e - 6721964
LM-102 - 6634658
HD-STND - 6634656
PRSO-5 - 6631822
77mm-ring - 6627149
sc-ms100 - 6627113
5000HD-plate - 6588253
Birdy+battery - 6588248
508-adaptor - 6567665
S1500-BH35 - 6531584
FW-50P - 6526057
c100-cable - 6516369
M9-LINECAM-46 - 6516352
SN2.2-WF - 6462820
7800-ST-CASE-BTY - 6443813
7800-battery - 6443812
P-18-JS - 6443811
gini-gear - 6337393
iFocus-LG - 6337392
Moza-monitor - 6337390
moza-monitor - 6337389
MOZA - 6337388
664/O/P-DU21 - 6325053
664/O/P - 6325052
RL-230-Chrgr - 6288577
FX3P-GPRO - 6288545
OCU-SSPiPAD/PROMO - 6283738
DDMOUNT-SUCTION - 6258027
VL5-KIT-BP5-S - 6257553
D-FOCUS-515 - 6257422
D-FOCUS-004 - 6230777
comfort-arm - 6216440
puing-magic-ifocus - 6214498
FCS633 - 6214488
5000-gimbal - 6201327
mb-600-flag - 6201113
HHG-01-Spare-Battery - 6174301
fc-02-parts - 6164962
VX17E-FK-USV - 6117524
IB500-S - 6117378
FH-slider - 6100252
FLY-X3-PLUS - 6100117
HHG-Clamp - 6100066
HHG-Battery - 6100033
flag-mb02 - 6070469
VB-150-chrg - 6054413
fc-02 - 6045825
vista-arm - 6027891
FLY-X3 - 6027874
A-GH4-PRO - 6027724
P-SLNG - 6015459
ZOOMH5 - 6015453
LPE6-PLATE - 6009082
F970-PLATE - 6008981
TILT-JIB T3 - 6003487
7800 - 5997221
OCU-SSPDSLR/PROMO - 5996720
MTF-BODY - 5975179
bmcc-01 - 5975163
SKU338 - 5975157
3ft-slide - 5974334
ELE-CNFLY-BMCC - 5893285
S900-BH35 - 5893217
DVC18029-BP-AB - 5893117
V1000-35BH - 5854640
9-cage-arms - 5771016
DR-MB600 - 5752097
77RING-MB600 - 5752096
FH-MB600 - 5752028
EVF4RVW - 5751693
SL-V 1200-EX - 5734649
blimp-clip - 5724769
other - 5724767
E6-coupler - 5724766
TP-5000 - 5724736
BOX01 - 5724654
WL3200 - 5724653
slide-eng-legs - 5715588
Arm-connector - 5715494
S1200-BH35 - 5715346
VK7i-E6-SHX7 - 5693352
SHX7 - 5693349

编辑:感谢Barmar!我已将其更改为以下(我认为是正确的)但它仍然找不到它。

<?php

// Establish connection with TG
$context = stream_context_create(array(
    'http' => array(
        'header'  => "Authorization: Bearer 872ed5e72dfc7e4e0adaa7c663cab7d81415078fdbe4f486d085b718c93b3eb3")
    )
);

// API URL for TG
$url = "http://api.tradegecko.com/variants";

// Get the JSON information from TG
$data = file_get_contents($url, false, $context);

// Decode that motherfucker
$json = json_decode($data);
$checking = array();
foreach ($json->variants as $row) {
    $getsku = $row->sku;
    $getid = $row->id;  
    $check_sku = $getsku." - ".$getid;
    // show decoded results
    //echo $check_sku."<br>";
    $checking[] = $check_sku;


$string = "gini-gear";
// if the SKU matches the string, echo the product ID
//foreach ($check_sku as $product_info) {
    if (strpos($string, $check_sku) !== FALSE) {
        echo "The SKU was found, the ID is ".$getid; 
        return true;
    }
//}
echo "The SKU was not found, try again";
return false;


// End of foreach $json->variants
}
?>

编辑2 :仍然获得相同的回报 - &#34;找不到SKU,再试一次&#34;

<?php

// Establish connection with TG
$context = stream_context_create(array(
    'http' => array(
        'header'  => "Authorization: Bearer 872ed5e72dfc7e4e0adaa7c663cab7d81415078fdbe4f486d085b718c93b3eb3")
    )
);

// API URL for TG
$url = "http://api.tradegecko.com/variants";

// Get the JSON information from TG
$data = file_get_contents($url, false, $context);

// Decode that motherfucker
$json = json_decode($data);
$checking = array();
foreach ($json->variants as $row) {
    $getsku = $row->sku;
    $getid = $row->id;  
    $check_sku = $getsku." - ".$getid;
    // show decoded results
    //echo $check_sku."<br>";
    $checking[] = $check_sku;

$string = "gini-gear";
foreach ($checking as $id => $product_info) {
    if (strpos($string, $product_info) !== FALSE) {
        echo "The SKU was found, the ID is ".$id; 
        return true;
    }

}
echo "The SKU was not found, try again";
return false;


// End of foreach $json->variants
}
?>

编辑3: var_dump提供以下内容。由于某种原因,有很多重复,这就是为什么它如此长(不能发布它太长),但看到它here

编辑4:我已经更改了strpos,但仍然遇到了同样的问题。以下是更正的strpos安排。

$string = "gini-gear";
foreach ($checking as $id => $product_info) {
    if (strpos($product_info, $string) !== FALSE) {
        echo "The SKU was found, the ID is ".$id; 
        return true;
    }

}

1 个答案:

答案 0 :(得分:0)

您需要在$checking循环中添加foreach数组。如果你想知道哪个ID匹配,你应该把它作为一个关联数组。

$checking = array();
foreach ($json->variants as $row) {
    $getsku = $row->sku;
    $getid = $row->id;  
    $check_sku = $getsku." - ".$getid;
    // show decoded results
    //echo $check_sku."<br>";
    $checking[$getid] = $check_sku;

}

如果你在循环后这样做。 $check_sku只有最后一行的值,而不是每一行的所有值。

然后您的搜索循环应如下所示:

foreach ($checking as $id => $product_info) {
    if (strpos($product_info, $string) !== FALSE) {
        echo "The SKU was found, the ID is ".$id; 
        return true;
    }
}

这是整个脚本,我已经测试过它并且有效:

<?php

// Establish connection with TG
$context = stream_context_create(array(
    'http' => array(
        'header'  => "Authorization: Bearer 872ed5e72dfc7e4e0adaa7c663cab7d81415078fdbe4f486d085b718c93b3eb3")
    )
);

// API URL for TG
$url = "http://api.tradegecko.com/variants";

// Get the JSON information from TG
$data = file_get_contents($url, false, $context);

// Decode it
$json = json_decode($data);
$checking = array();
foreach ($json->variants as $row) {
    $getsku = $row->sku;
    $getid = $row->id;  
    $check_sku = $getsku." - ".$getid;
    // show decoded results
    //echo $check_sku."<br>";
    $checking[$getid] = $check_sku;

}


$string = "gini-gear";
foreach ($checking as $id => $product_info) {
    if (strpos($product_info, $string) !== FALSE) {
        echo "The SKU was found, the ID is ".$id; 
        return true;
    }
}
echo "The SKU was not found, try again";
return false;