» »

php array in path določenega keya:

php array in path določenega keya:

Nextor ::

Pozdravljeni!

na php.net sem pobral kodo, ki v arrayu poišče določen key in vrne path do keya...

<?php

function search_in_array ($needle, $haystack, $inverse = false, $limit = 1) {
    $path = array ();
    $count = 0;
    if ($inverse == true)
        $haystack = array_reverse ($haystack, true);      
    foreach ($haystack as $key => $value) {
        if ($count > 0 && $count == $limit)   
            return $path;
        if ($value === $needle) {
            $path[] = $key;
            print_r($path);
            $count++;
        } else if (is_array ($value)) {
            $sub = search_in_array ($needle, $value, $inverse, $limit);
            if (count ($sub) > 0) {
                $path[$key] = $sub;
                $count += count ($sub);
            }
        }
    }
    return $path;
}

$info = array(array(array("id" => "25", "last_time" => "1260884655"),
                    array("id" => "26", "last_time" => "1260884656"),
                    array("id" => "27", "last_time" => "1260884657"),
                    array("id" => "28", "last_time" => "1260884658"),
                    array("id" => "29", "last_time" => "1260884659")),
              array(array("id" => "30", "last_time" => "1260884660"),
                    array("id" => "31", "last_time" => "1260884661"),
                    array("id" => "32", "last_time" => "1260884662"),
                    array("id" => "33", "last_time" => "1260884663"),
                    array("id" => "34", "last_time" => "1260884664")),
              array(array("id" => "35", "last_time" => "1260884665"),
                    array("id" => "36", "last_time" => "1260884666"),
                    array("id" => "37", "last_time" => "1260884667"),
                    array("id" => "38", "last_time" => "1260884668"),
                    array("id" => "39", "last_time" => "1260884669")),                    
            );


$r = search_in_array ('38', $info, true);

print_r($r);

?>


print_r vrne...
Array ( [0] => id ) Array ( [2] => Array ( [3] => Array ( [0] => id ) ) ) 


Zdaj pa bi rad tale id z vrednostjo "38" spremenil v ABC, ampak nimam pojma kako naj uporabim tale path, ki mi ga vrne funkcija... kakšna ideja?

Poskusil sem v stilu $info[$r] = 'ABC';, ampak ne deluje... ;((

HardFu ::

$info[2][3]['id'] = 'ABC';
http://codeable.io

Nextor ::

Ja HardPhuck tale rešitev je idealna, če bi že prej vedel kje točno v arrayu se nahaja key z vrednostjo '38'. Tale $info je vedno čist random in mi tale fixni pathi nič ne pomagajo, zato pa iščem po arrayu, ampak ne znam pol uporabit tegale patha, ki mi ga vrne funkcija.

Zato sem poskusil nekaj v stilu... info[$r] ker sem predvideval, da bo delovalo kot $info[2][3]['id'], ampak ne deluje...

Kakšna ideja? :|

Ziga Dolhar ::

$info[$r[0]][$r[1]]

Tkole nekako? (Nisem točno preverjal vrstnega reda in nivoja posamičnih arrayev.)
https://dolhar.si/

Nextor ::

Ziga tudi to se poskusil, ampak ne deluje...

Array, ki ga vrne funkcija je malo čuden...
Array
(
    [2] => Array
        (
            [3] => Array
                (
                    [0] => id
                )

        )

)


Nimam pojma kako naj to uštimam ;(( Še kakšna ideja? 8-O

Ziga Dolhar ::

A. Ti morda pomaga array_keys()?

$level1 = array_keys($r);
$level1 = $level1[0];
$level2 = array_keys($r[$level1]);
$level2 = $level2[0];

var_dump($info[$level1][$level2]);


Sori k neki pametujem, res se že zelooo dolgo nisem ukvarjal s PHPjem :).
https://dolhar.si/

Zgodovina sprememb…

Packač ::

Kar napiši svojo funkcijo, ki ti bo vrnila ustrezen array.

HardFu ::

Pa ce mas takole razmetane arraye, pomeni da je ponavadi z logiko aplikacije nekaj narobe, rajsi razmisli o resitvi na visjem nivoju
http://codeable.io

Nextor ::

HardPhuck rešitev na visjem nivoju? Kakšno rešitev pa predlagaš? Namreč funkcija ni moja, ampak sem jo pobral iz php.net

HardFu ::

Najprej razlozi kaj bi rad dosegel s to kodo
http://codeable.io

Ziga Dolhar ::

"višji nivo" --> opis delovanja oz. željenega rezultata. Tko, po kmečko :).
https://dolhar.si/

Packač ::

Na približno tak način (pa ne uporabi tega direktno, samo tisti opis "Višji nivo"):
class Item
{
public $id;
public $timestamp;

public function __construct($itemId, $itemTimestamp)
{
$this->id = $itemId;
$this->itemTimestamp = $itemTimestamp;
}
}

class Item_List
{
public $list = array()

public function loadItems()
{
// sql
foreach($items as $item) {
$this->list[$item->id] = new Item($item->id, $item->timestamp);
}
}

public function getItem($itemId)
{
return $list[$itemId];
}
}

Zgodovina sprememb…

  • spremenil: Packač ()

Nextor ::

Ok. Pa začnimo...

$info = array(1 => array(array("id" => "25", "last_time" => "1260884655"),
                         array("id" => "26", "last_time" => "1260884656"),
                         array("id" => "27", "last_time" => "1260884657"),
                         array("id" => "28", "last_time" => "1260884658"),
                         array("id" => "29", "last_time" => "1260884659")),
              2 => array(array("id" => "30", "last_time" => "1260884660"),
                         array("id" => "31", "last_time" => "1260884661"),
                         array("id" => "32", "last_time" => "1260884662"),
                         array("id" => "33", "last_time" => "1260884663"),
                         array("id" => "34", "last_time" => "1260884664")),
              3 => array(array("id" => "35", "last_time" => "1260884665"),
                         array("id" => "36", "last_time" => "1260884666"),
                         array("id" => "37", "last_time" => "1260884667"),
                         array("id" => "38", "last_time" => "1260884668"),
                         array("id" => "39", "last_time" => "1260884669")),
              );


Podatki, ki so nafilani v $info so parsani iz druge strani in so dejansko posti uporabnikov z id-jem, ki se lahko pojavijo večkrat in zadnjih timestampom posta. Mojo funkcijo sem si zamislo nekako tako... naprej preveri, če se določeni osebek z id-jem (za primer id='33') že nahaja v kater izmed pod arrayov v arrayu $info. Če ga najde ($info[2][2]['id']) ga odstrani iz pod arraya $info[2] in ga doda v pod array $info[3]... zakaj? Ker se je uporabnik z id-jem 33 pojavil že tretjič v parsanih podatkih zato ima count 3 oz. se bo ga prestavo v pod array $info[3]. Preden ga doda v $info[3] še preveri po timestampu na katero mesto v pod arrayu $info[3] ga mora dodat.

V bistvu bi rad naredil top lestvico najbolj aktivnih iz nekega drugega paga... da bi pol dobil nekaj takega...
Array (
  0 => array(id => '39', count => '3', timestamp => '1260884669'),
  1 => array(id => '38', count => '3', timestamp => '1260884668'),
  2 => array(id => '37', count => '3', timestamp => '1260884667'),
  3 => array(id => '36', count => '3', timestamp => '1260884666'),
  4 => array(id => '35', count => '3', timestamp => '1260884665'),
  5 => array(id => '34', count => '2', timestamp => '1260884664'),
  6 => array(id => '33', count => '2', timestamp => '1260884663'),
  7 => array(id => '32', count => '2', timestamp => '1260884662'),
  5 => array(id => '31', count => '2', timestamp => '1260884661'),
)


Evo po kmječko :D

Zgodovina sprememb…

  • spremenilo: Nextor ()

HardFu ::

Evo neki na hitro, tole ti dobi vse IDje, naprej bos pa verjetno sam znal (count in to)
<?php

class Extract {
	private $values;
	
	private function multiarray_keys($array) {
	    foreach($array as $key => $value) {
			if($key === 'id') {
				$this->values[] = $value;
			}
	        if (is_array($array[$key])) {
				$this->multiarray_keys($array[$key]);
			}
	    }
	    return $this->values;
	}
	
	public function search($array) {
		echo '<pre>';
		print_r($this->multiarray_keys($array));
		echo '</pre>';
	}
	
}

$info = array(array(array("id" => "25", "last_time" => "1260884655"),
                    array("id" => "26", "last_time" => "1260884656"),
                    array("id" => "27", "last_time" => "1260884657"),
                    array("id" => "28", "last_time" => "1260884658"),
                    array("id" => "29", "last_time" => "1260884659")),
              array(array("id" => "30", "last_time" => "1260884660"),
                    array("id" => "31", "last_time" => "1260884661"),
                    array("id" => "32", "last_time" => "1260884662"),
                    array("id" => "33", "last_time" => "1260884663"),
                    array("id" => "34", "last_time" => "1260884664")),
              array(array("id" => "35", "last_time" => "1260884665"),
                    array("id" => "36", "last_time" => "1260884666"),
                    array("id" => "37", "last_time" => "1260884667"),
                    array("id" => "38", "last_time" => "1260884668"),
                    array("id" => "39", "last_time" => "1260884669")),                    
            );

$extract = new Extract();
$extract->search($info);

?>
http://codeable.io

Zgodovina sprememb…

  • spremenil: HardFu ()

Nextor ::

Hvala HardPhuck!

Sem še jo malo predelal...
    private function multiarray_keys($array, $needle, $key, $searchlevel = 0) {
      while(is_array($array) && isset($array[key($array)])) {        
        if($searchlevel == 0) {
          if (key($array) == $key && $array[key($array)] == $needle) {
            return array(true,array(key($array)));
          }
        } elseif($searchlevel > 0) {
          $sub = $this->multiarray_keys($array[key($array)], $needle, $key, $searchlevel-1);
          if (isset($sub[0]) && ($sub[0])) {
            $sub[1][] = key($array);
            return array(true,$sub[1]);
          }
        }
        next($array);
      }
      return array(false);
    }

HardFu ::

Super, me veseli da ti je uspelo, ce te bolj podrobno zanima php ima lynda.com par zanimivih (in zelo izobrazevalnih) tecajev
http://codeable.io


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

Urnik if + date() [PHP - začetnik]

Oddelek: Izdelava spletišč
6673 (499) t3hn0
»

Php varen login

Oddelek: Programiranje
203909 (3409) Volk|
»

[PHP] empty array

Oddelek: Programiranje
91859 (1726) BigWhale
»

[(Cake)PHP] Shranjevanje povezanih tabel

Oddelek: Izdelava spletišč
5810 (735) vuego
»

Osnovno php vprašanje

Oddelek: Izdelava spletišč
141594 (1315) R33D3M33R

Več podobnih tem