I have two arrays like this.
$array1=array(1,2,3,4,5,7);
$array2=array(1,2,3,4,5,6);
So, the output should bring the difference in both arrays.
The output should be.
1,2,3,4,5 -> These numbers exist in both arrays, so these should be ignored.
7 and 6 -> These numbers are the un-common in both arrays, so I need these values in array.
The output should be 7 & 6.
Help me out. I have tried array_diff and other array elements.
try this
array_merge(array_diff($array1,$array2),array_diff($array2,$array1))
foreach($array1 as $key => $value) {
if($value != $array2[$key]) {
echo "\$array1[" . $key . "] (" . $value . ") is different to \$array2[" . $key . "] (" . $array2[$key] . "<br />";
}
}
Related
I want to merge 2 arrays by concatenating their values. Right now I use array_merge(), but that doesn't give me the expected result.
Code:
$software1 = $_POST['software1'];
$software2 = $_POST['software2'];
$software3 = array_filter($software2);
$result = array_merge($software1, $software3);
foreach($result as $value){
echo $value . "<br>";
}
Current output:
software
software
software
1
2
3
What I would like:
software 1
software 2
software 3
Just another way to do it: if $software2 are unique value, you can use array_combine:
foreach (array_combine($_POST['software2'], $_POST['software1']) as $key => $val) {
echo $val , ' ', $key;
}
You want to use array_map() here and concatenate both corresponding values from both arrays together creating a new array, e.g.
$result = array_map(function(...$v){
return implode(" ", $v);
}, $software1, $software3 /* , more arrays */);
Of course if you just want to output it you can use echo inside array_map(). And if you have an unknown amount of arrays you want to merge by concatenation you can do it with call_user_func_array(), e.g.
$result = call_user_func_array("array_map", [function(...$v){return implode(" ", $v);}, $arrays]);
Several ways. Here are two:
foreach($software1 as $key => $value){
echo "$value {$software2[$key]}<br>";
}
Or if one or the other are guaranteed to be unique, use it as the key:
foreach(array_combine($software1, $software2) as $key => $value){
echo "$key $value<br>";
}
I'm using PHP 5 with mySQL:
$line = mysql_fetch_array($result, MYSQL_ASSOC);
When I do this, I can see the results of the query printed:
foreach ($line as $data)
{
echo ($data . ", ");
}
But if I do this instead:
echo ($line[0] . " " . $line[1] . " " . $line[2]);
I don't see anything. Also I can't assign a value from $line:
$values[] = $line[0]; // fails - doesn't assign anything
Why? And what should I be doing instead?
You've nominated to retrieve records as associative entries only (MYSQL_ASSOC). This means your $line array will not contain numeric indices.
If you only want numeric arrays, use mysql_fetch_row().
Associative only, mysql_fetch_assoc()
You can also retrieve a mixed numeric and associative array using mysql_fetch_array($result, MYSQL_BOTH) however this will give you duplicate entries in your foreach loop.
You told MySQL that you want to fetch the row as an array, which means $line is not a 0-index array, it's an associative array, which means the keys of the array are the column names of the table(s) you selected from.
var_dump($line); // Will display the keys
try:
echo ($line["id"] . " " . $line["id"] . " " . $line["id"]);
so instead of using a index number like [0] you use the corresponding column-name
I am doing my head in trying to do a simple retrieval for specific arrays within a script so I have an original associative array:
$vNArray ['Brandon'] = $item[3];
$vNArray['Smith']= $item[4];
$vNArray ['Johnson']= $item[5];
$vNArray ['Murphy']= $item[6];
$vNArray ['Lepsky']= $item[7];
Which outputs a common result for most values:
foreach ($vNArray as $key => $value){
if(!empty($value)){
$result .= "\t\t\t\t<li><strong>$key</strong>" .$value. "</li>\n";
}
But then I want two of these arrays to render differently so I added another script suggested by someone:
$display_id=array('Brandon', 'Murphy');
foreach ($vNArray as $key => $value){
if(!empty($value)){
//Looks into the display_id array and renders it differently
if (in_array($key, $display_id)) {
$result .= "\t\t\t\t<li id=\"$key\"><strong>$key</strong>$value</li>\n";
} else {
$result .= "\t\t\t\t<li><strong>$key</strong>$value</li>\n";
}
}
}
The problem is that i want the result for these arrays to contain both within the first result but when I tried to output
$result .= "\t\t\t\t$key[1]".$value[1]." \n";
PHP thinks that the index is the value's character index, so I'm having major syntax issues like id="/" r.
I have also tried
$result .= "\t\t\t\t<li id=\"". $display_id['Brandon']$value.\""><strong>$key[1]</strong>". $display_id['Murphy']$value." </li>\n";
But I am still getting wrong syntax issues...like
syntax error, unexpected T_VARIABLE
Or some other error like this.
Could someone please help?
EDITED
I have made the syntax corrections but I still need to specify the index:
The result from
result .= "\t\t\t\t<li id=\"". $display_id['Brandon'] . $value."\"><strong>" . $key[1] . "</strong>". $display_id['Murphy'] . $value." </li>\n";
Needs to be (Note how each value is on the same output depending on what I'm targeting):
<li id="Brandon Value"><strong>Brandon</strong> Murphy Value</li>
Right now it ignores the index value of . $display_id['Brandon'] . $value. or . $display_id['Murphy'] . $value." all together and just repeats:
<li id="Brandon Value"><strong>Brandon</strong> Brandon Value</li>
<li id="Murphy Value"><strong>Murphy</strong> Murphy Value</li>
Just do $key, forget the [1] bit. Same with $value.
Each value needs to be concatenated with another, so for example:
echo $a . $b . $c . $d . $e;
Notice the . contact that joins each variable with the the next / prev variable, so your line:
$display_id['Brandon']$value
should look like:
$display_id['Brandon'] . $value
^
I would do the following though.
$result .= sprintf('<li id="%s"><strong>%s</strong> %s</li>',$display_id['Brandon'] . $value,$key[1],$display_id['Murphy'] . $value);
Also using sprintf also make your code more readable.
Sorry if this is confusing. It's tough for me to put into words having beginner knowledge of PHP.
I'm using the following foreach loop:
foreach ($_POST['technologies'] as $technologies){
echo ", " . $technologies;
}
Which produces:
, First, Second, Third
What I want:
First, Second, Third
All I need is for the loop to skip the echo ", " for the first key. How can I accomplish that?
You can pull out the indices of each array item using => and not print a comma for the first item:
foreach ($_POST['technologies'] as $i => $technologies) {
if ($i > 0) {
echo ", ";
}
echo $technologies;
}
Or, even easier, you can use implode($glue, $pieces), which "returns a string containing a string representation of all the array elements in the same order, with the glue string between each element":
echo implode(", ", $_POST['technologies']);
For general case of doing something in every but first iteration of foreach loop:
$first = true;
foreach ($_POST['technologies'] as $technologies){
if(!$first) {
echo ", ";
} else {
$first = false;
}
echo $technologies;
}
but implode() is best way to deal with this specific problem of yours:
echo implode(", ", $_POST['technologies']);
You need some kind of a flag:
$i = 1;
foreach ($_POST['technologies'] as $technologies){
if($i > 1){
echo ", " . $technologies;
} else {
echo $technologies;
}
$i++;
}
Adding an answer that deals with all types of arrays using whatever the first key of the array is:
# get the first key in array using the current iteration of array_keys (a.k.a first)
$firstKey = current(array_keys($array));
foreach ($array as $key => $value)
{
# if current $key !== $firstKey, prepend the ,
echo ($key !== $firstKey ? ', ' : ''). $value;
}
demo
Why don't you simply use PHP builtin function implode() to do this more easily and with less code?
Like this:
<?php
$a = ["first","second","third"];
echo implode($a, ", ");
So as per your case, simply do this:
echo implode($_POST['technologies'], ", ");
I am writing a recursive function to print out the differences between 2 multildimensional php arrays. The purpose of this code is to see the difference between jpeg headers to deteremine how adobe bridge cs3 is saving rating information within the jpg file.
When I am single-stepping through the code using my eclipse - zend debugger ide, it appears that even though the initial if statement is false (ie neither values is an array), the subsequent elseif statements are never executed. The function is attached below.
Note: Changes since original post based on comments
Added a default level= ''
Removed comments between the if{} elseif{} blocks
Removed an else; at the end of the block that had no function
Encoded the < and > symbols so angle bracket would show in my code
function array_diff_multi($array1,$array2,$level=''){
$keys = array_keys($array1);
foreach($keys as $key)
{
$value1 = $array1[$key];
if(array_key_exists($key,$array2) ){
$value2 = $array2[$key];
if (is_array($value1) && is_array($value2)){ // Check if they are both arrays, if so recursion is needed
array_diff_multi($value1,$value2,$level . "[ " . $key . " ]");
}
elseif(is_array($value1) != is_array($value2)){ // Recursion is not needed, check if comparing an array to another type
print "<br>" . $level . $key ."=>" . $value1 . "as array, compared to ". $value2 ."<br>";
}
elseif($value1 != $value2){ // the values don't match, print difference
print "<br>" . $level . $key ."=>" . $value1 ." != " . $value2 ."<br>";
}
}
else{
print "<br>" . $level. $key . "does not exist in array2";
}
}
}
could it be because you have
else;
at the end...?
Try removing that or turning that into 'real code'
It works fine for me here. I put your function (with the tiny difference of adding a default value of '' to the level parameter), and these two arrays:
$a1 = array('foo', 'bar', 2, array('baz', '3', 4, array(54,45)));
$a2 = array('faz', 'bar', 4, array('buz', '3', 5, 54));
And got this output:
0=>foo != faz
2=>2 != 4
[ 3 ]0=>baz != buz
[ 3 ]2=>4 != 5
[ 3 ]3=>Arrayas array, compared to 54
Perhaps your starting arrays are not what you think they are...?
This doesn't exactly answer your question, but I think Adobe Bridge saves metadata in dotfiles in the same directory as the files. For example, sort information is saved in a .bridgesort file.
The only way that all the elseifs would be skipped is if the two variables are not arrays and are equal.