PHP-将json_encode中的数组预匹配为str_replace(),并将其返回给数组
我有这个阵列:PHP-将json_encode中的数组预匹配为str_replace(),并将其返回给数组,php,arrays,Php,Arrays,我有这个阵列: $form = array( array( "type" => "text", "value" => " Hello @name How old are you? @age How are you today? @condition" ), array( "type" => "font", "family" => "A
$form = array(
array(
"type" => "text",
"value" => "
Hello @name
How old are you? @age
How are you today? @condition"
),
array(
"type" => "font",
"family" => "Arial",
"size" => 6,
"weight" => "Bold"
)
);
然后我做了这个json\u encode($form)
,它有以下输出:
[
{
"type":"text",
"value":"\r\n Hello @name\r\n\r\n How old are you? @age\r\n \r\n How are you today? @condition"
},
{
"type":"font",
"family":"Arial",
"size":6,
"weight":"Bold"
}
]
问题是json\u encode()
$old = array('@name','@age',@condition');
这个$old
数据就是我将放入str_replace()中的数据代码>
但是我想用数组的形式,比如用@
符号获取所有数据
我们可以用预赛吗?或者还有其他方法吗?是的,您可以使用
我会使用这样的函数来帮助我从任何类型的输入中获取数据:
/**
* We will take two param on this function
* $input is the data we will be look into
* $tags is reference to a array, we will store out result in this array.
*/
function fetch_tags($input, &$tags){
if(is_array($input)){
// If input is array, iterate it and pass the value to fetch_tags function
foreach($input as $key => $value ){
fetch_tags($value, $tags);
}
return true;
}elseif(is_string($input)){
/**
* If its a string, we can preg_match now.
* \@\S+ means we will take any string which follows a @
* If we get any matches, we will store that result in $tags
*/
if(preg_match_all('#(\@\S+)#', $input, $matches)){
$tags = array_merge($tags, $matches[1]);
}
return true;
}
return false;
}
例如:
<?php
$form = array(
array(
"type" => "text",
"value" => "
Hello @name
How old are you? @age
How are you today? @condition"
),
array(
"type" => "font",
"family" => "Arial",
"size" => 6,
"weight" => "Bold"
)
);
$words = [];
fetch_tags($form, $words);
print_r($words);
在str_replace
中,你也可以传递数组。是的,我已经这样做了,先生,但我希望它像动态的一样,或者像其他什么东西一样获取所有的数据@
在它上面的符号。谢谢你的朋友!:)
Array
(
[0] => @name
[1] => @age
[2] => @condition
)