标签: associative-array

PHP - 关联数组从键一和键二值分配第三个键值

我有一个关联数组,如下所示:

$sensorThreshold = [
  'ph' => [
    'minPh' => $sensorThreshold->where('name', 'Ph')->last()->min_threshold ?? 0,
    'maxPh' => $sensorThreshold->where('name', 'Ph')->last()->max_threshold ?? 0,
    'sum' =>  $sensorThreshold->where('name', 'Ph')->last()->min_threshold + $sensorThreshold->where('name', 'Ph')->last()->max_threshold,
  ]
]
Run Code Online (Sandbox Code Playgroud)

当我定义数组时如何访问minPh和值?maxPh喜欢:

$sensorThreshold = [
  'ph' => [
    'minPh' => $sensorThreshold->where('name', 'Ph')->last()->min_threshold ?? 0,
    'maxPh' => $sensorThreshold->where('name', 'Ph')->last()->max_threshold ?? 0,
    'sum' =>  minPh + maxPh,
  ]
]
Run Code Online (Sandbox Code Playgroud)

php arrays associative-array laravel

-1
推荐指数
1
解决办法
85
查看次数

json_decode:关联数组的对象

如何将这段可怕的代码转换为关联数组?:d

<?php
$json = file_get_contents('http://pastebin.com/NLHGMapT');
$track_data = json_decode($json);
?>
<ul>
<?php
    foreach ($track_data as $data)

            $text0 = $data->track[0]->artist->{'#text'};
            $text1 = $data->track[1]->artist->{'#text'};
            $text2 = $data->track[2]->artist->{'#text'};
            $text3 = $data->track[3]->artist->{'#text'};
            $artist0 = $data->track[0]->name;
            $artist1 = $data->track[1]->name;
            $artist2 = $data->track[2]->name;
            $artist3 = $data->track[3]->name;
            $url0 = $data->track[0]->url;
            $url1 = $data->track[1]->url;
            $url2 = $data->track[2]->url;
            $url3 = $data->track[3]->url;
        echo '<li><a href="'; echo $url0; echo '"><span>'; echo $text0; echo '</span> - <span>'; echo $artist0; echo '</span></li></a>';
        echo '<li><a href="'; echo $url1; echo '"><span>'; echo $text1; echo '</span> - …
Run Code Online (Sandbox Code Playgroud)

php arrays json associative-array

-2
推荐指数
1
解决办法
5472
查看次数

Nodejs:如何遍历关联数组

所以,假设我有一个带有一堆值的数组

"abc" = 5
"bcd" = 12
"ddd" = 13
Run Code Online (Sandbox Code Playgroud)

我希望能够以类似于的格式循环并打印所有这些

abc: 5
bcd: 12
ddd: 13
Run Code Online (Sandbox Code Playgroud)

如果有一个关联.数组很大,我不知道所有的键.如何打印出所有键和值?

谢谢

arrays associative-array node.js

-2
推荐指数
1
解决办法
8215
查看次数

转换PHP数组

我有php数组像:

['AL'=>'Albania','AD'=>'Andorra','AT'=>'Austria']
Run Code Online (Sandbox Code Playgroud)

我需要将其转换为

[[code=>'AL',country=>'Albania'],[code=>'AD',country=>'Andorra'],[code=>'AT',country=>'Austria']].
Run Code Online (Sandbox Code Playgroud)

如何在PHP中执行此操作?

php associative-array

-3
推荐指数
1
解决办法
104
查看次数

为什么JSON没有关联数组?

为什么JSON没有关联数组?YAML.

你不能在JSON中这样做:

{
    productsBySku: [
        12J432: {
            price: 49.99,
            qty_in_stock: 4
        },
        5X4352: {
            price: 29.99,
            qty_in_stock: 5
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

但是在YAML你可以这样做:

productsBySku:
  - 12J432: 
        price: 49.99,
        qty_in_stock: 4
  - 5X4352:
        price: 29.99,
        qty_in_stock: 5
Run Code Online (Sandbox Code Playgroud)

我在技术层面上想知道为什么这个决定可能是针对JSON做出的.

arrays json yaml associative-array

-4
推荐指数
1
解决办法
80
查看次数

标签 统计

associative-array ×5

arrays ×4

php ×3

json ×2

laravel ×1

node.js ×1

yaml ×1