Edit (Some background):
I am dealing with some data for states/regions/etc within a country. The data looks like so (essentially translated to english and its original form in the local language):
Array
(
['Anhui'] => "安徽省",
...etc
)
Now that's all fine and well if I am displaying the data to someone from China. However, if the visitor is from another country, it is clearly better to display the english name (Anhui). This is essentially the point of this exercise.
The data is then used to generate a drop down to set the value and options.
I have an array that looks like so:
Array
(
[0] => "item1"
[1] => "item2"
[2] => "item3"
)
I would like to turn this into an associate array:
Array
(
["item1"] => "item1"
["item2"] => "item2"
["item3"] => "item3"
)
Are there any ways to do this besides using a foreach loop?
Just use the same variable for both parameters of array_combine():
$arr = array_combine($arr, $arr);
However, I don't really see the point of this. Perhaps you could explain what you are trying to accomplish, so you could find the best solution.
You can use some php array functions
I think this would work, even though there are other ways.
$new_array = array_combine(array_values($old_array),$old_array);
Related
I have a "simple" question.
I am currently working on a program to search and filter through multiple multidimensional arrays. Now I printed an array and it showed me an output similar to this.
Array(
[host_/_service] => Array ([0] => object)
[host_/_service] => Array ([0] => object [1] => Server [2] => localhost )
[host_/_service] => Array ([0] => object [1] => Server [2] => localhost )
and so on, and so forth. Now I have discussed this with a colleague of mine and he says that in this array, there is in fact only one element to be selected since the other arrays are not separated by commas.
Unfortunately my colleague was not able to explain to me why this is the way it is and so I though some of you fellas my know more. I have no problem whatsoever, just curios to know more about this topic and if it is actually true.
P.S
I am really a novice when it comes to programming, so my understanding is quite limited.
P.P.S
Here is the code that actually outputs this results, just after it went through the loop.
$my_array = array();
foreach ($a->getSomething() as $key => $value {
$my_array[$value['host'].’_/_’.$value['service']][] = $value['hs'];
}
print_r($my_array);
I want to check, if array A contains all the items from array B (may contain others, but must contain all), when both arrays are multidimensional, i.e. can contains different variable types.
I've seen a lot (particularly this, this, this, this, this and this, also this, this and this as well). I've read PHP doc. Everything, that I checked, fails with "Array to string conversion" notice. Especially wen using array_intersect() or array_diff().
I'm using strict error checking, so notices actually holds further execution of entire script and are something, I don't generally like and want to avoid. Is it possible in this case?
My array A is:
Array
(
[0] => content/manage/index
[Content] => Array
(
[title] =>
[type] => 5
[category] =>
[recommended] =>
[featured] =>
[status] =>
[views] =>
[last_access_date] =>
[creation_date] =>
[modification_date] =>
[availability_date] =>
[author_id] =>
)
)
My array B is:
Array
(
[0] => /content/manage/index
[Content] => Array
(
[type] => 1
)
)
So, is there any way I can if I can use array_intersect on multidimensional arrays containing different variable types without getting notice?
My problem (and question) came out of misunderstanding, what "Array to string conversion" notice really means. In my case, it was trying to tell me, that I'm trying to walk multidimensional array with functions designed to be used on single dimension array.
Understanding that led me to a solution within few seconds. There are many of them here, on SO, but the one given by deceze here looked the best for me. So I adopted it into the form of such function:
function recursiveArrayIntersect($array1, $array2)
{
$array1 = array_intersect_key($array1, $array2);
foreach($array1 as $key=>&$value)
{
if(is_array($value)) $value = recursiveArrayIntersect($value, $array2[$key]);
}
return $array1;
}
I adopted it to my project and my way of coding, but all the credits still goes to deceze (his answer here)!
Now I can find an intersection of virtually any array, no matter what kind of variable types it contain and no matter of, how deep it is (how many subarrays it contains).
I am trying to convert indexed array to normal array. Basically what get is:
Array ( [0] => 14 [1] => 19 [2] => 20 )
And I need is:
Array(14,19,20);
I tried over Google but not information found. I think this kind of function isn't available in PHP, is there any? please let me know!
Thanks,
Asif
You're chasing shadows:
Both of the arrays you've shown are equal.
There is no such thing as an unindexed array in PHP.
But if you really want to be sure, use $newArray = array_values($array)
I'm using cakephp and am getting back a "double array" where it is giving me 2 arrays where it should be 1, I have looked into the issue as far as cakephp and can't figure it out and just want to move past this for now so I am wondering if anyone knows how to unset a second array if a variable has 2 arrays.. below is the print_r of the array, its just one variable that has this, which I find odd.. so I want to make it so there is not a 2nd set of duplicate values, if I do an array_push it pushes both values for that index into the resulting new array index so that won't work
one variable is equal to the following:
Array ( [0] => 42 [1] => 62 ) Array ( [0] => 42 [1] => 62 )
EDIT:
This is not an issue of my printing out the array twice accidentally, as I said above, with a foreach array_push of the variable, i end up with this, which is odd:
Array ( [0] => 4242 [1] => 6262 )
EDIT:
This is the cakephp database call that I am using, I know I didn't ask this in regards to cakephp but since some people think this is impossible i am posting this just so you can see what it does if you want
$specificfields_array = $this->Mymodel->find('list', array('fields' =>'Mymodel.id'),
'conditions' => array('emailgroup' => $categorynumber, 'sent' => '0');));
EDIT:
This is what a "foreach" array_push is:
$mynewarray = array();
foreach ($specificfields as $specificfields_current) {
array_push ($mynewarray, $specificfields_current);
}
A variable cannot "have two arrays". It can be one array that has two arrays nested. The scenario you describe is impossible (probably there are two print_r there or there is a < character hiding stuff – check the HTML source).
Can you post the controller, the model and the view file with your print_r calls to the http://bin.cakephp.org/ site and post the links back here so we can see all of your code?
I want to change the format of this array to match another one. One was pulled from a database, and the other was made using space delimted user input and then explode();
So here are the arrays I want to change, this is from the database (mysql_fetch_array). It grabs the single field from all rows.
Array
(
[name] => daniel
)
Array
(
[name] => alex
)
Array
(
[name] => richard
)
And here is what I want it to look like. This was the output of the user submitted values, space delimted and using PHPs explode() function.
Array
(
[0] => daniel
[1] => alex
[2] => david
)
What I want to be able to do is have these in the same format so that I can compare the two. The end goal is to be able to compare the two arrays (user input and database results), and create a final array containing only values that the user has inputted that the database doesn't already contain.
So using the data above this would be my final array:
Array
(
[0] => david
)
I would really appreciate help with the first bit, and if anyone else has a better way to achieve the end goal that would be a great extra bonus! (I get the feeling it might be easier to do this with SQL queries but I'm really confusing myself with these arrays)
Thanks!
array_diff($user_array, $database_array);
You can construct the $database_array like this:
$database_array = array();
//assuming each db record has only one value
//while there are still results in the db, do:
$database_array[] = reset($fetched_record);
See array_diff.