无法获得阵列PHP相同的值 [英] Can't get the same value in array PHP

查看:98
本文介绍了无法获得阵列PHP相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法获得阵列相同的值。第一值只是没有出现在输出中。在code比较ID和加入匹配id.This是我的code。该值:

I can't seem to get the same value in array. The first value just doesn't appear in the output. The code compares the id's and joins the values that match the id.This is my code:

<?php
$pic = array ('1.jpg','2.jpg','3.jpg');
$picid = array('aqua','green','orange');
$size = array('12','24','12');
$sizeid = array ('aqua','green','orange');

$newarray2 = array();

foreach (array_combine($pic, $picid) as $outpic => $outid) {

foreach (array_combine($size, $sizeid) as $outsize => $outsizeid) {

 if ($outid == $outsizeid) {

    $result = "$outpic $outsize";
    $newarray2[]= $result;

} }

$result1 = implode(",", $newarray2);
echo $result1;
$newarray2 = array();
}
?>

所需的输出我想是这样的:

The desired output I want to get is this:

1.jpg 12
2.jpg 24
3.jpg 12

但是,当我运行code我得到这样的:

But when I run the code I get this:

2.jpg 24
3.jpg 12

我觉得第一个值被莫名其妙地覆盖,但我不知道这周围的方式。我在做什么错了?

I think the first value is being overwritten somehow, but I don't know the way around this. What am I doing wrong?

推荐答案

您有12多个密钥,只有最后一个被使用。

You have multiple keys on 12, only the last one is used.

在下面的例子中我们增加额外的层在键,从而密钥可以重叠。

In the example below we add an additional layer to the keys, so that the keys can overlap.

$result = array();
foreach ($size as $i => $key) {
 $result[] = array($key => $sizeid[$i]);
}
foreach ($result as $value) {
 foreach($value as $outsize => $outsizeid){
  if ($outid == $outsizeid) {
   $result = "$outpic $outsize";
   $newarray2[]= $result;
  }
 }
}

这篇关于无法获得阵列PHP相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆