array-key

PHP: php and .html file separation

霸气de小男生 提交于 2020-01-07 08:07:37
问题 I'm currently working on separating HTML & PHP code here's my code which is currently working for me. code.php <?php $data['#text#'] = 'A'; $html = file_get_contents('test.html'); echo $html = str_replace(array_keys($data),array_values($data),$html); ?> test.html <html> <head> <title>TEST HTML</title> </head> <body> <h1>#text#</h1> </body> </html> OUTPUT: A it search and change the #text# value to array_value A it works for me. Now i'm working on a code to search "id" tags on html file. If it

utf8_encode does not produce right result

我是研究僧i 提交于 2020-01-03 04:34:49
问题 My problem is the following: I store an array, which has keys like "e", "f", etc. At some point, I have to get the value of the key. This works well. But if I want to store "í", "é", etc. as the keys, it won't produce the right result (results in �). My page has to be in UTF-8. Looking up the problem, I found out that utf8_encode should help my problem. It didn't: although it produced a more-readable character, it still totally differed from what I want. If important, phpinfo gives: Directive

How to merge two arrays by taking over only values from the second array that has the same keys as the first one?

狂风中的少年 提交于 2019-12-29 07:35:12
问题 I'd like to merge two arrays with each other: $filtered = array(1 => 'a', 3 => 'c'); $changed = array(2 => 'b*', 3 => 'c*'); Whereas the merge should include all elements of $filtered and all those elements of $changed that have a corresponding key in $filtered : $merged = array(1 => 'a', 3 => 'c*'); array_merge($filtered, $changed) would add the additional keys of $changed into $filtered as well. So it does not really fit. I know that I can use $keys = array_intersect_key($filtered, $changed

How to remove the unwanted nested keys from JSON

陌路散爱 提交于 2019-12-25 15:44:23
问题 This is my json: { "all_counts_reports":{ "26":{ "name":"kumar", "date":"2017-04-27", "trips_per_day":"2", "cash_trips":"0", "credit_trips":"1", "compliment_trips":"1" }, "28":{ "name":"kumar", "date":"2017-04-29", "trips_per_day":"1", "cash_trips":"1", "credit_trips":"0", "compliment_trips":"0" } } } I want to remove the second level keys (e.g "26:" and "28": ) using PHP. In other words, I want to replace the double-quoted number keys with zero-indexed numeric keys. How can I make it look

How to remove the unwanted nested keys from JSON

强颜欢笑 提交于 2019-12-25 15:42:57
问题 This is my json: { "all_counts_reports":{ "26":{ "name":"kumar", "date":"2017-04-27", "trips_per_day":"2", "cash_trips":"0", "credit_trips":"1", "compliment_trips":"1" }, "28":{ "name":"kumar", "date":"2017-04-29", "trips_per_day":"1", "cash_trips":"1", "credit_trips":"0", "compliment_trips":"0" } } } I want to remove the second level keys (e.g "26:" and "28": ) using PHP. In other words, I want to replace the double-quoted number keys with zero-indexed numeric keys. How can I make it look

PHP Replace multidimensional array keys

只谈情不闲聊 提交于 2019-12-25 11:27:07
问题 I would like to replace some keys, my array is: Array ( [0] => Array ( [0] => test1 [1] => test2 [2] => test3 [3] => test4 [4] => test5 [5] => test6 ) [1] => Array ( [0] => test7 [1] => test8 [2] => test9 [3] => test10 [4] => test11 [5] => test12 ) ) My wish: Array ( [45] => Array ( [0] => test1 [1] => test2 [2] => test3 [3] => test4 [4] => test5 [5] => test6 ) [51] => Array ( [0] => test7 [1] => test8 [2] => test9 [3] => test10 [4] => test11 [5] => test12 ) ) 45 and 51 are examples. How can

PHP Replace multidimensional array keys

て烟熏妆下的殇ゞ 提交于 2019-12-25 11:26:07
问题 I would like to replace some keys, my array is: Array ( [0] => Array ( [0] => test1 [1] => test2 [2] => test3 [3] => test4 [4] => test5 [5] => test6 ) [1] => Array ( [0] => test7 [1] => test8 [2] => test9 [3] => test10 [4] => test11 [5] => test12 ) ) My wish: Array ( [45] => Array ( [0] => test1 [1] => test2 [2] => test3 [3] => test4 [4] => test5 [5] => test6 ) [51] => Array ( [0] => test7 [1] => test8 [2] => test9 [3] => test10 [4] => test11 [5] => test12 ) ) 45 and 51 are examples. How can

How to wrap around in PHP array when index falls off the end?

有些话、适合烂在心里 提交于 2019-12-22 10:09:24
问题 I want to be able to retrieve the value of an array by using the numeric key. The catch is that if the key is beyond the array length, I need it to loop through the array again. $my_array = array('zero','one','two','three','four','five','six','seven'); function loopArrayValues($array,$key){ //this is what is needed to return return } echo "Key 2 is ".loopArrayValues($my_array,2)."<br />"; echo "Key 11 is ".loopArrayValues($my_array,11)."<br />"; echo "Key 150 is ".loopArrayValues($my_array,11

Why cant I access $_POST variable with a hyphen/dash in its key if passing key as variable?

萝らか妹 提交于 2019-12-20 04:56:26
问题 I've written a small static method for my class which returns either $_POST variable if it is set or NULL otherwise. Input elements in HTML form have names with hyphens for example 'customer-name'. So I think I could access them like this $var = $_POST['customer-name'] . But with my method: public static function getPost($param) { echo $param." = ".$_POST[$param]."<br/>"; return isset($_POST[$param]) ? $_POST[$param] : NULL; } I can not. And I notice some strange behavior when added some echo

replace array keys with given respective keys

家住魔仙堡 提交于 2019-12-18 13:09:41
问题 I have an array like below $old = array( 'a' => 'blah', 'b' => 'key', 'c' => 'amazing', 'd' => array( 0 => 'want to replace', 1 => 'yes I want to' ) ); I have another array having keys to replace with key information. $keyReplaceInfoz = array('a' => 'newA', 'b' => 'newB', 'c' => 'newC', 'd' => 'newD'); I need to replace all keys of array $old with respective values in array $keyReplaceInfo . Output should be like this $old = array( 'newA' => 'blah', 'newB' => 'key', 'newC' => 'amazing', 'newD