This question already has answers here:
Selecting a random element from a PHP associative array
(4 answers)
Closed 8 years ago.
I need to select a random item (img1, img2, etc.) from simple nested arrays. I'm sure this is easy but I am stumped. The array has this format:
Array
(
[0] => Array
(
[homepage_image] => img1
)
[1] => Array
(
[homepage_image] => img2
)
)
$fields is the name of the main array.
I've tried using:
$random = array_rand($fields);
But of course that just gives me 0 or 1. How do I randomly get img1, img2, etc?
Use array_rand() to find a random key of your array:
$image = $fields[array_rand($fields)]['homepage_image'];
Related
This question already has answers here:
How can I add all of my array values together in PHP?
(4 answers)
Closed 3 years ago.
I have an array and I want to sum up the values of the array such as [0][1][2] and assign the result of the sum to a variable
My array
Array ( [0] => 1 [1] => 2 [2] => 0 )
Can anyone provide me a solution that would be really helpful?
Use php array_sum() function.
$val = array_sum($yourArray);
It will sum up all of your array elements into a variable.
This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 5 years ago.
I've the following array:
Array
(
[0] => Array
(
[ID] => 1
[more_user_data] => More
)
[1] => Array
(
[ID] => 2
[more_user_data] => More
)
)
Now I want to have a comma separated list of the IDs to use them in an own array. To get something like this:
array(1,2)
How could I only extract the IDs from the second array?
Use array_column() function like:
$arr = array_column($array, 'ID');
Working Example
This question already has answers here:
Merge two arrays containing objects and remove duplicate values
(7 answers)
Closed 6 months ago.
the below are the array am getting out i want to mearge array and remove duplicats of it and pass too for loop not able too do i just want is remove duplicate and pass too for loop as a unique value
i tried with
print_r(array_merge($uniq_arr));
for($i = 0; $i < count($uniq_arr); $i++) {
$tag = $uniq_arr[$i];
}
Array
(
[0] => diet
[1] => exercise
)
Array
(
[0] => diet
[1] => exercise
)
Array
(
[0] => diet
[1] => exercise
)
Array
(
[0] => water intake
[1] => hygiene
[2] => diet
)
Out put in tag shold be like each unique value
You could just use array_merge() indiscriminately then use array_unique() to remove any duplicate entries.
Here is how you could implement the array_merge, array_unique solution. I am making an assumption that you have 4 arrays like so:
$tag1Array = array('diet','exercise');
$tag2Array = array('diet','exercise');
$tag3Array = array('diet','exercise');
$tag4Array = array('water intake','diet','exercise');
$commontags = array_unique(array_merge($tag1Array,$tag2Array,$tag3Array,$tag4Array));
This should result in a single array of the unique values present in any of the 4 tag arrays.
This question already has answers here:
How to combine two arrays together?
(3 answers)
Closed 6 years ago.
I am having two arrays like this
Array
(
[0] => username
[1] => avatar
)
Array
(
[0] => name
[1] => 4.jpg
)
Here, I need to get these values in the following format
'username'=>name,'avatar'=>4.jpg
i.e., merge the same key values in the above format..
How should I do this,..Someone help me..
If you think that my title is wrong,Please change it into correct format.
Thanks,
Use array_combine()
$final_array = array_combine($fist_array,$second_array);
Reference:- http://php.net/manual/en/function.array-combine.php
use $c = array_combine($a, $b);
This question already has answers here:
PHP - Accessing Multidimensional Array Values
(4 answers)
Closed 8 years ago.
I've got this url:
http://web.com/script.php?identifiers%5Bmc%5D%5Bnick%5D=name1&identifiers%5Bcs%5D%5Bnick%5D=name2&identifiers%5Bcs%5D%5Bpassword%5D=mypass
so i will get array like this:
[identifiers] => Array
(
[mc] => Array
(
[nick] => name1
)
[cs] => Array
(
[nick] => name2
[password] => mypass
)
)
How do I take value name1 and put into variable $mc_name?
That's a simple array containing another array so you can simply specify multiple indexes for included array:
$mc_name = $_GET['identifiers']['mc']['nick'];
To better understand how it works think of it like assigning each array first to a variable like:
$identifiers = $_GET['identifiers'];
$mc_array = $identifiers['mc'];
$mc_name = $mc_array['nick'];
which will essentially do the same thing at once, without the need to specify multiple variables and arrays.
Start with:
identifiers = $_GET['identifiers']
If you know the key names, then simply:
$mc_name = $identifiers['mc']['nick']
If you know it's the first value or the first value, then you can:
$mc_name = array_shift($identifiers); // get the 'mc' array
$mc_name = array_shift($identifiers); // get the 'nick' value
Not that array_shift will actually remove the elements from the original array.