Turn X arrays into a single array - php

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.

Related

Show array as written with rand inside

I have an array such as:
$var = array('hi','ho',rand(2,5));
What I would like to echo is the entire array, exactly as written.
Normally when you try a print_r, it shows as:
Array (
[0] => hi
[1] => ho
[2] => 3
)
But I want:
Array (
[0] => hi
[1] => ho
[2] => rand(2,5)
)
You can get this with file_get_contents, but is there any way to do so within the actual PHP file?
I don't think it's possible because when array is created, random value is assigned to element with index 2 and you cannot check how this value was created.
I don't think it's possible, since the rand is already evaluated as soon as you set the array to some variable.
A workaround would be the hold the expression as a string and then eval it when you need it. Like this:
$varStr = "array('hi','ho',rand(2,5))";
echo $varStr;
// when you actually need it
$var = eval($varStr);
However, this is almost never a good idea. Providing a use-case where you need this might help come up with a better solution.

Converting indexed array to normal or simple array

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)

PHP comparing two (potentially differently-sized) 2D arrays

I'm trying to compare two 2D arrays in PHP and my head is hurting with all the recursion - this'll probably be obvious to someone who's used to it.
I have two 2D arrays which look like this, called $submittedArray2D and $dbArray:
{
[0] => {
[0] => 'name'
[1] => 'sectors'
[2] => 'address'
[3] => 'url'
}
[1] => {
[0] => 'name'
[1] => 'sectors'
[2] => 'address'
[3] => 'url'
}
...
}
$submittedArray2D comes from user input, while $dbArray holds what is already in the database.
I want to compare them to see if any of the values have been updated in $submittedArray2D, and save those changes to $dbArray accordingly.
I also want to pass any changed addresses to a separate function for geocoding, the results of which will be stored in another database field, where the keys reference the same entry in $dbArray (so $geocode[4] refers to $dbArray[4], for example).
I've tried out some foreach statements to go through the arrays sequentially, but this breaks down if one of the entries has been removed from the middle of the array, because the two arrays are no longer in sequence. I'm wondering whether some kind of ID for each array entry might make sense, but I'm not sure where to start.
Any ideas or pointers would be much appreciated!
I managed it in the end by storing a unique ID alongside each array element, that was it was possible to loop through and check whether that ID existed in the comparator array and then check whether the address had changed, and if so store the change (along with the ID) in a separate array for processing.

Create associative array from an array without keys

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);

CakePHP returning double array from find('list) query

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?

Categories