Skip to content
Fix Code Error

How to push both value and key into PHP array

March 13, 2021 by Code Error
Posted By: Anonymous

Take a look at this code:

$GET = array();    
$key = 'one=1';
$rule = explode('=', $key);
/* array_push($GET, $rule[0] => $rule[1]); */

I’m looking for something like this so that:

print_r($GET);
/* output: $GET[one => 1, two => 2, ...] */

Is there a function to do this? (because array_push won’t work this way)

Solution

Nope, there is no array_push() equivalent for associative arrays because there is no way determine the next key.

You’ll have to use

$arrayname[indexname] = $value;
Answered By: Anonymous

Related Articles

  • Composition or Inheritance for classes with almost similar…
  • Reference — What does this symbol mean in PHP?
  • Best way to extract messy HTML tables using BeautifulSoup
  • How does PHP 'foreach' actually work?
  • useEffect Error: Minified React error #321 (GTM…
  • How do I extract data from JSON with PHP?
  • Beautiful way to remove GET-variables with PHP?
  • PHP parse/syntax errors; and how to solve them
  • How to use the encrypt password for login php
  • Reference - What does this regex mean?

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How to hide the title bar for an Activity in XML with existing custom theme

Next Post:

Matching a space in regex

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error