我有一个像这样的json数组:
"spellsCount": [ { "value": 0, "globalID": 26000000 }, { "value": 0, "globalID": 26000001 }, { "value": 0, "globalID": 26000002 }, { "value": 0, "globalID": 26000003 }, { "value": 0, "globalID": 26000005 }, { "value": 0, "globalID": 26000009 }, { "value": 0, "globalID": 26000011 } ],现在我需要做的是创建一个php循环或一些将遍历整个数组并使值为0的变量;
现在棘手的是,并非所有GlobalID显示在该阵列中。 例如26000010未显示。
我需要它来制作一个像$spells26000000 = 0;的变量$spells26000000 = 0; 对于每个项目。 包括介于两者之间但缺失的那些。 所以也会有一个变量$spells26000010 = 0; 如果使用上面的数组。
最初我有这个:
for ($i=0; $i < 100; $i+=1){// start at index 0 (1st value) and increment by 1 if (isset($data['spellsCount'][$i])) { // just in case there aren't actually 100 // Use variable variables to create the $achievement variables you want $globalID = $data['spellsCount'][$i]['globalID']; ${"spells$globalID"} = $data['spellsCount'][$i]['value']; } }但后来我意识到有些价值观并不会总是显示出来,所以我想我需要这样做,但我不知道如何做。
顺便一提! 我正在使用json_decode这样的$data = json_decode($jsondata, true);
I have a json array like this:
"spellsCount": [ { "value": 0, "globalID": 26000000 }, { "value": 0, "globalID": 26000001 }, { "value": 0, "globalID": 26000002 }, { "value": 0, "globalID": 26000003 }, { "value": 0, "globalID": 26000005 }, { "value": 0, "globalID": 26000009 }, { "value": 0, "globalID": 26000011 } ],Now what I need to do is make a php loop or something that will go through that entire array and make variables with a value of 0;
Now the tricky bit is that not all GlobalIDs show in that array. For example 26000010 is not showing.
I need it to make a variable like $spells26000000 = 0; for each item. INCLUDING ones that are in between but are missing. So there would also be a variable $spells26000010 = 0; if the above array was used.
Initally I had this:
for ($i=0; $i < 100; $i+=1){// start at index 0 (1st value) and increment by 1 if (isset($data['spellsCount'][$i])) { // just in case there aren't actually 100 // Use variable variables to create the $achievement variables you want $globalID = $data['spellsCount'][$i]['globalID']; ${"spells$globalID"} = $data['spellsCount'][$i]['value']; } }But then I realized that some values won't always show so I figured out I need to do it like this but I am not sure how to.
By the way! I am using json_decode like this $data = json_decode($jsondata, true);
最满意答案
<?php $data = json_decode(data(), true); $spellsCounts = []; // make the globalID the index of each respective element foreach($data['spellsCount'] as $sc) { $spellsCounts[ $sc['globalID'] ] = $sc; } // sort the elements by ascending key (i.e. globalID) ksort($data); // run from first.globalID==min to end.globalID==max $min = reset($spellsCounts)['globalID']; // Function array dereferencing has been added in php 5.4 $max = end($spellsCounts)['globalID']; for($i=$min; $i<=$max; $i++) { // ....but this just doesn't sound right // it's just polluting the namespace ${'spells'.$i} = isset($spellsCounts[$i]) ? $spellsCounts[$i]['value'] : 'n/a'; } // how about using the $spellsCount array // and fill it up with the missing values ( in case you really need that) ? for($i=$min; $i<=$max; $i++) { // ....but this just doesn't sound right if ( !isset($spellsCounts[$i]) ) { $spellsCounts[$i] = [ 'globalID' => $i, 'value' => 'n/a' ]; } } var_dump($spellsCounts); function data() { return '{"spellsCount": [ { "value": 0, "globalID": 26000000 }, { "value": 0, "globalID": 26000001 }, { "value": 0, "globalID": 26000002 }, { "value": 0, "globalID": 26000003 }, { "value": 0, "globalID": 26000005 }, { "value": 0, "globalID": 26000009 }, { "value": 0, "globalID": 26000011 } ]}'; } <?php $data = json_decode(data(), true); $spellsCounts = []; // make the globalID the index of each respective element foreach($data['spellsCount'] as $sc) { $spellsCounts[ $sc['globalID'] ] = $sc; } // sort the elements by ascending key (i.e. globalID) ksort($data); // run from first.globalID==min to end.globalID==max $min = reset($spellsCounts)['globalID']; // Function array dereferencing has been added in php 5.4 $max = end($spellsCounts)['globalID']; for($i=$min; $i<=$max; $i++) { // ....but this just doesn't sound right // it's just polluting the namespace ${'spells'.$i} = isset($spellsCounts[$i]) ? $spellsCounts[$i]['value'] : 'n/a'; } // how about using the $spellsCount array // and fill it up with the missing values ( in case you really need that) ? for($i=$min; $i<=$max; $i++) { // ....but this just doesn't sound right if ( !isset($spellsCounts[$i]) ) { $spellsCounts[$i] = [ 'globalID' => $i, 'value' => 'n/a' ]; } } var_dump($spellsCounts); function data() { return '{"spellsCount": [ { "value": 0, "globalID": 26000000 }, { "value": 0, "globalID": 26000001 }, { "value": 0, "globalID": 26000002 }, { "value": 0, "globalID": 26000003 }, { "value": 0, "globalID": 26000005 }, { "value": 0, "globalID": 26000009 }, { "value": 0, "globalID": 26000011 } ]}'; }更多推荐
发布评论