我正在从我的数据库中返回给我的数组中构建一个新数组。我的原始数组由许多对象组成,并且几个对象可以像这样对象:
object(stdClass)[26]
public 'id' => string '6' (length=1)
public 'title' => string 'Something' (length=41)
public 'excerpt' => string '<p>Something</p>' (length=104)
public 'content' => string '<p>Lorem Ipsum</p>' (length=18)
public 'location' => string '' (length=0)
public 'author' => string '' (length=0)
public 'date' => string '2015-09-28' (length=10)
public 'publish' => string '1' (length=1)
public 'top' => string '0' (length=1)
public 'post_id' => string '0' (length=1)
public 'news_image_path' => string '14545819551_gallery.jpg' (length=23)
public 'news_main_image' => string '0' (length=1)
这个对象的唯一区别在于&#34; news_image_path&#34;财产和&#34; news_main_image&#34;属于&#34; 0&#34;,&#34; 1&#34;或NULL。根据那个&#34; news_main_image&#34;我想合并那些相同的对象,我希望只有'news_main_image' => string '1'
的那个对象在这个过程中幸存下来,如果没有&#39; news_image_path&#39;应为NULL或者如果有'news_main_image' => string '0'
属性&#39; news_image_path&#39;也应该为null。
简而言之:
如果'news_main_image' => string '1'
则'news_image_path' => string '14545819576_main.jpg'
如果'news_main_image' => string '0'
则'news_image_path' => NULL
如果'news_main_image' => NULL
则'news_image_path' => NULL
但其他所有财产应保持不变!
我的代码到目前为止,但它覆盖了自己,如果在前一次迭代中设置了news_image_path
,则会在其上复制NULL:
foreach ($result as $key => $value)
{
if ($value->news_main_image == '1')
{
$tmp[$value->id] = $value;
$tmp[$value->id]->news_image_path = $value->news_image_path;
}
elseif ($value->news_main_image == '0')
{
// DO SOMETHING HERE that will copy to the new array but it wont copy over the existing $tmp[$value->id]->news_image_path
$tmp[$value->id] = $value;
$tmp[$value->id]->news_image_path = NULL;
}
else
{
$tmp[$value->id] = $value;
}
}
$result = $tmp;
答案 0 :(得分:0)
我略微改进了你的代码并尝试了它,并且工作正常。
foreach ($itemArray as $item)
{
$tmp[$item->id] = $item;
if ($item->news_main_image == '0'){
$tmp[$item->id]->news_image_path = NULL;
}
}
$result = $tmp;
但是它应该和你的一样完全没有......
答案 1 :(得分:0)
如果我理解你,假设你有一个看起来像这样的对象数组:
array(2) {
[0]=>
object(stdClass)#22 (2) {
//....
["news_image_path"]=>
string(23) "14545819551_gallery.jpg"
["news_main_image"]=>
string(1) "1"
}
[1]=>
object(stdClass)#23 (2) {
//...
["news_image_path"]=>
string(23) "14545819552_gallery.jpg"
["news_main_image"]=>
int(0)
}
}
您可以尝试以下代码
//$o is the array of objects you have
//$res will be your result array of objects
$res = array();
foreach ( $o as $a ){
//$tmp will be your new object
$tmp = $a;
if (! $a->news_main_image ){
$tmp->news_image_path = null;
}
$res[] = $tmp;
}
,您的输出将如下所示
array(2) {
[0]=>
object(stdClass)#22 (2) {
["news_image_path"]=>
string(23) "14545819551_gallery.jpg"
["news_main_image"]=>
string(1) "1"
}
[1]=>
object(stdClass)#23 (2) {
["news_image_path"]=>
NULL
["news_main_image"]=>
int(0)
}
}
希望有所帮助
答案 2 :(得分:0)
虽然我讨厌使用两个foreach
语句,但这是解决方案。如果有人能写得更好,请告诉我:
foreach ($result as $key => $value)
{
if ($value->news_main_image == '1')
{
$news_image_path[$value->id] = $value->news_image_path;
}
else
{
$value->news_image_path = NULL;
}
$tmp[$value->id] = $value;
}
foreach ($news_image_path as $key => $value)
{
$tmp[$key]->news_image_path = $value;
}
$result = $tmp;