简体   繁体   English

PHP:如何仅从数组中为项的每个值打印一次键?

[英]PHP: How do I print the key from array only once and every value for each item?

So here's my code: 所以这是我的代码:

foreach ($result->devices as $device) {
    $flag = false;
    foreach ($device as $key => $value) {
        if(!$flag){
            var_dump($key);
            $flag = true;
        }
        var_dump($value);
    }
}

What I try to achieve is to only print the key (description, brand, etc...) only once but print the value of all those keys for every device found. 我试图实现的是只打印一次密钥(描述,品牌等),但为找到的每个设备打印所有这些密钥的值。 This is to make it eventually save as an .csv 这是为了使其最终另存为.csv

Note: $result = a multidimensional array where I'm only interested in the devices section of it, hence the $result->devices . 注意:$ result =一个多维数组,在这里我只对它的devices部分感兴趣,因此$result->devices The way this works now is it only prints the first key for every device found, what I get now looks something like: 现在的工作方式是,它只为找到的每个设备打印第一个密钥,我现在得到的内容类似于:

string 'deviceId' (length=8) // keeps getting repeated
int 4268
string 'beschrijving nr 53' (length=18)
 ...
string 'deviceId' (length=8) // keeps getting repeated
int 4267
string 'Weer een tooltje' (length=16)

What I try to get is something resembling: 我试图得到的是类似的东西:

string 'deviceId' (length=8)
string 'description' (length=??)
string 'status' (length=??)
 ... // rest of keys, followed by devices
int 4268
string 'beschrijving nr 53' (length=18)
 ... // rest of values, followed by other devices
int 4267
string 'Weer een tooltje' (length=16)

I'm sure it's something simple like the wrong order of operation or moving something in/out a certain loop, but after having tried various variations, I'm none the wiser. 我敢肯定这很简单,例如操作顺序错误或将某些内容移入/移出某个循环,但是尝试了各种变体之后,我再也不明智了。

So to whom may solve this mystery, I thank you greatly. 因此,谁可以解决这个难题,我深表感谢。

So, I suppose, your data looks kind of like this? 所以,我想,您的数据看起来像这样吗?

$result = (object)[];

$result->devices = [
    ['deviceId' => 1, 'description' => 'desc1', 'status' => 'status14'],
    ['deviceId' => 2, 'description' => 'desc2', 'status' => 'status15'],
    ['deviceId' => 3, 'description' => 'desc3', 'status' => 'status16'],
    ['deviceId' => 4, 'description' => 'desc2', 'status' => 'status17'],
];

Then to create a CSV file from it I would go like this: 然后从中创建一个CSV文件,我将像这样:

$fp = fopen('csv.csv', 'w+');

foreach($result->devices as $i => $device)
{
    // First row? Write headers (keys) first!
    if($i == 0) fputcsv($fp, array_keys($device)); 

    // Write data rows
    fputcsv($fp, array_values($device));
}
fclose($fp);

This way, you will get this: 这样,您将获得以下信息:

deviceID,description,status
1,desc1,status14
2,desc2,status15
3,desc3,status16
4,desc4,status17

Is this what you need? 这是您需要的吗?

Thanks to Oleg Dubas, I've found the problem. 感谢Oleg Dubas,我找到了问题所在。 It appears I was overcomplicating things (as I tend to do), in combination with trying to print out parts of an object, instead of having the object cast into an array. 似乎我在使事情变得过于复杂(我倾向于这样做),并尝试打印出对象的某些部分,而不是将对象转换为数组。 That's why when I tried a similar approach like this before, it didn't work. 这就是为什么当我尝试过类似的方法时,它不起作用。 This is the new and working code: 这是新的工作代码:

foreach ($result->devices as $key => $device) {
    if ($key == 0) {
        fputcsv($out, array_keys((array)$device), "\t");
    }
    fputcsv($out, array_values((array)$device), "\t");
}

As you can see it is almost identical to Oleg's code, but I just had to cast the object to an associative array. 如您所见,它几乎与Oleg的代码相同,但是我只需要将对象转换为关联数组即可。

Thanks again for the help, Oleg. 再次感谢您的帮助,Oleg。 It was much appreciated. 非常感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM