1

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.

4
  • 1
    Could you please describe what you mean with hash? Commented May 25, 2011 at 10:40
  • What is an "array of hash"? Are you looking for PHP's equivalent to Perl's hashes, "associative arrays"? Commented May 25, 2011 at 10:43
  • well, it's like map or key-value pair. I'm parsing few files in a for loop & i want to add both name of file & url of file into an array which is nothing but an array of map. i.e. i want to add or push a key,value pair into an array. i.e. array_push(name,url) to an array of map. I'm new to PHP...may be i'm wrong with the php terminology...but that is what i need to do. Commented May 25, 2011 at 10:46
  • Please read the PHP manual page on arrays to understand how they work in PHP. There's a link in my answer. Commented May 25, 2011 at 18:11

4 Answers 4

8

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for pointing that array_push() is actually pointless in most of the situations and can be replaced with $a[] = 'something' (unless of course you want it to get the number of the elements after the update within the same line).
6

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 Comments

Yes, i want this only. Now how can i map 2nd value from 1st value. [key/value] pair here ?
I need to sort 1st value [name] or key, 2nd value should also get sorted on that basis.
@mandy I did not understand what you are talking about, but the accepted answer shows that you were trying to find out how to create associative arrays. I did use associative arrays, but did not explain it the way you needed it to be explained, I think.
hey thanks...i was looking for something like hash or map [perl background], but associate arrays has done my job.
2
<?php
    $aArrayOfHash['example'] = 'http://example.com/';
?>

Comments

-2

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 Comments

This is accepted? It seems to have nothing to do with the question :/
That's why I write it before code. Yes, you are right. This answer doesn't have anything to do with question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.