I have an array (array01) that contains a bunch of sub arrays consisting of two pieces of data each, like:
Array ( [0] => Array ( [0] => 10CC [1] => Dreadlock Holiday )
[1] => Array ( [0] => 10CC [1] => I\'m Not In Love )
[2] => Array ( [0] => 10CC [1] => Dreadlock Holiday ) )
etc...
I have another array (array02) like:
Array ( [66] => Array ( [0] => 10CC [1] => Dreadlock Holiday )
[585] => Array ( [0] => 10CC [1] => I\'m Not In Love )
etc...
I'm successfully using foreach and then in_array to see what array01 elements are in array02. However, what I am struggling to figure out is how to get the id of what element in array2 the hit was on.
So for example, array01's 0 and 2 elements (both 10CC, Dreadlock Holiday), are matched in array02, but how do I get the ID of the element (in this case, 66)?
Thanks for your help.
Have you tried the array_search function?
look for documentation for the array_intersect() in PHP manual. Note that the original keys are preserved so it returns an array with the elements present on the array02 array with the original keys.
Hope it's what you're looking for
Cheers
Related
I'm writing a code for looping out data from an multi dimensional array.
While looping I got confused while getting the details from an array. I have tried several ways for getting but in vain.
Now what I want is to get the values from the key 4 provided in the array.
Array
(
[match1] => Array
(
[4] => Array
(
[0] => Array
(
[0] => Sanjay
[1] => Delhi
[2] => 23
)
[1] => Array
(
[0] => Ram
[1] => Mumbai
[2] => 26
)
)
[5] => Array
(
[0] => Array
(
[0] => Sanjay
[1] => Delhi
[2] => 23
)
[1] => Array
(
[0] => Ram
[1] => Mumbai
[2] => 26
)
)
)
)
Thanks
In order to access the multi dimensional array you need to access via foreach() or directly by using the keys that you have in the print_r() function.
Hence as per your Example you can directly access the variable the you need using the
first array variable along with the key that the first array has.
Consider this array and you need to fetch the first value you can process like this.
print_r($var); resulting in this
Array
(
[match1] => Array
(
[4] => Array
(
[0] => Array
(
[0] => Sanjay
[1] => Delhi
[2] => 23
)
[1] => Array
(
[0] => Ram
[1] => Mumbai
[2] => 26
)
)
)
)
You can retrieve the variable in two methods as follows
Method One:
As the variable that contains the array is $var hence you need to access like this.
In order to fetch the value that the key has you can have $var['match1'][4] and you can apply foreach over to the variable and get the value that it has.
foreach($var['match1'][4] as $inner_value)
{
// Do what ever stuff you need
}
Method Two:
Getting key value 0 - 0 in the array that it has you can code as - $var['match1'][4][0]
Getting key value 1 - 1 in the array that it has you can code as - $var['match1'][4][1]
You can get as much value inside the array as you can with the help of the above two points
Output for both it will be the same as follows
Sanjay Delhi 23
Ram Mumbai 26
It's really very simple. Let's assume the name of your main array is $mainarray. So here is how you can get the array of key 4.
$key4array=$mainarray['match1'][4];
foreach($key4array as $arrayele) {
echo $arrayele[0]." ".$arrayele[1]." ".$arrayele[2]."<br>";
}
Output will be,
Sanjay Delhi 23
Ram Mumbai 26
Access the first level array by match1 key and then 4 as index to get the second level array.
I sorted an array in PHP and got this as a result: (the numbers represent how often the word was found in that array)
Array
(
[photo] => 9
[photography] => 6
[art] => 3
[mystery] => 1
)
Now I would like to turn that sorted array into a "regular" array like this:
Array
(
[0] => [photo]
[1] => [photography]
[2] => [art]
[3] => [mystery]
)
How to do that?
If your first array is the variable $sortedArray, then you could do the following to get exactly what you want:
$regularArray = array_keys($sortedArray);
See the array_keys php documentation for more information.
Dear stackoverflow members,
I want to join 2 arrays in random as mentioned below, i tried different methods but i was not able to produce the result that i exactly wanted.
I have 2 arrays which looks like this. Lets call this names.
Array
(
[bill] => Array
(
)
[steve] => Array
(
)
[Ritchie] => Array
(
)
)
now these names are generated from another function, it's output looks something like this.
Array
(
[1] => Array
(
[email] => info#bill.com
[name] => bill
[web] => http://bill.com
)
[2] => Array
(
[email] => rich#steve.com
[name] => steve
[web] => http://steve.com
)
[3] => Array
(
[email] => god#linux.com
[name] => Ritchie
[web] => http://linux.com
)
[4] => Array
(
[email] => dummy#dummy.com
[name] => Ritchie
[web] => http://linux.com
)
)
and the 2nd array, lets call it countries.
Array
(
[0] => USA
[1] => UK
[2] => Netherlands
[3] => Australia
[4] => Germany
)
Now here is the exact problem. i want the 1st array names and the 2nd array countries to be joined and form another associative array in random order. But please note that the function which returns the name array returns the key : Ritchie twice, it has 2 occurrences. So the final array should be something like this.
Array
(
[Ritchie] => Array
(
[0] => USA,
[1] => Germany
)
[bill] => Array
(
[0] => Netherlands
)
[steve] => Array
(
[0] => UK
)
)
Since the key Ritchie has 2 occurrences, 2 unique values from the country array should be added. The number of keys or occurrences in names and the keys in countries will always be the same. If their is anything unclear, let me know i'll clear it out.
I did heavy research on the internet and stackoverflow and i was only able to come up with this. The current solution i have is this. Please be kind to help me to improve the current lines of code to suit my need or may be this might not be elegant, if so, please be kind to suggest a better solution.
$jumble = array();
foreach ($names as $name) {
$jumble[$name] = $countries[array_rand($countries)];
}
Thank you.
Try this code:
shuffle($countries);
$n = count($countries); $i = 0;
$jumble = array();
foreach ($names as $name) {
if (!isset($jumble[$name])) $jumble[$name] = array();
$jumble[$name][] = $countries[$i++ % $n];
}
Demo
I have an array as such
Array
(
[1] => Array
(
[0] => PrettyName1
[1] => PrettyName2
[2] => PrettyName3
)
[2] => Array
(
[0] => UglyURL1
[1] => UglyURL2
[2] => UglyURL3
)
)
I want to be able to put these into an array for a code-igniter template, well when I push and merge arrays I end up messing the whole thing up. Can someone show me the proper way to merge these arrays? I need something like
Array
(
PrettyName1 => UglyURL1
PrettyName2 => UglyURL2
PrettyName3 => UglyURL3
)
Maybe something like this array_combine($ar[1], $ar[2]) ?
I want to sort two dimensional array according to the occurrence of the search string in php. i tried to fetch data according to the relevance of search string . i come across match and against but it will work on the MYISAM but my table is in innodb. so plan to sort the array using any sort function but i cant find out anything.
my search pressure tes string array
Array
(
[0] => pressure
[1] => tes
)
My output array which matches the above string with title is
Array
(
[0] => Array
(
[title] => tests.doc
[link] => http://localhost/test.doc
[snippet] =>
)
[1] => Array
(
[title] => Pressure Testing Your Company
[link] => http://localhost/Pressure_Testing_Your_Companys.pdf
[snippet] => Questions used by the CFO against dimensions critical to success
)
[2] => Array
(
[title] => pressure.doc
[link] => http://localhost/pressure.doc
[snippet] => Templates for services
)
)
In the above array the most relevant array[1] then array[2] and then array[0] should be in this order. i want to sort this array accordingly. my output should be like below:
Array
(
[0] => Array
(
[title] => Pressure Testing Your Company
[link] => http://localhost/Pressure_Testing_Your_Companys.pdf
[snippet] => Questions used by the CFO against dimensions critical to success
)
[1] => Array
(
[title] => pressure.doc
[link] => http://localhost/pressure.doc
[snippet] => Templates for services
)
[2] => Array
(
[title] => tests.doc
[link] => http://localhost/test.doc
[snippet] =>
)
)
please help me!!!!
Look at the example #3, I think that's what you want.
http://php.net/manual/en/function.array-multisort.php
Just tested this, it seems to be working :
$titles = array();
foreach ( $array as $key => $value )
{
$titles[ $key ] = $value['title'];
}
array_multisort( $titles, SORT_ASC , $array );
I would highly recommend to sort your results in your MySQL query, that would be much better performance wise.