I have a array with a key value you on it like:
$some_array['array_key'] = "some string";
Is it possible to use array_push to add more elements to the array?
Ive tried this:
array_push($some_array['array_key'],"another string");
and I have tried other obvious way, but nothing seems to work. Is it possible to add array_push into a array with key value?
Thanks for any help you can offer,
--Bryan
If you want $some_array['array_key'] to be an array of values, you have to initialize it as an array, like this:
$some_array['array_key'] = array('some string');
Only then can you use array_push() or the [] = notation:
$some_array['array_key'][] = 'another string';
Related
$test = ['hi'=>['ho'=>1] ];
$str = 'hi[ho]';
var_dump ( isset($test[$str] ) );
In this example the variable test has a key of 'hi' and the key 'hi' is a multidimensional array with another key of 'ho'.
I am looking for a way to access the ['hi']['ho'] via a string. Something akin to $test[$str] .
This is a simplified example and the real code will have multidimensional arrays with up to 10 deep, but the concept will still be the sam.e
I need to select and group some items according to some values and it's easy using an associative multidimensional array:
$Groups = array(
"Value1" = array("Item1", "Item3"),
"Value2" = array("Item2", "Item4")
);
But some items hasn't the value so my array will be something like:
$Groups = array(
"Value1" = array("Item1", "Item3"),
"Value2" = array("Item2", "Item4")
"" = array("Item5", "Item6")
);
I've tested it (also in a foreach loop) and all seems to work fine but I'm pretty new to php and I'm worried that using an empty key could give me unexpected issues.
Is there any problem in using associative array with empty key?
Is it a bad practice?
If so, how could I reach my goal?
There's no such thing as an empty key. The key can be an empty string, but you can still access it always at $groups[""].
The useful thing of associative arrays is the association, so whether it makes sense to have an empty string as an array key is up to how you associate that key to the value.
You can use an empty string as a key, but be careful, cause null value will be converted to empty string:
<?php
$a = ['' => 1];
echo $a[''];
// prints 1
echo $a[null];
// also prints 1
I think, it's better to declare some "no value" constant (which actually has a value) and use it as an array key:
<?php
define('NO_VALUE_KEY', 'the_key_without_value');
$a = [NO_VALUE_KEY => 1];
Consider I have associative array with same key values:
$arr = {'MessageID' =>1 ,'MessageID' =>5 , 'MessageID' => 8};
Now I want every call to function foo() which will insert a new key value of 'MessageID'=>integer
How can we do that without overriding other existing key values pairs?
As many answerers before, you can't have keys with the same string in the array in PHP. If you want to represent multiple values per key I use something like this:
$arr = ['MessageID' => [1, 5, 8]];
You would append to MessageID with this code:
$arr['MessageId'][] = 10;
Then it would look like this:
['MessageID' => [1, 5, 8, 10]]
This worked well for me with HTTP headers and other things which are key value based, but can have multiple values.
In php array, you can not insert multiple values with same index.
If you want to use it then you can use it with following manner
$MessageID = array(1,5,8);
and
$MessageID[] = $newValue; to insert new value.
You cannot have an array with duplicate keys.
A better implementation would be to have an array called $messageIDs and save the actual values in the array:
$messageIDs = array (1, 5, 8);
As i mentioned in comments, you cannot have duplicate keys in an array. Also your sample with curly braces is not valid php syntax.
Perhaps you need a multidimentional array:
$arr = [['messageID'=>1],['messageID'=>5],['messageID'=>8]];
in which case you would add another value like so:
$arr[] = ['messageID'=>11];
I want to store the parents keys of an array so I can access it later.
Something like:
$arr['hello'][0]['world'] = 'a';
$arr['hello'][1]['world'] = 'b';
And store both hello, 0 and world as some kind of variable so I can access the array with it:
For example, something I would think it may work is:
$indexes = array('hello', 0, 'world');
$arr[$indexes]
But this doesn't work, as an array is an illegal offset type for another array. So is there a way to access an array by an array of parents keys (variable)?
i think you want
echo $array[{$one}][{$two}];
So do you want to access a child array by a custom key that occurs in a parent array?
$parent_array[$custom_key] = array('hello',0,'world');
i have an array which could look like this:
$config[$name][$array]['key'] = 'value'; // here it's an array
or
$config[$name][$array] = 'value'; // here it's not an array
i want to check if the second array key ('array') is an array or not.
could someone help me?
use the function is_array http://php.net/manual/en/function.is-array.php
if(is_array($config[$name][$array])) echo "Yup I'm an array";
PHP has a built in function, is_array. That should let you know whether or not you have an array, or a plain string.