Pushing multiple arrays to slice into one || PHP - php

So I have something like:
$array = array("red", "yellow", "hello", "world");
$array2 = array("1", "2", "3", "4");
$myArray = array();
array_push($myArray, $array, $array2 );
$myArray = array_slice($myArray, 0, 2);
and I want $myArray to be ["red", "yellow"], and if $array was empty $myArray would be ["1", "2"]
Does that make any sense? Right now array_slice is counting the arrays being pushed into $myArray, not the content inside them. How would I go about doing this?

Where you use array_push you probably want to use array_merge:
$array = array("red", "yellow", "hello", "world");
$array2 = array("1", "2", "3", "4");
$myArray = array_merge($array, $array2);
$myArray = array_slice($myArray, 0, 2);
Explanation:
array_push pushes the elements to the end of the existing array, so
$array = array('a');
array_push($array, 'b');
// results in $array = array('a', 'b');
Thus in your code, just after the array_push-call
$myArray = array(array('red', 'yellow', ...), array('1', '2', ...))
array_merge merges two or more arrays
$myArray = array_merge($array, $array2);
// results in $myArray = array('red', 'yellow', ..., '1', '2', ...)

Try using array_merge() instead of array_push()
$array = array("red", "yellow", "hello", "world");
$array2 = array("1", "2", "3", "4");
$myArray = array_merge($array, $array2 );
$myArray = array_slice($myArray, 0, 2);
array_push() is adding each array as an element of $myArray instead of combining them with it.

Related

Make one recursive combined array from other arrays

I want to go from these arrays:
$array1 = ["x", "y", "z"];
$array2 = ["a","b"];
$array3 = ["1","2","3","4","5","6"];
To this array:
$arrayResult =
array(
array("x" => array("a" => array(1,2,3,4,5,6),
"b" => array(1,2,3,4,5,6)),
"y" => array("a" => array(1,2,3,4,5,6),
"b" => array(1,2,3,4,5,6)),
"z" => array("a" => array(1,2,3,4,5,6),
"b" => array(1,2,3,4,5,6)))
);
I tried to make this combined array with cartesian product approaches, but no satisfying result so far.
Here is another solution without using any loop:
$array1 = ["x", "y", "z"];
$array2 = ["a","b"];
$array3 = ["1","2","3","4","5","6"];
$result = array_combine(
$array1,
array_fill(
0,
count($array1),
array_combine(
$array2,
array_fill(0, count($array2), $array3)
)
)
);
print_r($result);
Here is the demo
use array_fill_keys twice to get the result
$result = array_fill_keys(
$array1,
array_fill_keys($array2, $array3)
);
Demo on eval.in

associative array_search not working as expected

Hi I am trying to array_search for associative array in Php but the function isnot working as expected. can anyone help me with this. currently using php version 5.4
<?php
$mainArray = array("array1","array2","array3");
$array1 = array("item"=>"apple","price"=>2);
$array2 = array("item"=>"banana","price"=>3);
$array3 = array("item"=>"carrot","price"=>4);
echo phpversion();
echo "this line ".array_search("apple", array_column($mainArray,'item'));
echo "end";
?>
Problem: In your code $mainArray = array("array1","array2","array3"); you are declaring array of strings not array of arrays.
Change this:
$mainArray = array("array1","array2","array3");
This:
$mainArray = array($array1, $array2, $array3);
And use this $mainArray = array($array1, $array2, $array3) after definition of $array1, $array2, $array3
PHP code: Try this code snippet here
<?php
ini_set('display_errors', 1);
$array1 = array("item" => "apple", "price" => 2);
$array2 = array("item" => "banana", "price" => 3);
$array3 = array("item" => "carrot", "price" => 4);
//should be declared like this and should be after defintion of $array1, $array2, $array3
$mainArray = array($array1, $array2, $array3);
echo "this line " . array_search("apple", array_column($mainArray, 'item'));
?>

php update array values

I have two arrays with different length, but keys are similar.
My requirement is update $array1 with values of $array2 similar keys
$array1 = array("Jan"=>"0", "Feb"=>"0", "Mar"=>"0", "Apr"=>"0");
$array2 = array("Jan"=>"2", "Mar"=>"3");
Output:
$res = array("Jan"=>"2","Feb"=>"0","Mar"=>"3","Apr"=>"0");
You can achieve it by this code:
$array1 = array("Jan" => "0", "Feb" => "0", "Mar" => "0", "Apr" => "0");
$array2 = array("Jan" => "2", "Mar" => "3");
$array3 = array_replace($array1, $array2);
print_r($array3);
You can simply use + operator.
$array1 = array("Jan"=>"0","Feb"=>"0","Mar"=>"0","Apr"=>"0");
$array2 = array("Jan"=>"2", "Mar"=>"3");
print_r($array2 + $array1);
DEMO
Try this:
array_merge($array1, $array2);

I want to compare two arrays in PHP

Assume that I have two arrays as follow:
$array1 = array(1, 3, 5);
$array2 = array('x'=> 1, 'y'=> 2, 'z'=> 5);
How to check that the two arrays are exactly the same in most efficient and proper way and it doesn't care the keynames of the *$array2.
I want to make a function which should return true if values are exactly the same, and false if any of the ones are different both in value(s) and number of elements.
Thanks for your time and reading.
In the simplest case you can just use array_diff. It ignores the keys in your second array, but also the order of the values. It would return an empty set if the arrays are equal:
if (count(array_diff($array1, $array2)) == 0) {
// equal
You could also compare the arrays directly, after stripping keys from the second:
if ($array1 == array_values($array2)) {
That would additionally compare the order of contained values.
array_values($array1) === array_values($array2)
Assuming that arrays have same order.
Try this
$array1 = array(1, 3, 5);
$array2 = array('x'=> 1, 'y'=> 2, 'z'=> 5);
$array2 = array_values($array2);
echo $array1 == $array2 ? 'true' : 'false';
array_diff will do the job for you:
<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
if(empty($result)){
// arrays contain the same values!
}
?>
Create a class containing an array and make that class implement the Comparable interface, for example http://php.net/manual/language.oop5.interfaces.php#69467
like this:
<?php
$array1 = array ("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array ("a" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
if(count($result) == 0)
{
.......
}
?>

php array_unique with exceptions

I want to remove duplicate values in an array except 1 value.
Eg:
$array = array ("apple", "orange", "orange", "banana", "grapes","grapes", "apple");
How can I remove all duplicate values and keep all duplicate values that equal "apple"
$array = array ("apple", "orange", "banana", "grapes", "apple");
There are about 400 values
$seen = array()
foreach ($array as $value)
if ($value == 'apple' || !in_array($value, $seen))
$seen[] = $value;
$seen will now have only the unique values, plus the apple.
$numbers = array_count_values($array);
$array = array_unique($array);
$array = array_merge($array, array_fill(1, $numbers['apple'], 'apple'));
$array = array ("apple", "orange", "orange", "banana", "grapes","grapes", "apple");
$counts = array_count_values($array);
$new_array = array_fill(0, $counts['apple']-2, 'apple'); // -2 to handle there already being an apple from the array_unique count below.
$new_array = array_merge(array_unique($array), $new_array);

Categories