str_replace:一维替换数组,二维haystack
str_replace: one-dimensional replacements array, two-dimensional haystack
我有一个这种格式的二维数组:
$oldArr = [
0 => [
"color" => "red",
"shape" => "circle",
"size" => "small",
],
1 => [
"color" => "green",
"shape" => "square",
"size" => "large",
],
2 => [
"color" => "yellow",
"shape" => "triangle",
"size" => "large",
],
];
以及这种格式的一维数组:
$newVals = [
0 => "large",
1 => "large",
2 => "small",
];
我正在尝试使用 str_replace()
遍历 $oldArr
中的每个 "size"
值,并将其替换为 $newVals
中与其位置匹配的值。由于这些数组将始终具有相同数量的顶级键值对,因此我基本上是在尝试获取 $newVals
并将其映射到每个 $oldArr["size"]
值。最终结果应该是
$newArr = [
0 => [
"color" => "red",
"shape" => "circle",
"size" => "large",
],
1 => [
"color" => "green",
"shape" => "square",
"size" => "large",
],
2 => [
"color" => "yellow",
"shape" => "triangle",
"size" => "small",
],
];
谁能推荐解决此问题的最佳方法?我尝试在 foreach 循环中使用 str_replace
,但没有成功:
foreach($oldArr as $entity):
str_replace($entity['size'], $newVals, $entity);
endforeach;
您可以使用此代码:
<?php
$oldArr = [
0 => [
"color" => "red",
"shape" => "circle",
"size" => "small",
],
1 => [
"color" => "green",
"shape" => "square",
"size" => "large",
],
2 => [
"color" => "yellow",
"shape" => "triangle",
"size" => "large",
],
];
$newVals = [
0 => "large",
1 => "large",
2 => "small",
];
$newArr = array();
foreach($oldArr as $key => $entity){
$newEntity = $entity;
$newEntity['size'] = $newVals[$key];
$newArr[$key] = $newEntity;
}
var_dump($newArr);
您可以使用 array_map()
并一次循环遍历两个数组,而不是使用原始数组的大小值,您只需使用新数组,例如
$result = array_map(function($old, $new){
return ["color" => $old["color"], "shape" => $old["shape"], "size" => $new];
}, $oldArr, $newVals);
我有一个这种格式的二维数组:
$oldArr = [
0 => [
"color" => "red",
"shape" => "circle",
"size" => "small",
],
1 => [
"color" => "green",
"shape" => "square",
"size" => "large",
],
2 => [
"color" => "yellow",
"shape" => "triangle",
"size" => "large",
],
];
以及这种格式的一维数组:
$newVals = [
0 => "large",
1 => "large",
2 => "small",
];
我正在尝试使用 str_replace()
遍历 $oldArr
中的每个 "size"
值,并将其替换为 $newVals
中与其位置匹配的值。由于这些数组将始终具有相同数量的顶级键值对,因此我基本上是在尝试获取 $newVals
并将其映射到每个 $oldArr["size"]
值。最终结果应该是
$newArr = [
0 => [
"color" => "red",
"shape" => "circle",
"size" => "large",
],
1 => [
"color" => "green",
"shape" => "square",
"size" => "large",
],
2 => [
"color" => "yellow",
"shape" => "triangle",
"size" => "small",
],
];
谁能推荐解决此问题的最佳方法?我尝试在 foreach 循环中使用 str_replace
,但没有成功:
foreach($oldArr as $entity):
str_replace($entity['size'], $newVals, $entity);
endforeach;
您可以使用此代码:
<?php
$oldArr = [
0 => [
"color" => "red",
"shape" => "circle",
"size" => "small",
],
1 => [
"color" => "green",
"shape" => "square",
"size" => "large",
],
2 => [
"color" => "yellow",
"shape" => "triangle",
"size" => "large",
],
];
$newVals = [
0 => "large",
1 => "large",
2 => "small",
];
$newArr = array();
foreach($oldArr as $key => $entity){
$newEntity = $entity;
$newEntity['size'] = $newVals[$key];
$newArr[$key] = $newEntity;
}
var_dump($newArr);
您可以使用 array_map()
并一次循环遍历两个数组,而不是使用原始数组的大小值,您只需使用新数组,例如
$result = array_map(function($old, $new){
return ["color" => $old["color"], "shape" => $old["shape"], "size" => $new];
}, $oldArr, $newVals);