This question already has answers here:
How to access and manipulate multi-dimensional array by key names / path?
(10 answers)
Convert delimited string into array key path and assign value
(5 answers)
Closed 7 years ago.
How do I turn this:
$keys=array(1,8,27);
Into this?
$array=array();
$array[1][8][27]='Hooray!';
I have an array that I need as the keys of a multidimensional array.
In the example:
I am using 1, 8 and 27.
I think this would work:
$array[$keys[0]][$keys[1]][$keys[2]] = "Hooray!";
Can't even begin to imagine how to go about making it dynamic though.
Related
This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
php - deep search array of arrays and return only matching elements
(5 answers)
Closed 5 years ago.
I find this quite hard to explain as you can see in the title but let's say this is how my object looks like,
{
"success":true,
"objects":[
{
"name":"Stick",
"value":"wood",
"size":"large"
}...
]
}
Now I'm trying to get all the data where the objects name is Stick, so basically if it's "name" is stick it should return, name, value and size.
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
I have multidimensional array string stored in PHP variable and I want to extract the keys and its values. Below is the given string which need to be split.
[{"cutoffTime":"1","refundInPercentage":"50"},
{"cutoffTime":"3","refundInPercentage":"70"},
{"cutoffTime":"6","refundInPercentage":"90"}
]
How can I extract this?
Try this to have your work done
json_decode($string)
This question already has answers here:
Get first key in a (possibly) associative array?
(25 answers)
Closed 7 years ago.
How to get first element of array in php using key and how to find first key of array?
You can try this -
$key = array_shift(array_keys($yourArray));
echo $yourArray[$key];
This question already has answers here:
How to Sort a Multi-dimensional Array by Value
(16 answers)
Closed 9 years ago.
hey guys i have a bit of a chicken or egg problem. I query an ldap database for a list of users and recieve a 2-dimensional array in the form
Array[index]['username']
The ldap database is currently not sorted alphabetically, or at least records are not being retrieved in an alphabetic order, so using a for loop on the [index] returns an unsorted looking list.
How can i sort my Array alphabetically by the values in 'username'?
is this even possible(easily)? :O
There's loads of duplicates for that... but here you go:
usort($array, function($a,$b){ return strcmp($a['username'], $b['username']); } );
This question already has answers here:
Want to got the new array that not in array A?
(4 answers)
Closed 8 years ago.
If I have 2 PHP array's that contain names. The first array contains over 100,000 keys
The second array is only 10,000 or less keys
I need to filter out any names from my 10,000 key array if the key exist in the master 100k array
So I will end up with a 3rd array that will contain the 10,000 key array minus any items that existed in the main array.
This is kind of confusing for me to explain so I hope I make sense here. If you can help I would appreciate it.
A sample array might look like this (both arrays will be in same format)
array(
'Coders4africa',
'uiri00',
'phileverson',
'nileshgr',
'MSVenom',
'dshafik',
'rafavilar'
)
array_diff($array1, $array2)
From the docs: returns an array containing all the entries from array1 that are not present in any of the other arrays.