2016-11-23 9 views
0

なぜこの関数が私が定義した配列に何かをプッシュしていないのを理解しようとしています。私はprint_r $ location_matchesを空にします。要素を配列にプッシュする関数を作成するにはどうすればよいですか?

あなたは道ので、変数を変更したい場合は、参照によって通過を確認する必要があり
$id = get_the_ID(); 
$location_matches = array(); 

function find_location_meta($location_id, $product_id, $location_matches_arr) { 
    $meta_info = get_post_meta($location_id); 
    $working_with = unserialize($meta_info[locations_products_carried][0]); 
    for ($i = 0; $i < count($working_with); $i++) { 
     if ($working_with[$i][locations_products][0] == $product_id) { 
      array_push($location_matches_arr, $working_with[$i]); 
     } 
    } 
} 

find_location_meta(94, $id, $location_matches); 
+0

配列を返すか、参照渡しする必要があります。 –

+0

$ working_with = unserialize($ meta_info [locations_products_carried] [0]); - locations_products_carriedは変数のいずれかです。次に$記号が必要です。そうであれば文字列で、次に引用符が必要です。 $ meta_infoや$ working_withを使って "print_r"を試してみてください - あなたは一歩一歩進むことをお勧めします。 iterateを実行する前に$ working_withに要素があるかどうかを確認してください。それにはlocations_productsに問題があります(変数か文字列です)。そして、正しい$ location_idが確実に94ですか? –

答えて

2

$id = get_the_ID(); 
$location_matches = array(); 

function find_location_meta($location_id, $product_id, &$location_matches_arr) { 
    $meta_info = get_post_meta($location_id); 
    $working_with = unserialize($meta_info[locations_products_carried][0]); 
    for ($i = 0; $i < count($working_with); $i++) { 
     if ($working_with[$i][locations_products][0] == $product_id) { 
      array_push($location_matches_arr, $working_with[$i]); 
     } 
    } 
} 

find_location_meta(94, $id, $location_matches); 

あなたはそれができたので、私は、関数の宣言で&を追加したことに気づくでしょうその正確な変数を指し、その内容を変更します。

+0

完璧に働いて、ありがとう! – user3006927

関連する問題