Adding an array into an array of arrays - php

Ideally, I'd like the ability to add a 3rd array into an array of 2 arrays. I've tried array_push, array_merge, and array_merge_recursive. Here is the relevant code:
$array1 = array("color" => "red", "shape" => "triangle");
$array2 = array("color" => "green", "shape" => "trapezoid");
$array3 = array("color" => "blue", "shape" => "square");
$result = array($array1, $array2);
$result = array_merge($result, $array3);
print_r($result);
This current code returns: Array ( [0] => Array ( [color] => red [shape] => triangle ) [1] => Array ( [color] => green [shape] => trapezoid ) [color] => blue [shape] => square )
The problem with it is I need it to number the 3rd array as well. So, [0], [1], and [2]

You're merging an array of strings ($array3) with an array of arrays ($result).
To achieve the result you want, you should either do
$result = array($array1, $array2, $array3);
or use array_push() instead of array_merge()
$result = array($array1, $array2);
array_push($result, $array3);

$array1 = array("color" => "red", "shape" => "triangle");
$array2 = array("color" => "green", "shape" => "trapezoid");
$array3 = array("color" => "blue", "shape" => "square");
$result = array($array1, $array2);
array_push($result, $array3);
array_push is the way to go because you will add the new array to the array of arrays. The issue with array_merge is that it takes the contents of $array3 (not the array itself) and adds them to $result.
When you said that you previously tried array_push I'm guessing that you used it incorrectly like this: $result = array_push($result, $array3); which will overwrite the result you're looking for with the length of the created array, rather than the array you're creating.

Related

Put a named key before numeric keys in array

Need to compare two arrays
Working example
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
Array1 Output:
Array ( [a] => green [0] => red [1] => blue )
When I do Like this
$array1 = array();
while($fetch = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
{
$array1[] = $fetch['color'];
}
I get this output:
Array ([0] => gren [1] => red [2] blue
How do I add the "a" to the array and make the first color be number zero?
This adding the "a" but it gets the zero number
array_unshift($array1,"a");
LIKE
Array ( [0] => a [1] => green
I want this
Array ( [a] => green [0]
I'm not sure why you want to do this, but here's how:
$array1 = array();
while ($fetch = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
if (empty($array1)) {
$array1['a'] = $fetch['color'];
} else {
$array1[] = $fetch['color'];
}
}
$arr = array(0 => 'green', 1 => 'red', 2 => 'blue');
$res = array_merge(array('a' => current($arr)), array_slice($arr, 1));
You can do with array_merge and array_shift function:
$arr = array(0 => 'green', 1 => 'red', 2 => 'blue');
$new = array_merge(array('a' => array_shift($arr)), $arr);
Demo: http://codepad.org/osifrZKZ

How to add multiple arrays to a subarray?

I have the following three arrays of objects:
First Array: $array1
Array (
[0] => Array (
[id] => 1
[name] => world
)
)
Second Array: $array2
Array (
[count] => 1
)
Third Array: $array3
Array (
[KM] => 2
)
I want to add the associative elements from $array2 and $array3 into the subarray at $array1[0].
Desired output:
Array (
[0] => Array
(
[id] => 1
[name] => world
[count] => 1
[KM] => 2
)
)
This is how you would use array_merge()
<?php
$array1 = array(array("id" => 1, "name" => "world"));
$array2 = array("KM" => 2);
$array3 = array("count" => 1);
print_r(array(array_merge($array1[0], $array2, $array3)));
?>
Which would output:
Array
(
[0] => Array
(
[id] => 1
[name] => world
[KM] => 2
[count] => 1
)
)
array_merge is the function you've been looking for
Sample code:
$output_array[0] = array_merge($array1[0], $array2, $array3));
print_r($output_array);
This will get you the desired output:
<?php
$array1 = array(array("id" => 1, "name" => "world"));
$array2 = array("KM" => 2);
$array3 = array("count" => 1);
$array1[0] = array_merge($array1[0], $array2, $array3);
print_r($array1);
?>
array_merge() will merge arrays. Problem is, the structure of your $array1 differs from the others. You have an array inside an array. To produce the desired output as specified you would need this:
$array4 = array(
0 => array_merge($array1[0], $array2, $array3)
);
But I believe you want something more like this:
$array4 = array_merge($array1[0], $array2, $array3);
array array_merge ( array $array1 [, array $... ] )
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.

How can I extract only the indexes of the array_diff function?

How can I extract only the indexes of the array_diff function?
$array1 = array("a" => "green", "red", "blue", "red", "pink");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);
Instead of it showing: Array ( [1] => blue [3] => pink )
I want it to display only the indexes like this: 1, 3 (maybe inside a new array called $indexesresult)
(Reason being that I am comparing an online (mysqli) array with a localhost (mysqli) array and I have to remove whitepaces before I can compare the arrays- I tried hundreds of ways around this but to no avail: array_diff does not like any type of whitespaces). With the indexesresult I can then get the original values back into the arrays to display the differences in a neat tabular format.
The array_keys would help.
$array1 = array("a" => "green", "red", "blue", "red", "pink");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
$indexesresult=array_keys($result); //<----- Here
print_r($indexesresult);
OUTPUT :
Array
(
[0] => 1
[1] => 3
)
Just try with:
print_r( array_keys($result) );

How to remove elements of an array that match elements in another array

I'd like to write a function that finds matches of all elements within a one-dimensional non-associative array and removes those elements completely from another one-dimensional non-associative array, including the index. Here's an example below.
<?php
function magicfunc($colors, $remove) {
// some magic here
}
EXAMPLE:
$colors = array(
'red',
'green',
'blue',
'purple',
'green',
'yellow',
'pink',
'orange'
);
$remove = array(
'green',
'white',
'pink'
);
magicfunc($colors, $remove);
WOULD RETURN:
Array
(
[0] => red
[1] => blue
[2] => purple
[3] => yellow
[4] => orange
)
How can I achieve this? Notice how there may be elements that are matched more than once (green), and it's also possible that there are no elements that match a particular one to be removed (white). The function should not have issues with these contingencies.
Try array_diff : http://us3.php.net/array_diff
<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);
?>
Result:
Array
(
[1] => blue
)

Merging multiple PHP arrays

My problem is this: I'm creating an Array in order to store these 2 types of 'refinement'. However, what is happening is as the information is collected from the database, each 'refinement' is assigned to it's own specific entry when the arrays are created within the while loop.
while($row = mysqli_fetch_array($result2, MYSQLI_ASSOC)){
etc...
}
So for instance, the 1st array would be a reference to 'Die Hard' and the 2nd, 'Breaking Bad' and the 3rd, 'Greys Anatomy'. What i'm trying to achieve is to merge them into 1 single array.
Array
(
[genreType] => Action
[mediaType] => Film
)
Array
(
[genreType] => Action
[mediaType] => TV
)
Array
(
[genreType] => Drama
[mediaType] => TV
)
Thanks for any help.
Try looking at this, http://php.net/manual/en/function.array-merge.php
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
OUTPUT
Array (
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
Why can't you just use array_merge? from the php docs http://php.net/manual/en/function.array-merge.php
while{$row...
{
$movies[] = $row;
//OR
$tmp['genreType'] = $row['genre'];
//and the like.....
$movies[] = $tmp;
}
Resulting in
$movies = array(
array(
"title"=>"Die Hard",
"genreType"=>"Action",
"mediaType"=>"Film"
),
array(
"title"=>"some other movie",
"genreType"=>"Comedy",
"mediaType"=>"TV"
)
);
like this?

Categories