I want to make an array in PHP but it should be in specific format such this:
array(1, 5, 3)
I mean, I have the values 1, 5 and 3 from my database, so I had to loop it with the use of array_push
$a=array();
foreach( $db_nums as $db_num ){
array_push($a, $db_num);
}
print_r($a);
but it outputs:
Array ( [0] => 1 [1] => 5 [2] => 3 );
i want it to be only:
array(1, 5, 3 );
Any ideas how? Thanks a lot for any help!
Just use the below code:
$Array = array(1,2,3);
Edit:
$a = array();
foreach( $db_nums as $db_num )
{
$a[] = $db_num;
}
print_r($a);
$db_nums = array(1, 2, 3); //pointless example, but comes from DB
$a = array();
foreach ($db_nums as $n) {
$a[] = $n;
}
var_export($a);
That will output:
array (
0 => 1,
1 => 2,
2 => 3,
)
Which is about as close as you're going to get without writing your own function to do it.
(Also note that in this example, you could just directly do var_export($db_nums).)
<?php $array = array("foo", "bar", "hallo", "world"); var_dump($array); ?>
You can use that
Related
I have a PHP array which looks like this...
array
(
[0] => apple,
[1] => orange,
)
I need to ensure the array contains 4 items, so in the instance above I want to end up with this...
array
(
[0] => apple,
[1] => orange,
[2} => ,
[3] => ,
)
Am I best looping through this with a counter and creating a new array, or is there a better method?
Pad your array with elements to a size that you need:
$my_arr = [1,2];
$my_arr = array_pad($my_arr, 4, '');
This should do what you're after
$iNumberOfElements = 5;
$a = array('apple', 'orange');
if(count($a) < $iNumberOfElements){
while (count($a) < $iNumberOfElements) {
$a[] = "";
}
}
var_dump($a);
exit;
as #iainn said: php.net/manual/en/function.array-pad.php
there is this function:
$input = array(12, 10, 9);
$result = array_pad($input, 5, 0);
// result is array(12, 10, 9, 0, 0)
5 is the size of your array, 0 is the default value to empty cells
I have the two PHP loops as below:
foreach ($directData as $key => $val) {
echo $val;
echo "|"; //Just for the visual.
}
foreach ($sdata as $key => $val) {
echo $val;
echo "|"; //Just for the visual.
}
Which output:
5|5|5|10|10|10|0| and the second: 2|2|2|5|5|5|20|
My question is, how can I combine these two result (add them) and print it out like above?
So, it would be:
7|7|7|15|15|15|20
php > $a = [1, 2, 3];
php > $b = [4, 5, 6];
php > $c = array_map(function($x, $y){ return $x + $y; }, $a, $b);
php > print_r($c);
Array
(
[0] => 5
[1] => 7
[2] => 9
)
php > print_r(implode($c, '|') . '|');
5|7|9|
Broken down for better readability, this will sum them into $new array.
array_map will iterate through both given arrays and apply the callback function to both of the arrays at their current indexes. The callback function will sum the values of at current indexes.
$new=array_map(
function(){
return array_sum(func_get_args());
},
$directData, $sdata
);
$out= '';
foreach ($new as $key => $val)
$out .= $val.'|';
echo rtrim($out,'|');
in simple way you can do it like:
$finalData = array_map(function () {
return array_sum(func_get_args());
}, $directData, $sdata );
print_r($finalData );
If each index corresponds in the arrays
$rezult = array();
for($i=0;$i<count($directData);$i++){
$rezult[] = $directData[$i] + $sdata[$i];
}
echo join("|",$rezult);
Here is a method that uses the iterators that all PHP arrays have.
/**
* Use the iterator that all PHP arrays have.
*
* Check both arrays for having entries.
*/
$listOut = array(); // output in here
while (current($listA) !== false && current($listB) !== false) {
$listOut[] = current($listA) + current($listB);
next($listA);
next($listB);
}
Input:
$listA = array(5, 5, 5, 10, 10, 10, 0);
$listB = array(2, 2, 2, 5, 5, 5, 20, );
Output
Array
(
[0] => 7
[1] => 7
[2] => 7
[3] => 15
[4] => 15
[5] => 15
[6] => 20
)
7| 7| 7| 15| 15| 15| 20
I have the following code:
$a = array();
$b = array('a', 'b');
for($i=0; $i<3; $i++){
$a[] = array($b[$i] => array(1, 2, 3));
}
print_r($a);
I get the following result:
Array
(
[0] => Array
(
[a] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
[1] => Array
(
[b] => Array
(
[0] => 1
[1] => 2
[1] => 3
)
)
)
This is what I'm trying to accomplish:
array (
'a' => array ( 1, 2, 3 )
'b' => array ( 1, 2, 3 )
)
What am I doing wrong? I don't want $a to add numeric elements, but rather contain a, b, c as the indexes. Any suggestions? Thanks
change the forloop to like this
for($i=0; $i<count($b); $i++){
$a[$b[$i]] =array(1, 2, 3);
}
You can do,
$a = array();
$b = array('a', 'b');
for($i=0; $i<3; $i++){
if(isset($b[$i])){
$a += array($b[$i] => array(1, 2, 3));
}
}
DEMO.
You can set the key for $a like so:
$a = array();
$b = array('a', 'b');
for($i=0; $i<count($b); $i++){
$a[$b[$i]] = array(1, 2, 3);
}
print_r($a);
Also, i changed your for loop to use count($b) as you where iterating 1 to many times with your hard coded 3
Try:
$a[$b[$i]] = array(1,2,3);
One more iteration..
$a = array();
$b = array('a', 'b');
for($i=0; $i<3; $i++){
$a[$b[$i]] = array(1, 2, 3);
}
print_r($a);
Let's check what you were doing wrong.
$a = array();
$b = array('a', 'b'); // Count of elements is 2
for($i=0; $i<3; $i++){ // this will loop 3 times assigning 0,1,2 to $i where. You only needed 0 and 1 for an array with 2 elements
$a[] = array($b[$i] => array(1, 2, 3)); // here you are adding a new element to $a without providing key. So it becomes a numeric indexed array.
}
Solution:
for($i=0; $i<count($b); $i++){ // you could use $i<2 as well however count($b) makes your code more dynamic and result won't be affected if no of elements in $b changes.
$a[$b[$i]] =array(1, 2, 3); // you put $b[$i] as key for $a which creates an associative array
}
So I'm using the pin method, but the reference is detected one level too late:
$pin = time();
function wrap($arr){
test($arr);
}
function test(&$arr){
global $pin;
if(in_array($pin, $arr))
return print "ref";
$arr[] = $pin;
foreach($arr as &$v){
if($v != $pin){
if(is_array($v))
return test($v);
print $v . " ";
}
}
}
$array = array(1, 2, 3);
$array[4] = &$array;
wrap($array);
I get 1 2 3 1 2 3 rec
But I expect 1 2 3 rec
If I just do test($arr) then it works, but the problem is that I need to wrap the test function inside another one that accepts values not references :(
Is there any way I can detect the reference at the right moment with my wrapper function too?
Introduction
I think a better approach would be to create a copy of the array and compare modification rather than use global pin and it can still be a 100% Recursive
Example 1
This is from your example above :
$array = array(1,2,3);
$array[4] = &$array;
wrap($array);
Output
Array
(
[0] => 1
[1] => 2
[2] => 3
[4] => ref
)
Example 2
Are we really sure its detecting reference or just a copy of the array
//Case 1 : Expect no modification
$array = array(1, 2, 3, array(1, 2, 3));
wrap( $array);
//Case 2 : Expect Modification in Key 2
$array = array(1, 2, 3, array(1, 2, 3));
$array[2] = &$array;
wrap( $array);
Output
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
Array
(
[0] => 1
[1] => 2
[2] => ref
[3] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
Example 3
Is this really recursive ?
$array = array(1, 2, 3, array(1, 2, 3));
$array[4][4][2][6][1] = array(1,2,3=>&$array);
wrap( $array);
Output
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[4] => Array
(
[4] => Array
(
[2] => Array
(
[6] => Array
(
[1] => Array
(
[0] => 1
[1] => 2
[3] => ref <-- GOT YOU
)
)
)
)
)
)
Your Modified Function
/**
* Added printf since test now returns array
* #param array $arr
*/
function wrap(array $arr) {
printf("<pre>%s<pre>", print_r(test($arr), true));
}
/**
* - Removed Top Refrence
* - Removed Global
* - Add Recursion
* - Returns array
* #param array $arr
* #return array
*/
function test(array $arr) {
$temp = $arr;
foreach ( $arr as $key => &$v ) {
if (is_array($v)) {
$temp[$key]['_PIN_'] = true;
$v = isset($arr[$key]['_PIN_']) ? "ref" : test($v);
}
}
unset($temp); // cleanup
return $arr;
}
I think you are over-complicating things. I solved this by looping over the array and checking if the current value in the array is equivalent (===) with the array.
function wrap( $arr){
test($arr);
}
function test( $arr){
foreach( $arr as $v) {
if( $v === $arr) {
print 'ref, ';
} else {
if( is_array( $v)) {
test( $v);
} else {
print $v . ', ';
}
}
}
}
I used the following test cases:
echo "Array 1:\n";
$array1 = array(1, 2, 3);
$array1[4] = &$array1;
wrap( $array1);
echo "\nArray 2:\n";
$array2 = array(1, 2, 3, array(1, 2, 3));
$array2[2] = &$array2;
wrap( $array2);
Which produced this output:
Array 1:
1, 2, 3, ref
Array 2:
1, 2, ref, 1, 2, 3,
However, the above method will fail for nested references. If nested references are possible, as in the following test case:
echo "\nArray 3:\n";
$array3 = array(1, 2, 3, array(1, 2, 3));
$array3[3][2] = &$array3;
wrap( $array3);
Then we need to keep track of all the array references we've seen, like this:
function wrap( $arr){
test( $arr);
}
function test( $arr){
$refs = array(); // Array of references that we've seen
$f = function( $arr) use( &$refs, &$f) {
$refs[] = $arr;
foreach( $arr as $v) {
if( in_array( $v, $refs)) {
print 'ref, ';
} else {
if( is_array( $v)) {
$f( $v);
} else {
print $v . ', ';
}
}
}
};
$f( $arr);
}
Using the above test case, this outputs:
Array 3:
1, 2, 3, 1, ref, 3,
Edit: I've updated the final function that keeps track of all references to eliminate the global dependencies.
function wrap($arr){ test($arr); }
/// ...
wrap($array);
Your wrap() function allocates new memory block for $arr. When you calling test() function within wrap()s body, it takes reference of $arr memory block, but not an $arrays memory block, because $arr is a copy of $array and PHP memory management system stores them separately.
There is a universal reference spotting function:
function is_equal_refs(&$a, &$b){
$buffer = $a; // saving current value in temporary variable
$a = md5(time()); // assigning new value to memory block, pointed by reference
$result = ($a === $b); // if they're still equal, then they're point to the same place.
$a = $buffer; // restoring value
return $result; // returning result
}
So, lets do some testing:
<?php
header('Content-Type: text/plain');
function is_equal_refs(&$a, &$b){
$buffer = $a;
$a = md5(time());
$result = ($a === $b);
$a = $buffer;
return $result;
}
function wrap($arr){ test($arr); }
function test(&$arr){
foreach($arr as &$v){
if(is_equal_refs($arr, $v)){
print_r('ref');
echo PHP_EOL;
break;
}
if(is_array($v))return test($v);
print_r($v);
echo PHP_EOL;
}
}
$array = array(1, 2, 3);
$array[] = &$array;
wrap($array);
?>
Shows:
1 // < $arr
2
3
1 // < $array
2
3
ref // < $array doubled -> reference found
The reason of such behavior is $arr[3] contains reference for $arrays memory block, but not reference of itself's memory block.
Lets remove a $array[] = &$array; row, and modify wrap() function to check:
function wrap($arr){
$arr[] = &$arr;
test($arr);
}
And result would be:
1 // < $arr
2
3
ref // < $arr doubled -> reference found
Because $arr not points to $array, but to itself in $arr[3]. So, in your code there are different references you want to spot.
CONCLUSION: What you want to achieve is breaking out PHP memory management rules.
UPDv1:
Need to seek a workaround, to restore $array reference in a wrap() function scope.
1) A "bad" / "globals" practice:
<?php
header('Content-Type: text/plain');
function is_equal_refs(&$a, &$b){
$buffer = $a;
$a = md5(time());
$result = ($a === $b);
$a = $buffer;
return $result;
}
function wrap($array){
global $check; // <- THIS
test(empty($check) ? $array : $check); // <- THIS
}
function test(&$arr){
foreach($arr as &$v){
if(is_equal_refs($v, $arr)){
print_r('ref');
echo PHP_EOL;
break;
}
if(is_array($v)){
test($v);
} else {
print $v . ' ';
echo PHP_EOL;
}
}
}
$array = array(1, 2, 3);
$array[] = &$array;
$check = &$array; // <- and THIS
wrap($array);
?>
Which shows:
1
2
3
ref
2) A "wrap everything in array or object" practice: (prefered and reliable)
<?php
header('Content-Type: text/plain');
define('REF_MARKER', 'x-my-tr!cky-ref'); // trick key definition
function is_equal_refs(&$a, &$b){
$buffer = $a;
$a = md5(time());
$result = ($a === $b);
$a = $buffer;
return $result;
}
function wrap(array $arr){
// restore reference, if trick.
// it might be moved to the top part of test() function (might affect performance).
if(isset($arr[REF_MARKER]))$arr = &$arr[REF_MARKER];
test($arr);
}
// $array - subject to test;
// $refs - internal ref list of all `subjects`;
function test(&$array, $refs = array()){
$refs[] = &$array;
foreach($array as &$value){
foreach($refs as &$ref){
if(is_equal_refs($ref, $value))return print 'ref ';
}
if(is_array($value)){
$refs[] = &$value;
test($value, $refs);
} else {
print $value . ' ';
}
}
}
$array = array(1, 2, 3);
$array[] = &$array;
wrap(array(REF_MARKER => &$array)); // trick
print PHP_EOL;
$ring = array(1, 2, 3, array(4, 5, 6));
$ring[3][] = &$ring;
wrap(array(REF_MARKER => &$ring)); // trick
print PHP_EOL;
$test = array('a', 'b', 'c');
$ring = array(1, 2, 3);
$ring[] = &$test;
$test[] = &$ring;
wrap(array(REF_MARKER => &$ring)); // trick
print PHP_EOL;
wrap(range(1, 5)); // normal
print PHP_EOL;
$test = array(1, 2, 3, array(1, 2, 3), 4, array(5, 2, 3), array(6, array(1, 2, 3), 7), array(1, 2, 3));
wrap($test); // normal
print PHP_EOL;
$test[] = &$test;
$test[3][] = &$test;
$test[5][] = &$test[3];
wrap(array(REF_MARKER => &$test)); // trick
?>
Shows:
1 2 3 ref
1 2 3 4 5 6 ref
1 2 3 a b c ref
1 2 3 4 5
1 2 3 1 2 3 4 5 2 3 6 1 2 3 7 1 2 3
1 2 3 1 2 3 ref 4 5 2 3 ref 6 1 2 3 7 1 2 3 ref
i have two arrays
$array1 = array(1, 2, 2, 3);
$array2 = array( 1, 2, 3,4);
and when did :
var_dump(array_diff($array1, $array2));
getting :
array(0){}
as output , but i am looking for :
array(1){[2]=>2}
can someone please let me know how to do it
Thanks in Advance
Try this
$array1 = array(1, 2, 2, 3, 4, 5, 5, 7);
$array2 = array(1, 2, 4, 6, 3, 3, 5);
$diff = array_filter($array1,
function ($val) use (&$array2) {
$key = array_search($val, $array2);
if ( $key === false ) return true;
unset($array2[$key]);
return false;
}
);
print_r($diff);
// Array ( [2] => 2 [6] => 5 [7] => 7 )
If you want to count number of duplicate element from same array as well as from multiple arrays, please use below code,
<?php
$array1 = array(1,2,2,3,7);
$array2 = array(1,2,3,4);
$diff_array = array();
$diff_array1 = array_count_values($array1);
$diff_array2 = array_count_values($array2);
$a = array_keys($diff_array1);
$b = array_keys($diff_array2);
for($i=0;$i<count($a);$i++)
{
if($a[$i] == $b[$i])
{
$x = $a[$i];
$y = $b[$i];
$diff_array1[$x] += $diff_array2[$y];
}
}
$diff_array1=array_diff($diff_array1, array('1'));
echo '<pre>';
print_r($diff_array1);
?>
This will get you the desired result:
$array1 = array(1, 2, 2, 3);
$array2 = array( 1, 2, 3,4);
$countArray1 = array_count_values($array1);
$countArray2 = array_count_values($array2);
foreach($countArray1 as $value=>$count) {
if($count > 1) $dupArray[] = $value;
}
foreach($countArray2 as $value=>$count) {
if($count > 1) $dupArray[] = $value;
}
print_r($dupArray);
Result
Array
(
[0] => 2
)
Explanation
Using array_count_values will count all the values of an array, which would look like:
Array
(
[1] => 1
[2] => 2
[3] => 1
)
Array
(
[1] => 1
[2] => 1
[3] => 1
[4] => 1
)
We then iterate through each array_count_values to locate values that occur more than once. This will work when you have more than one set of duplicate values:
$array1 = array(1, 2, 2, 3);
$array2 = array( 1, 2, 3, 4, 3);
Result
Array
(
[0] => 2
[1] => 3
)
While it may be less elegant, the simple way to do this is with a for loop:
$diff_array = array();
for ($i = 0; ($i < count($array1)) and ($i < count($array2)); $i++)
{
if ($array1[$i] !== $array2[$i]) { $diff_array[$i] = $array1[$i]; }
}