How to push hash into array of hash in php?

我是研究僧i 提交于 2020-01-02 23:11:30

问题


Like array_push() where we can push an element in to array. I want to push an hash [name,url] in to an array of hash.


回答1:


ifif i understand your problem, you want to retrieve hash value from a url then use parse_url with PHP_URL_FRAGMENT argument

$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

Reference




回答2:


If you're referring to associative arrays where the key is user-provided (rather than an auto-incrementing numeric field), just use direct syntax:

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

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

I strongly recommend reading the PHP manual section on the topic. That's what it's there for.




回答3:


I do not know, what do you need, but it you need to push pair of values into array, this may 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 that (each element of the bigger array is array itself - associative array with two keys and two values corresponding to them):

[
    ['name' => 'something1', 'url' => 'http://www1'],
    ['name' => 'something2', 'url' => 'http://www2']
]



回答4:


<?php
    $aArrayOfHash['example'] = 'http://example.com/';
?>


来源:https://stackoverflow.com/questions/6123122/how-to-push-hash-into-array-of-hash-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!