一個php變數封裝進數組的有關問題

來源:互聯網
上載者:User
一個php變數封裝進數組的問題
遇到一個關於json的問題。有一組正則規則,由於有很多行(以後可能還需要增刪)
所以在首頁頁面裡用一個固定的foreach代碼,然後將正則規則封裝一個json數組,儲存到外部檔案(以後修改規則時只需修改這個外部檔案)。

json.txt代碼
$json=<<[{"a":"!preg_match([email protected]\\\.aaa\\\.com@',$url,$match)||!preg_match([email protected]\\\.bbb\\\.com@',$url,$match)||!preg_match([email protected]\\\.ccc\\\.com@',$url,$match)","b":"1"},{"a":"!preg_match([email protected]\\\.ddd\\\.com@',$url,$match)||!preg_match([email protected]\\\.eee\\\.com@',$url,$match)","b":"2"},{"a":"!preg_match([email protected]\\\.fff\\\.com@',$url,$match)||!preg_match([email protected]\\\.ggg\\\.com@',$url,$match)","b":"3"}]
EOT;

php頁面代碼

include("json.txt");
$date = json_decode($json);
foreach($date as $row){
if($row->a){
//做一些$row->b的事物
}
}


但是現在$url,$match2個php變數在json_decode後消失了,試加花括弧{$url},{$match2}也沒用。
現在如何解決這個問題?不行的話不用json也可以。最終目前是封裝成一個數值,可以用foreach迴圈讀出。謝謝。 json php


------解決方案--------------------
$json=<<<'EOT'
....
EOT;
就不會使變數“消失”
但我不是道你對 '$var' 如何還原成變數
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.