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');
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
When I put print_r($data); I get the following
Array
(
[name] => Cheese
)
Is there a way to get the key name in a variable on its own?
There may be occasions that name could be email and other values.
Use array_keys():
var_dump(array_keys($data));
Return all the keys or a subset of the keys of an array
Do you mean you know the value but you don't know the key? If so you could write something like this:
$array = ['name' => 'Cheese'];
array_flip($array);
var_export($array['Cheese']); // Output: name
You can have the array key extracted to their own variables using the extract function. For example
$a = array("color"=>"blue");
extract($a);
echo $color;
Can someone explain to me by the most easiest way possible difference between following? After reading about PHP arrays, i still dont get it.
print $myArray[0]->token
and
print $myArray[0]["token"]
Edit:
Question is not about best approach, but about meaning of that. Answer can be fond here, but it is not direct answer to my question
An example of all this :
<?php
//Creating simple object
$bookObject = new stdClass;
$bookObject->title = "Harry Potter and the Prisoner of Azkaban";
//Adding object to books array
$books = array($bookObject);//this array is equivalent to your $myArray
//Acessing object using -> operator
echo $books[0]->title;
//Re-initializng books array
$books = array(0=>array("title" => "Harry Potter and the Prisoner of Azkaban"));
//Accessing elements of an array by key
echo $books[0]['title'];
?>
$myArray is an array having an object with the property (attribute, variable) named token as its first element.
$myArray is an array having an associative array with the key named token as its first element.
So, it's about two different data structures that the array is holding as the first element indexed by 0.
An object inside an array having token element
An array inside an array having token element
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 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';