How to push a hash into a hash array in php?

Like array_push (), where we can push an element into an array. I want the hash [name, URL] in the hash array.

+1
source share
4 answers

If I understand your problem, you want to get the hash value from the URL, then use parse_url with the argument PHP_URL_FRAGMENT

 $url = 'http://username: password@hostname /path?arg=value#anchor'; print_r(parse_url($url)); echo parse_url($url, PHP_URL_FRAGMENT); 

will return

  [fragment] => anchor 

Link

-1
source

If you are referring to associative arrays where the key is provided to the user (rather than an automatically incrementing number field), simply use the direct syntax:

 $a = Array(); $a['name'] = 'url'; 

Note that $a = Array(); array_push($a, 'lol'); $a = Array(); array_push($a, 'lol'); is (almost) the same as $a = Array(); $a[] = 'lol'; $a = Array(); $a[] = 'lol'; . array_push is just a (meaningless) "shortcut" for the same syntax that works only for automatic numeric indices.

I highly recommend reading the PHP manual section on the topic . What is it there for.

+7
source

I don't know what you need, but you need to insert a couple of values ​​into an array, this might be your solution:

 $hashes_array = array(); array_push($hashes_array, array( 'name' => 'something1', 'url' => 'http://www1', )); array_push($hashes_array, array( 'name' => 'something2', 'url' => 'http://www2', )); 

After that, $hashes_array should look like this (each element of the larger array is the array itself - an associative array with two keys and two corresponding values):

 [ ['name' => 'something1', 'url' => 'http://www1'], ['name' => 'something2', 'url' => 'http://www2'] ] 
+4
source
 <?php $aArrayOfHash['example'] = 'http://example.com/'; ?> 
+2
source

Source: https://habr.com/ru/post/900382/


All Articles