更改数组/对象嵌套组合数组中的值时出现问题
I don't know what to do to get this done what would like to do. I tried multiple approaches, e.g. I used array_map
, array_walk
, nested foreach
loops with get_object_vars
and I worked with json_decode/encode
and so on. I always come a little bit further but never reach my goal and I would like to get some guidance from you
Basically when you see the array below, how would you proceed when you want to change some value in the path
array for multiple values in the array itself?
My questions:
1) Is it right that I must convert both nested objects to an array first or is this not nesessary to do this? I mean I always get some type conversion error which tells me that I either have everything as an object or array. Is this right?
2) If this mistery is solved, which php array function is the appropriate one to change values in an array(/object)? As I have written above, I tried so many and I don't see the trees in the woods anymore. Which one do you suggest to me to use in a foreach loop?
Array
(
[0] => stdClass Object
(
[doc] => stdClass Object
(
[path] => Array
(
[0] => Bob
[1] => pictures
[2] => food
)
)
)
[1] => stdClass Object
(
[doc] => stdClass Object
(
[path] => Array
(
[0] => Alice
[1] => pictures
[2] => vacations
[3] => rome
)
)
)
)
我不知道该怎么做才能完成这项工作。 我尝试了多种方法,例如 我使用 基本上当你看到下面的数组时,如果你想改变,你将如何进行? 我的问题: strong> p>
1)我是否必须先将两个嵌套对象转换为数组,或者是 这不是必要的吗? 我的意思是我总是得到一些类型转换错误,它告诉我,我要么将所有内容都作为对象或数组。 这是对吗? p>
2)如果这个问题得到解决,哪个php数组函数适合更改数组(/ object)中的值? 正如我上面所写,我尝试了很多,我再也看不到树林里的树木了。 您建议我在foreach循环中使用哪一个? p>
array_map code>,
array_walk code>,使用
get_object_vars code>嵌套
foreach code>循环,并使用
json_decode / encode 等等。 我总是来得更远,但从未实现我的目标,我希望得到你的一些指导 p>
path code>数组中的一些值是否为数组本身中的多个值? p>
Array
(
[0] => stdClass Object
(
[doc] => stdClass对象
(
[路径] =>数组
(
[0] => Bob
[1] => pictures
[2] => food
)
)
)
[1] => stdClass对象
(
[doc] => stdClass对象
(
[路径] =>数组
(
[0] => Alice
[1] => pictures
[2] => vacations
[3] => rome
)
)
)
)
code> pre>
div>
I would suggest that,
- you create an array with keys as new path and value as old path ( path to be replaced).
- Loop you path array and check if it is available in above defined array.
- If available replace it with key of above defined array.
For example
// array defined as point 1
$change_path_array= array('pics'=>'pictures','meal'=>'food');
// $array is your array.
foreach ($array as $value) {
// loop you path array
for($i=0;$i<count($value->doc->path);$i++){
// check if the value is in defined array
if(in_array($value->doc->path[$i],$change_path_array)){
// get the key and replace it.
$value->doc->path[$i] = array_search($value->doc->path[$i], $change_path_array);
}
}
}
Out Put: picture is replaced with pics and food with meal
Array
(
[0] => stdClass Object
(
[doc] => stdClass Object
(
[path] => Array
(
[0] => Bob
[1] => pics
[2] => meal
)
)
)
[1] => stdClass Object
(
[doc] => stdClass Object
(
[path] => Array
(
[0] => Alice
[1] => pics
[2] => vacations
[3] => rome
)
)
)
)
You can modify the code to check casesensitive.
You can do it like this:
for($i = 0; $i < count($arr); $i++){
$path_array = $arr[$i]->doc->path;
// do your modifications for [i]th path element
// in your case replace all 'Bob's with 'Joe's
$path_array = array_map(function($paths){
if($paths == 'Bob') return 'Joe';
return $paths;
}, $paths_array);
$arr[$i]->doc->path = $path_array;
}
Example of changing all pictures
to photos
:
$doc1 = new \stdClass;
$doc1->doc = new \stdClass;
$doc1->doc->path = array('Bob', 'pictures', 'food');
$doc2 = new \stdClass;
$doc2->doc = new \stdClass;
$doc2->doc->path = array('Alice', 'pictures', 'vacations', 'rome');
$documents = array($doc1, $doc2);
/* change all 'pictures' to 'photos' */
foreach ($documents as &$doc) {
foreach ($doc->doc->path as &$element) {
if ($element == 'pictures') {
$element = 'photos';
}
unset($element);
}
unset($doc);
}
print_r($documents);