How to solve array problem?
Excepted result:
Array
(
[0] => 2011/03/13
[1] => 2011/03/14
[2] => 2011/02/21
)
Failed result that I get now:
Array
(
[0] => 2011/03/13
)
Array
(
[0] => 2011/03/14
)
Array
(
[0] => 2011/02/21
)
PHP code:
<?php
function get_dir_iterative($dir='.',$exclude=array('cgi-bin','.','..')){
$exclude=array_flip($exclude);
if(!is_dir($dir)){return;}
$dh=opendir($dir);
if(!$dh){return;}
$stack=array($dh);
$level=0;
while(count($stack)){
if(false!==($file=readdir($stack[0]))){
if(!isset($exclude[$file])){
if(is_dir("$dir/$file")){
$dh=opendir("$file/$dir");
if($dh){
$d=$file;
array_unshift($stack,$dh);
++$level;
}
}else{
if(isset($d)&&$level>0){
$mod=date('Y/m/d',filemtime("$d/$file"));
$ds="$d/";
}else{
$mod=date('Y/m/d',filemtime($file));
$ds='';
}
$array=array($mod);
//$b=array_merge($array); // it doesn't solve the problem
print_r($array);
}
}
}else{
closedir(array_shift($stack));
--$level;
}
}
}
get_dir_iterative();
?>
Update:
On replacing $array=array($mod); with $array[]=$mod; does not return excepted result.
Array
(
[0] => 2011/03/13
)
Array
(
[0] => 2011/03/13
[1] => 2011/03/14
)
Array
(
[0] => 2011/03/13
[1] => 2011/03/14
[2] => 2011/02/21
)
array_merge() takes several parameters, the arrays that you want to merge, in your situation it should be:
array_merge($array, $mod);
// Or:
$array[] = $mod;
Note that you can simply add a new entry to your global array (second example) to add a value. Array_merge eats more memory than it needs.
$array=array($mod);
//$b=array_merge($array); // it doesn't solve the problem
print_r($array);
For one, array_merge requires at least two arrays to do its thing. You're only providing one, so there's nothing to merge with. Your "live" version creates a new array each time, so that's why you're getting the 'bad' output. Why not simply do
$array[] = $mod;
which'll append the file's mtime to the array on each iteration? You might want to store the file's details as well, so you know where the mtime's coming from, so
$array[$dir/$file] = $mod;
might be of more use.
You can achieve this by replacing this code: $array=array($mod); with this code: $array[]=$mod;.
Try replacing the commented out line with $b[] = $array
Related
How do you count items in an array that's part of an array? I'm using Advance Custom Fields plugin in WordPress and my array print_r(get_field('irl_today_entry')) output looks like this:
Array
(
[0] => Array
(
[acf_fc_layout] => irl_today_website_entry
[irl_today_website] => Array
(
[0] => Array
( data removed)
[1] => Array
( data removed )
)
)
[1] => Array
(
[acf_fc_layout] => irl_today_social_entry
[irl_today_social] => Array
(
[0] => Array
( data remove )
[1] => Array
( data remove)
)
)
)
How do you only count items in [irl_today_social]? I've tried a lot of options that do not work.
If you have multiple entries with irl_today_social you might also use array_map and array_column
$res = array_map(function($x) {
return count($x);
}, array_column($arrays, "irl_today_social"));
print_r($res);
Output
Array
(
[0] => 2
)
See a php demo
You can use array_reduce,
array_reduce(get_field('irl_today_entry'),function($a,$b){
return count($a["irl_today_website"]) + count($b["irl_today_website"]);
});
Loop through your array, get the count of your desired index, and add that to a counter.
$count = 0;
foreach(get_field('irl_today_entry') as $entry){
$count = $count + count(entry['irl_today_social']);
}
Simply you can use "Count" Function
Here is the Solution:
Step 1: First get your array items of ['irl_today_entry'].
Step 2 Then Count your ['irl_today_social'] items.
Example:
foreach(get_field('irl_today_entry') as $entry){
$socialData = count(entry['irl_today_social']);
}
Thanks
according to my understaing just simply try this
echo count($your_array[1]['irl_today_social']);
I am trying to count elements in an array, but it doens't work as intended:
I have a while loop, which loops through my user table:
while($refsData=$refs->fetch()){
$new_array = array($refsData['id']);
print_r($new_array);
$outcome = $rentedrefs->_paying($new_array);
}
The print_r($new_array); gives me:
Array
(
[0] => 90427
)
Array
(
[0] => 90428
)
Array
(
[0] => 90429
)
Array
(
[0] => 90430
)
Array
(
[0] => 90431
)
Array
(
[0] => 90432
)
Array
(
[0] => 90433
)
Array
(
[0] => 90434
)
Array
(
[0] => 90435
)
Array
(
[0] => 90436
)
Inside the _paying function, I count the number of values from the array:
function _paying($referrals_array){
echo count($referrals_array);
}
The problem is, that the above count($referrals_array); just gives me: 1, when it should be 10
What am I doing wrong?
You create a new array at each step of the loop. Instead it should be written like this:
$new_array = array();
while($refsData=$refs->fetch()){
$new_array[] = $refsData['id'];
// print_r($new_array);
}
$outcome = $rentedrefs->_paying($new_array);
Note that I moved the _paying call outside the loop, as it seems to be the aggregating function. If not, you'd most probably make it process $refsData['id'] instead - not the whole array.
As a sidenote, I'd strongly recommend using fetchAll() method (instead of fetch when you need to fill a collection with results of a query. It'll be trivial to count the number of the resulting array.
It works properly, in each circulation of loop you have one array, so in first you have:
Array
(
[0] => 90427
)
in 2nd:
Array
(
[0] => 90428
)
and so on.
It should work properly:
var $count = 0;
while($refsData=$refs->fetch()){
$new_array = array($refsData['id']);
$count += count($new_array);
$outcome = $rentedrefs->_paying($new_array);
}
You are creating $new_array as a new array with the single element $refsData['id']. The count of 1 is therefore correct.
To get the number of results, either use a COUNT(*) select to ask your sql server, or add a counter to your loop, like this:
$entries = 0;
while($refsData=$refs->fetch()){
$new_array = array($refsData['id']);
print_r($new_array);
$entries++;
$outcome = $rentedrefs->_paying($new_array);
}
echo $entries;
You are not adding elements to an array, but creating a new array each iteration. To add elements, just do:
$new_array[] = $refsData['id'];
This is what I get after a print_r($myArray) (wrapped in pre) on my array.
Array
(
[0] => 203.143.197.254
[1] => not/available
)
Array
(
[0] => 40.190.125.166
[1] => articles/not/a/page
)
Array
(
[0] => 25.174.7.82
[1] => articles/not/a/page
)
How would I return or echo just the first two in this case (no regex), given the fact that I would like to only output each array whose [1] value has not been echoed before?
My list as far more entries and $myArray[1] is sometimes the same, I want to skip echoing the same thing.
I have tried array_unique but I can't get it to work as param 1 is expected to be an array.
print_r(array_unique($myArray));
This works. Didn't do a full copy paste job but hopefully you get the idea of the logic
$echoed = array();
foreach($array as $arr) {
if(!in_array($arr[1],$echoed)) {
echo $arr[1];
$echoed[] = $arr[1];
}
}
$echoedBefore = array();
print_r(array_filter($myArray, function($entry) {
global $echoedBefore;
$alreadyEchoed = in_array($entry[1], $echoedBefore);
if (!$alreadyEchoed) {
$echoedBefore[] = $entry[1];
}
return !$alreadyEchoed;
}));
I need to add some details to an array without overwriting the old data.
At the moment I have something like this if I print_r($data)
Array
(
[one] => Hello
[two] => Hello World
)
I then have a function that adds some data to the array
foreach ($rubric as $rs){
if($rs['position']==1){$data['one']=$rs;}
if($rs['position']==2){$data['two']=$rs;}
}
This gives me the following if I print_r($data)
Array
(
[one] => Array
(
[id] => 1
)
[two] => Array
(
[id] => 2
)
)
Does anyone have any ideas
What I would like to do is....
foreach ($rubric as $rs){
if($rs['position']==1){$data['one']['details']=$rs;}
if($rs['position']==2){$data['two']['details']=$rs;}
}
With the aim of adding a new subarray called "details" within each array item...
Does that make sense? If I try to run that code however I get the following error
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Could someone tell me what I'm doing wrong?
See, array_push array_unshift.
Array push puts an element at the end. Array unshift adds a number to the beginning of the array.
You can also use the structure
$myArray['nameOfNewElement']=$newElement;
This adds $newElement to the array $myArray;
You can use array_merge.
According to your question, here is the solution :
// sample array
$rubric = array(0=> array("position"=>1),1 => array("position"=>2));
$data = array("one" => "Hello","two" => "hello world");
foreach ($rubric as $rs){
if($rs['position']==1){
$d= array_merge($data,$rs);
}
if($rs['position']==2){
$d= array_merge($data,$rs);
}
}
print_r($d);
Here is the working DEMO : http://codepad.org/rgKiv542
Hope, it'll help you.
When you write $data['one'] = $rs;, you are assigning $rs to overwrite the value "Hello".
Perhaps what you were trying to do is
$data['three'] = $rs['id'];
I'm new to working with arrays so I need some help. With getting just one vaule from an array. I have an original array that looks like this:
$array1= Array(
[0] => 1_31
[1] => 1_65
[2] => 29_885...)
What I'm trying to do is seach for and return just the value after the underscore. I've figured out how to get that data into a second array and return the vaules as a new array.
foreach($array1 as $key => $value){
$id = explode('_',$value);
}
which gives me:
Array ( [0] => 1 [1] => 31 )
Array ( [0] => 1 [1] => 65 )
Array ( [0] => 29 [1] => 885 )
I can also get a list of the id's or part after the underscore by using $id[1] I'm just not sure if this is the best way and if it is how to do a search. I've tried using in_array() but that searches the whole array and I couldn't make it just search one key of the array.
Any help would be great.
If the part after underscore is unique, make it a key for new array:
$newArray = array();
foreach($array1 as $key => $value){
list($v,$k) = explode('_',$value);
$newArray[$k] = $v;
}
So you can check for key existence with isset($newArray[$mykey]), which will be more efficient.
You can use preg_grep() to grep an array:
$array1= array("1_31", "1_65", "29_885");
$num = 65;
print_r(preg_grep("/^\d+_$num$/", $array1));
Outputs:
Array
(
[1] => 1_65
)
See http://ideone.com/3Fgr8
I would say you're doing it just about as well as anyone else would.
EDIT
Alternate method:
$array1 = array_map(create_function('$a','$_ = explode("_",$a); return $_[1];'),$array1);
echo in_array(3,$array1) ? "yes" : "no"; // 3 being the example
I would have to agree. If you wish to see is a value exists in an array however just use the 'array_key_exists' function, if it returns true use the value for whatever.