Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] Wyświetl nty element z tablicy, po elemencie o wartości x
d0m1n1k_
post
Post #1





Grupa: Zarejestrowani
Postów: 208
Pomógł: 3
Dołączył: 13.07.2009
Skąd: Tarnów

Ostrzeżenie: (0%)
-----


Witam,
poszukuję rozwiązania, które pozwoliłoby mi wyszukać w tablicy po wartości a nie kluczu.
Po czym wyświetliłoby mi przykładowo 4 element w kolejności w tej tabeli.

Szukałem w googlaku, ale wszystkie rozwiązania jakie widzę, odnoszą się do kluczy a nie wartości.

  1. function get_next($array, $key) {
  2. $currentKey = key($array);
  3. while ($currentKey !== null && $currentKey != $key) {
  4. next($array);
  5. $currentKey = key($array);
  6. }
  7. return next($array);
  8. }


Z góry dziękuję za pomoc.
Pozdrawiam


--------------------
Prywatna koncepcja pasji - SprawdzAuto.info
Go to the top of the page
+Quote Post
Pyton_000
post
Post #2





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


  1. <?php
  2. $tab = ['a', 'b', 'c', 'd', 'e', 'f'];
  3.  
  4. $search = array_search('c', $tab);
  5.  
  6. foreach(array_slice($tab, $search, 5) as $val) {
  7. echo $val . PHP_EOL;
  8. }
Go to the top of the page
+Quote Post
d0m1n1k_
post
Post #3





Grupa: Zarejestrowani
Postów: 208
Pomógł: 3
Dołączył: 13.07.2009
Skąd: Tarnów

Ostrzeżenie: (0%)
-----


hmm, próbowałem to zastosować, jednak tablica z której korzystam posiada klucze i wartości, a mówiąc precyzyjniej jest generowana przez PHP Simple HTML DOM Parser
w procesie wyciągania tekstu, poniżej kod jaki temu służy:

  1. $link = 'http://www.ebay.com/itm/samochod_do_ktorego_potrzebuje_pobrac_przebieg_ktory_znajduje_sie_w_trzecim_elem
    encie_po_slowie__Mileage:__'
    ;
  2. $html = file_get_html($link);
  3. foreach($html->find('text') as $g)
  4. $tabelka5[] = $g->plaintext;
  5.  
  6. //Tak próbowałem dołożyć twoją propozycję:
  7. $search = array_search(' Mileage: ', $tabelka5);
  8.  
  9. foreach(array_slice($tabelka5, $search, 3) as $val) {
  10. echo $val . PHP_EOL;
  11. }


Ze względu na strukturę kodu, nie jestem wstanie zastosować bardziej precyzyjnego zapytania.
Przebieg znajduje się w tabelce nie oznaczanej niczym nadzwyczajnym lub identyfikacyjnym, ponadto może być pierwsza, piąta i n-ta w kolejności.

  1. <td class="attrLabels">Mileage: </td>
  2. <td width="50.0%"><span>96,150</span></td>


Ten post edytował d0m1n1k_ 26.10.2015, 11:16:36


--------------------
Prywatna koncepcja pasji - SprawdzAuto.info
Go to the top of the page
+Quote Post
Pyton_000
post
Post #4





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


Pokaż tablicę źródłową i wynik jaki ma być.
Go to the top of the page
+Quote Post
d0m1n1k_
post
Post #5





Grupa: Zarejestrowani
Postów: 208
Pomógł: 3
Dołączył: 13.07.2009
Skąd: Tarnów

Ostrzeżenie: (0%)
-----


Jest tego naprawdę dużo, więc obetnę w kilku miejscach:

Wynik - który wychodzi jeśli podam key i pomijam funkcję array_search():
  1. array(4) { [0]=> string(23) " Mileage: " [1]=> string(25) " " [2]=> string(13) " " [3]=> string(6) "96,150" }


Tablica:
  1. array(2272) { [0]=> string(1) " " [1]=> string(1) " " [2]=> string(1) " " [3]=> string(1) " " [4]=> string(157) " body #Body .btn, body #Body c-std { filter:none; -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr='#0079bc',endColorstr='#00509d')"; } " [5]=> string(2) " " [6]=> string(3) " " [7]=> string(2) " " [8]=> string(2) " " [9]=> string(37) " Ford Other Base Sedan 2 Door | eBay " [10]=> string(2) " " [11]=> string(7) " " [12]=> string(1) " " [13]=> string(293) "body .is.vi-ppc-main .mm-dp { width:35%; } #vi-snippet-description-main.u-padB20 { padding-top:10px; float:left; } .vi-descsnpt-feedbacklnk { position: relative; top: -20px; width: 500px; margin-left: 250px; } .asqMain { clear:both; } div#bsi-c { clear: both; }" [14]=> string(262) " html a:link{ color: #0654ba; } ul#bc a:link{ color: #0654ba; } .mbg a:link{ color: #0654ba !important; } .mbg-l a:link{ color: #0654ba !important; } #Body .nav-tabs-m a:link{ color: #0654ba; } " [15]=> string(6) " " [16]=> string(2) " " [17]=> string(2) " " [18]=> string(3) " "
  2.  
  3. ...
  4.  
  5. [1156]=> string(23) " Mileage: " [1157]=> string(25) " " [1158]=> string(13) " " [1159]=> string(6) "96,150" [1160]=> string(11) " " [1161]=> string(8) " " [1162]=> string(10) " " [1163]=> string(25) " Fuel Type: " [1164]=> string(25) " " [1165]=> string(13) " " [1166]=> string(8) "Gasoline" [1167]=> string(11) " " [1168]=> string(30) " Exterior Color: " [1169]=> string(25) " " [1170]=> string(13) " " [1171]=> string(6) "Silver" [1172]=> string(11) " " [1173]=> string(8) " " [1174]=> string(10) " " [1175]=> string(27) " For Sale By: " [1176]=> string(25) " " [1177]=> string(13) " " [1178]=> string(14) "Private Seller" [1179]=> string(11) " " [1180]=> string(30) " Interior Color: " [1181]=> string(25) " " [1182]=> string(13) " " [1183]=> string(10) "Blue/Black" [1184]=> string(11) " " [1185]=> string(8) " " [1186]=> string(10) " " [1187]=> string(35) " Disability Equipped: " [1188]=> string(25) " " [1189]=> string(13) " " [1190]=> string(2) "No" [1191]=> string(11) " " [1192]=> string(35) " Number of Cylinders: " [1193]=> string(25) " " [1194]=> string(13) " " [1195]=> string(1) "8" [1196]=> string(11) " " [1197]=> string(8) " " [1198]=> string(10) " " [1199]=> string(8) " " [1200]=> string(8) " " [1201]=> string(13) " " [1202]=> string(4) " " [1203]=> string(2) " " [1204]=> string(5) " " [1205]=> string(3) " " [1206]=> string(5) " " [1207]=> string(100) "if ((typeof (oGaugeInfo) != "undefined")){var descGaugeStartInfo = {descST:(new Date()).getTime()};}" [1208]=> string(8) " " [1209]=> string(8) " " [1210]=> string(1) " " [1211]=> string(5) " " [1212]=> string(2) " " [1213]=> string(3) " " [1214]=> string(37) "Questions and answers about this item" [1215]=> string(3) " " [1216]=> string(2) " " [1217]=> string(2) " " [1218]=> string(60) " No questions or answers have been posted about this item." [1219]=> string(2) " " [1220]=> string(3) " " [1221]=> string(14) "Ask a question" [1222]=> string(31) " - opens in a new window or tab" [1223]=> string(5) " " [1224]=> string(2) " " [1225]=> string(2) " " [1226]=> string(2) " " [1227]=> string(3) " " [1228]=> string(1) " " [1229]=> string(9) " " [1230]=> string(18) " " [1231]=> string(35) " " [1232]=> string(26) " " [1233]=> string(53) " Seller assumes all responsibility for this listing." [1234]=> string(74) " Vehicle history reports are only available on vehicles made after 1981." [1235]=> string(2) " " [1236]=> string(23) " " [1237]=> string(18) " " [1238]=> string(22) " " [1239]=> string(53) " Seller assumes all responsibility for this listing." [1240]=> string(11) " " [1241]=> string(12) " " [1242]=> string(7) " "
  6.  
  7. ...
  8.  
  9. [2155]=> string(7) " " [2156]=> string(6) " " [2157]=> string(2) " " [2158]=> string(4) " " [2159]=> string(3) " " [2160]=> string(3) " " [2161]=> string(3) " " [2162]=> string(2) " " [2163]=> string(6) " " [2164]=> string(7) " " [2165]=> string(2) " " [2166]=> string(3) " " [2167]=> string(3) " " [2168]=> string(3) " " [2169]=> string(3) " " [2170]=> string(3) " " [2171]=> string(2) " " [2172]=> string(1) " " [2173]=> string(25) "No ratings or reviews yet" [2174]=> string(6) " " [2175]=> string(6) " " [2176]=> string(7) " " [2177]=> string(16) "Be the first to " [2178]=> string(14) "write a review" [2179]=> string(6) " " [2180]=> string(5) " " [2181]=> string(4) " " [2182]=> string(6) " " [2183]=> string(5) " " [2184]=> string(5) " " [2185]=> string(6) " " [2186]=> string(2) " " [2187]=> string(1) " " [2188]=> string(1) " " [2189]=> string(3) " " [2190]=> string(4) " " [2191]=> string(5) " " [2192]=> string(4) " " [2193]=> string(5) " " [2194]=> string(4) " " [2195]=> string(5) " " [2196]=> string(4) " " [2197]=> string(3) " " [2198]=> string(2) " " [2199]=> string(3) " " [2200]=> string(1) " " [2201]=> string(4) " " [2202]=> string(5) " " [2203]=> string(7) " " [2204]=> string(7) " " [2205]=> string(17) "Back to home page" [2206]=> string(6) " " [2207]=> string(6) " " [2208]=> string(3) " " [2209]=> string(5) " " [2210]=> string(13) "Return to top" [2211]=> string(3) " " [2212]=> string(2) " " [2213]=> string(3) " " [2214]=> string(4) " " [2215]=> string(17) "More to explore :" [2216]=> string(4) " " [2217]=> string(55) "Ford Falcon Sedan Delivery Interior Door Panels & Parts" [2218]=> string(1) "," [2219]=> string(5) " " [2220]=> string(30) "Ford Country Sedan Carburetors" [2221]=> string(1) "," [2222]=> string(5) " " [2223]=> string(24) "Ford Country Sedan Hoods" [2224]=> string(1) "," [2225]=> string(5) " " [2226]=> string(26) "Ford Country Sedan Fenders" [2227]=> string(1) "," [2228]=> string(5) " " [2229]=> string(38) "Ford Country Sedan Electronic Ignition" [2230]=> string(1) "," [2231]=> string(5) " " [2232]=> string(40) "Ford Country Sedan Differentials & Parts" [2233]=> string(5) " " [2234]=> string(2) " " [2235]=> string(1) " " [2236]=> string(14) " " [2237]=> string(1) " " [2238]=> string(43) "*Provided by AutoCheck, a part of Experian." [2239]=> string(372) " Copyright Experian Information Solutions, Inc. 2010. All rights reserved. Experian provides these reports to you on an "as is" basis and eBay is not responsible or liable for the comprehensiveness or accuracy of the reports. eBay does not endorse any information contained in the reports. If you have any questions about the reports, contact Experian." [2240]=> string(1) " " [2241]=> string(3) " " [2242]=> string(4) " " [2243]=> string(26) "Additional site navigation" [2244]=> string(10) "About eBay" [2245]=> string(13) "Announcements" [2246]=> string(9) "Community" [2247]=> string(15) "Security Center" [2248]=> string(17) "Resolution Center" [2249]=> string(25) "Seller Information Center" [2250]=> string(8) "Policies" [2251]=> string(10) "Affiliates" [2252]=> string(14) "Help & Contact" [2253]=> string(8) "Site Map" [2254]=> string(54) "Copyright Š 1995-2015 eBay Inc. All Rights Reserved. " [2255]=> string(14) "User Agreement" [2256]=> string(2) ", " [2257]=> string(7) "Privacy" [2258]=> string(5) " and " [2259]=> string(7) "Cookies" [2260]=> string(1) "." [2261]=> string(0) "" [2262]=> string(0) ""
  10.  
  11. ...
  12.  
  13. [2266]=> string(0) "" [2267]=> string(13) " " [2268]=> string(21) " " [2269]=> string(1) " " [2270]=> string(1) " " [2271]=> string(6) "" }


--------------------
Prywatna koncepcja pasji - SprawdzAuto.info
Go to the top of the page
+Quote Post
Pyton_000
post
Post #6





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


No dobra, ale to co Ci podałem powinno działać.

Go to the top of the page
+Quote Post
d0m1n1k_
post
Post #7





Grupa: Zarejestrowani
Postów: 208
Pomógł: 3
Dołączył: 13.07.2009
Skąd: Tarnów

Ostrzeżenie: (0%)
-----


Sam $search wywala bool(false) przy var_dump.
I to niezależnie czy wybieram:
"Mileage", "Mileage:", "Mileage: ", " Mileage:", " Mileage: ", "Mileage~", "~Mileage:~"


--------------------
Prywatna koncepcja pasji - SprawdzAuto.info
Go to the top of the page
+Quote Post
Pyton_000
post
Post #8





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


Bo Twój szukany ciąg znaków ma długość 25.

Przy dodawaniu do tablicy daj trim($g->plaintext)
i wtedy szukaj "Mileage:"
Go to the top of the page
+Quote Post
d0m1n1k_
post
Post #9





Grupa: Zarejestrowani
Postów: 208
Pomógł: 3
Dołączył: 13.07.2009
Skąd: Tarnów

Ostrzeżenie: (0%)
-----


Nadal bez zmian.
Ale na przekór przeciwnościom, znalazłem rozwiązanie które wyszukuje mi pole Mileage, ale nie wiem jak wstawić do array_slice

  1. $inputx = preg_quote('Mileage', '~');
  2. $resulx = preg_grep('~' . $inputx . '~', $tabelka5);


Wynik:
  1. array(1) { [1159]=> string(8) "Mileage:" }


Trochę pokombinowałem i udało się.
Końcowy kod:

  1. $inputx = preg_quote('Mileage:', '~');
  2. $resulx = preg_grep('~' . $inputx . '~', $tabelka5);
  3. $pierwszy_klucz = key($resulx);
  4. //$search został zastąpiony $first_key
  5.  
  6. foreach(array_slice($tabelka5, $pierwszy_klucz , 5) as $val) {
  7. echo "Wynik: " . $val . PHP_EOL . "<br/>";
  8. }


Teraz tylko muszę sobie poradzić z przemianowaniem foreach na wyświetlenie tylko piątego wyniku ;-]

Zrobione ;-)

  1. $przebieg = array_slice($tabelka5, $pierwszy_klucz, 5);
  2. echo $przebieg[3];


Ten post edytował d0m1n1k_ 26.10.2015, 12:36:18


--------------------
Prywatna koncepcja pasji - SprawdzAuto.info
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 21.08.2025 - 03:53