2017-09-08 5 views
0

このセクションのPHPコードは、APIからの応答を受け取り、応答の特定の部分が変数と一致するかどうかを確認します。変数が配列の一部である場合の戻り値

以下のように...配列attributionには、$this_idに一致するsource_idがありますか?

$this_id = 15; 
$array = json_decode($response, true); 

if (in_array($this_id, array_column($array['data']['attribution'], 'source_id'))) { 
     // then do this 
} 
else { 
     // then do this instead 
} 

これは完全に機能します。私がやろうとしているのは、APIをもう一度呼び出す必要なく、この配列から追加データを得ることです。ロジックは上記と同じですが、trueの場合は['data']['attribution']['item_number']の値が返されます。 attributionにはたくさんのオブジェクトが存在する可能性があることに注意してください。

この値を返すにはどうすればよいですか?

+0

あなたはAPIへの呼び出しで '' [ 'ITEM_NUMBER']を渡すつもりですか? – Rahi

答えて

2

変更array_searchからin_array

$this_id = 15; 
$array = json_decode($response, true); 
$ind = array_search($this_id,array_column($array['data']['attribution'], 'source_id')); 

if ($ind !== false) { 
    $found = $array['data']['attribution'][$ind]; 
    //then do this and also use $found 
}else{ 
    //then do this instead 
} 
+0

ありがとう!これは私が必要としていたものです! – jonmrich

関連する問題