Referencing part of an array and appending information to that index - php

Is it possible to add one array to the end of a specific index of another array?
I want to search each index for a specific value (in this case "apple").
Then I want to add a line of data after an index that has "apple" as its first value.
First array:
Array
(
[0] => apple,1,2,3
[1] => orange,a,b,c
)
Second array:
Array
(
[0] => apple,a,b,c
)
Final product:
Array
(
[0] => apple,1,2,3, apple,a,b,c
[1] => orange,a,b,c
)
Notice that I am ignoring the "orange" index because it did not match "apple".
I have tried "pushing" or "merging" the arrays but that does not seem to work.
Part of the problem is that I:
Do not know how to search an array for "Apple" if that word is part of a larger set of information within the array (e.g., apple, 1,2,3)
I do not know how to reference only part of an index (e.g., find part of the array that has "apple", and then add some information to that specific index).
Is this even possible to do? Excited to learn more about this!

Related

Target array element that has a blank key/name

I have an array being generated by data which I have no control over and it returns the below array. Is there a way to target specifically the element that has a blank key/name?
I've tried $array[''], $array[' '] and $array[0] but none of them seem to work. Is there another way without looping through the array, the empty key/name can be anywhere in the array, it's not necessarily the last array element?
var_dump(array_keys($array)); gives me [0]=> string(2) " " for the element in question, I've tried targeting the element with a single and double space as the key/name but still gives an undefined index error.
I've also run serialize()on the keys from the array and the element with the blank key/name is returning s:2:" ";.
Array
Array
(
[desc] => Information etc.
[ref] => 2
[ ] => 123
)
It i probably not '' but something that shows up as '' (null maybe ?). Try var_dump(array_keys($array)) to get actual key values and types. this should show you the proper key to use

Check, if array A contains all items from array B, when both arrays are multidimensional

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

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?

Turn X arrays into a single array

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.

Categories