第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在沒有鍵的情況下在php對(duì)象中定位數(shù)組

如何在沒有鍵的情況下在php對(duì)象中定位數(shù)組

PHP
收到一只叮咚 2024-01-19 17:14:20
我有一個(gè)來自郵政編碼數(shù)據(jù)庫項(xiàng)目的郵政編碼數(shù)據(jù)庫,該數(shù)據(jù)庫以 .csv 文件形式提供。我將其轉(zhuǎn)換為JSON,它沒有每個(gè)數(shù)組的鑰匙,它們看起來像這樣:[    {      "zipcode": 35004,      "state abbreviation": "AL",      "latitude": 33.606379,      "longitude": -86.50249,      "city": "Moody",      "state": "Alabama"    }]我已經(jīng)弄清楚了如何搜索所有數(shù)組并找到匹配的郵政編碼,但是我不知道如何使搜索返回?cái)?shù)據(jù)有關(guān)它找到的數(shù)組。我可以這樣搜索:$filename = './assets/data/zips.json';    $data = file_get_contents($filename);    $zipCodes = json_decode($data);    $inputZip = 55555;    foreach ($zipCodes as $location) {        if ($inputZip == $location->zipcode) {            echo "Success";        }    }誰能告訴我如何使用此搜索(或一個(gè)更好的想法)將與搜索的郵政編碼相關(guān)的緯度和經(jīng)度存儲(chǔ)到兩個(gè)變量上?我?guī)缀跽麄€(gè)周末都浪費(fèi)了這個(gè),所以所有的幫助都非常感謝。另外,我沒有使用RAW CSV文件執(zhí)行此功能的問題,但我無法像JSON那樣獲得。感謝大家!
查看完整描述

3 回答

?
肥皂起泡泡

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超6個(gè)贊

要通過數(shù)組項(xiàng)目的屬性找出數(shù)組的索引,請(qǐng)使用Array_column僅獲取該列/屬性的值,然后使用Array_search查找索引。


<?php

$inputZip = 35004;


$index = array_search($inputZip, array_column($zipCodes, 'zipcode')); // 0


print_r($zipCodes[$index]); // the entire object

https://3v4l.org/TAXQq


查看完整回答
反對(duì) 回復(fù) 2024-01-19
?
鴻蒙傳說

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊

要獲取由于必須使用的某些參數(shù)而過濾的數(shù)組的鍵,您可以array_keys使用array_filter。


例如


$array = [1,2,1,1,1,2,2,1,2,1];


$out = array_filter($array,function($v) {

    return $v == 2;

});

print_r(array_keys($out));

輸出


Array

(

    [0] => 1

    [1] => 5

    [2] => 6

    [3] => 8

)

在PHP沙箱中嘗試以上示例


匹配您的實(shí)際數(shù)據(jù)結(jié)構(gòu)。


$json = '[{"zipcode": 35004,"state abbreviation": "AL","latitude": 33.606379,"longitude": -86.50249,"city": "Moody","state": "Alabama"},{"zipcode": 35004,"state abbreviation": "AL","latitude": 33.606379,"longitude": -86.50249,"city": "Moody","state": "Alabama"},{"zipcode": 35005,"state abbreviation": "AL","latitude": 33.606579,"longitude": -86.50649,"city": "Moody","state": "Alabama"}]';

$array = json_decode($json);

$out = array_filter($array, function($v) use ($inputZip) {

    return $v->zipcode == $inputZip; // for the below output $inputZip=35004 

});

print_r(array_keys($out));

輸出


Array

(

    [0] => 0

    [1] => 1

)

在PHP沙箱中嘗試示例



查看完整回答
反對(duì) 回復(fù) 2024-01-19
?
慕田峪4524236

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超5個(gè)贊

根據(jù)您的代碼:


foreach ($zipCodes as $index => $location) { // index is a key

    if ($inputZip == $location->zipcode) {

        echo "Index is ".$index;

        break;

    }

}

var_dump($zipCodes[$index]);

我應(yīng)該注意,看來您在做錯(cuò)了什么,因?yàn)槟幌胂襁@樣存儲(chǔ)數(shù)據(jù)并一直循環(huán)循環(huán)。


查看完整回答
反對(duì) 回復(fù) 2024-01-19
  • 3 回答
  • 0 關(guān)注
  • 173 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)