associative array_search not working as expected - php

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

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

combain two arrays in php array_combine not working?

First string:
$a = '_edit_last,_edit_lock,wpvp_fp_code,video_category';
second string:
$b = '1,1464965316:1,{"src":"http://localhost/wbg/wp-content/uploads/2016/05/PHP-Tutorial-1-Introduction-PHP-For-Beginners.mp4","splash":"http://localhost/wbg/wp-content/uploads/2016/05/default_image.jpg","width":"640","height":"360"},free 200';
Convert String into combined array.
I need out put for:
array("_edit_last"=>" 1", "_edit_lock"=>"1464965316:1", "wpvp_fp_code"=>"{"src":"http://localhost/wbg/wp-content/uploads/2016/05/PHP-Tutorial-1-Introduction-PHP-For-Beginners.mp4","splash":"http://localhost/wbg/wp-content/uploads/2016/05/default_image.jpg","width":"640","height":"360"}","video_category"=>"free 200");
Use array merges recursive function to merge two arrays
For example
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
print_r($result);
Array Combine Function:
$a = array('_edit_last', '_edit_lock', 'wpvp_fp_code', 'video_category');
$b = array('1', '1464965316:1', '"{src":"http://localhost/wbg/wp-content/uploads/2016/05/PHP-Tutorial-1-Introduction-PHP-For-Beginners.mp4","splash":"http://localhost/wbg/wp-content/uploads/2016/05/default_image.jpg","width":"640","height":"360"}','free 200');
$c = array_combine($a, $b);
echo "<pre>";
print_r($c);
WATCH DEMO
Convert sting into array
$str = '_edit_last,_edit_lock,wpvp_fp_code,video_category';
print_r (explode(", ",$str));
DEMO
I have used the double space instead of a comma(,) in string2. Because comma(,) is not a unique function
DEMO - 3

two arrays with the same value

I have two arrays.
I need to make a foreach where it loops the values that is the same in both arrays. The arrays are not in same order and one of the arrays have more values than the other array.
I could do this.
foreach($array1 as $items1)
{
foreach($array2 as $items2)
{
if($items1 == $items2)
Echo "Match!";
}
}
But that takes a lot of time to load
Edit
I dont get any matches.
Array 1
$array1 = array();
while($fetch = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
{
$array1[] = array("a" => $fetch['value']);
}
Array 2
$filename = "test.txt";
$fp = #fopen($filename, 'r');
if ($fp) {
$array2 = explode("\n", fread($fp, filesize($filename)));
}
CODE
array_unshift($array2,"b");
$result = array_intersect($array1, $array2);
print_r($result);
You can use array_intersect:
$matches = array_intersect($array1, $array2);
You can use PHP in built function array_intersect()
<?php
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
?>
Output
Array
(
[a] => green
[0] => red
)
Note : Returns an array containing all of the values in array1 whose values exist in all of the parameters.

Pushing multiple arrays to slice into one || 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.

PHP array_intersect case-insensitive and ignoring tildes

Is there any function similar to "array_intersect" but it is in mode case-insensitive and ignoring tildes?
The array_intersect PHP function compares array elements with === so I do not get the expected result.
For example, I want this code :
$array1 = array("a" => "gréen", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
Outputs gréen and red. In default array_intersect function just red is proposed (normal cause ===).
Any solution ?
Thank you in advance
$result = array_intersect(array_map('strtolower', $array1), array_map('strtolower', $array2));
<?php
function to_lower_and_without_tildes($str,$encoding="UTF-8") {
$str = preg_replace('/&([^;])[^;]*;/',"$1",htmlentities(mb_strtolower($str,$encoding),null,$encoding));
return $str;
}
function compare_function($a,$b) {
return strcmp(to_lower_and_without_tildes($a), to_lower_and_without_tildes($b));
}
$array1 = array("a" => "gréen", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_uintersect($array1, $array2,"compare_function");
print_r($result);
output:
Array
(
[a] => gréen
[0] => red
)

Categories