i want to do this type
in my form i have check box array and the function i want to call as the size of check box array,
now all is ok simple one time calling.
but i want to call the function as above,
and store the function return value in one array
as function return array so i want to do like
this
for user id 1->callfunction return array
user id 2->callfunction return array
....
....
i have try to used the array_push but i does not get any result
here is my code
$track = array();
for($i=0;$i<sizeof($usr);$i++)
{
if (!empty($start) and !empty($end))
{
$track_e = $tracker->getProjectTrack($id, $usr[$i], $taski, $start, $end);
//$track = $tracker->getProjectTrack($id, $usr, $taski, $start, $end);
}
$track=array_push($track,$track_e);
}
if you want to go through array, use foreach
$track = array();
if (!empty($start) and !empty($end)){
foreach ($usr as $u){
array_push($track,$tracker->getProjectTrack($id, $u, $taski, $start, $end);
}
}
Solution:
$track=array_push($track,$track_e);
array_push doesn't return an array; it returns the new number of elements in the array, modifying the array it receives as an argument in-place. It's actually much easier to just write:
$track []= $track_e;
Suggestion:
for($i=0;$i<sizeof($usr);$i++) {
# ...
$track_e = $tracker->getProjectTrack($id, $usr[$i], $taski, $start, $end);
Why not simplify the process of indexing $usr and counting the number of elements in it like so:
foreach ($usr as $usr_elem) {
# ...
$track_e = $tracker->getProjectTrack($id, $usr_elem, $taski, $start, $end);
Array ( [0] => Array ( [0] => 5 ) [1] => Array ( [0] => 6 ) [2] => Array ( [0] => 7 ) )
instead of this it returns
Array ( [0] => Array ( [0] => Array ( [0] => 7 ) ) [1] => Array ( [0] => Array ( [0] => 9) ) )
something like that
so i want to return it in as same first one.
Related
I need the array as (2) from a single query
can anyone help ?
1. Array
(
[0] => Array
(
[crop_id] => 3
[crop_name] => Barley
)
2. Array
(
[0] => Array
(
[Barley] => 3
)
)
Don't know about query but you can do that simply using array_map()
$array[] = array('crop_id' => 3, 'crop_name' => "Barley");
$result = array_map("myfunction",$array);
print_r($result);
function myfunction($v)
{
$data = [];
$data[$v['crop_name']] = $v['crop_id'];
return $data;
}
Let's suppose you have stored your data in array named as crop_data like
$crop_data[0][crop_id]=3;
$crop_data[0][crop_name]='Barley';
....
$crop_data[n][crop_id]=187;
$crop_data[n][crop_name]='Wheat'
Try this code:
$new_crop_result=array()
foreach($crop_data as $key=>$record)
{
$new_crop_result[$key][$record[crop_name]]=$record[crop_id];
}
I have below type of array, now I want to get count of it's subarray
For example I want to get count key 7 & 8. How to do it ? Any solution for that ? I tried but but no success :(
Array
(
[0] => stdClass Object
(
[id] => 4
[Blogdata] => stdClass Object
(
[7] => Array
(
[0] => stdClass Object
(
[blog_id] => 105
)
)
[8] => Array
(
[0] => stdClass Object
(
[blog_id] => 101
)
)
)
)
)
$date_count = array();
foreach($FeaturedBlogs as $Key=>$date) {
foreach($date as $d) {
$key = array_keys($d); // get our date
// echo $key;echo "<br>";
print_r($d);
$date_count[$key[0]]++;
}
}
Try this....
//Count Sub Array
$final_array = [];
$x = 0;
function countSubArray($data)
{
global $final_array;
global $x;
foreach($data as $key)
{
if(is_array($key))
{
$final_array[$x][0] = "1";
$final_array[$x][1] = json_encode($key);
$final_array[$x][2] = count((array)$key);
$x++;
countSubArray($key);
}
if(is_object($key))
{
$final_array[$x][0] = "2";
$final_array[$x][1] = json_encode($key);
$final_array[$x][2] = count((array)$key);
$x++;
countSubArray($key);
}
}
}
// Call Function...
countSubArray($arr); // what array you count..
// Display Sub Array Count...
$t_count = 0;
foreach($final_array as $d)
{
if($d[0] == 1)
{
echo "Array Count :".$d[2]." Array : ".$d[1]."<br>";
$t_count++;
}
}
echo "Total Array Count :".$t_count;
output of this example is...
Array
(
[0] => stdClass Object
(
[id] => 4
[Blogdata] => stdClass Object
(
[7] => Array
(
[0] => stdClass Object
(
[blog_id] => 135
)
)
[8] => Array
(
[0] => stdClass Object
(
[blog_id] => 101
)
)
)
)
)
Array Count :1 Array : [{"blog_id":135}]
Array Count :1 Array : [{"blog_id":101}]
Total Array Count :2
Updated Answer
This is going to use a function as it's own callback function. The function will loop through an object or an array and test each element to see if it is another object or array.
If it did find a new object or array, then the function calls itself again to perform the same operations on the element, effectively traversing through your entire array.
When it has found the bottom of each element it will return the value of the counter which was keeping track of how many arrays it came across.
Like so:
function arrayCounter($data){
//At this point we have an array or an object.
//Lets loop across the elements and see if we have any more nested arrays or objects.
foreach($data as $key){
$count = 0;
//Test each element to see if it's an object or an array.
if(is_array($key) || is_object($key)){
//If it is we are going to send the element through another instance of the function.
$count += arrayCounter($key);
}
//If the element is an array we are going to increment the counter.
if(is_array($key)){
$count++;
}
}
return $count;
}
$count = arrayCounter($data);
echo 'Count: ' . $count; //Using your data this will return "2".
Hope it helps!
I have 5 different array whose structure is :-
Array ( [0] => http://www.php.net/200
)
Array ( [0] => http://www.php.net/?setbeta=1&beta=1302
)
Array ( [0] => http://www.php.net/downloads.php200
)
Array ( [0] => http://www.php.net/docs.php200
)
Array ( [0] => http://www.php.net/FAQ.php302
)
I need to merge these all in a single array whose structure would be like:-
Array ( [0] => http://www.php.net/200
[1] => http://www.php.net/?setbeta=1&beta=1302
[2] => http://www.php.net/downloads.php200
[3] => http://www.php.net/docs.php200
[4] => http://www.php.net/FAQ.php302
)
One thing i want to confirm that these arrays are forming inside a loop function and it could be of any number and also they have a single name i.e $array
Simplest is probably just array_merge().
$merged = array_merge( $ar1, $ar2, $ar3, $ar4, $ar5 );
Use array_merge.
$arr = array_merge($arr1,$arr2,$arr3,$arr4,$arr5);
Edit After seeing your comment, you are having multidimensional array.
$arr = array();
for($i = 0; $i < $old_arr; $i++) {
$arr[] = $old_arr[$i][0];
}
Use array_merge
$arr1=Array ( 0 => 'http://www.php.net/200');
$arr2=Array( 0 => 'http://www.php.net/?setbeta=1&beta=1302');
$arr3=Array( 0 => 'http://www.php.net/downloads.php200');
$merged=array_merge($arr1,$arr2,$arr3);
print_r($merged);
$newarray = array();
while ( is_array( array_get_function_here() ) )
{
$newarray = array_merge( $newarray, $array );
}
I would try that if you get the different arrays in a looping function.
The above $array is something that array_get_function_here() should return for the while loop validity check (if it returns and array, the while check runs as true). array_get_function_here() is just an example and should be some function that returns a value for $array.
I am getting an array like below after i formatted my result set into the below format.
Array
(
[0] => xxx#2456
[1] => xxx#2345
[2] => xxy#1234
[3] => xxy#123
[4] => xyz#3456
);
I want the result array from the above array like below. I am thinking how ot proceed using PHP string functions or is there any other way.
Note: There may be so many array elements like the above but i want unique elements after we added last digits after '#'.
Array
(
[0] => xxx#4801
[1] => xxy#1464
[2] => xyz#3456
);
Any ideas how to proceed....... In the above output array format xxx#4801 is sum of 2345 and 2456 of Input array.......
You can try
$unique = array_reduce($data, function ($a, $b) {
$c = explode("#", $b);
if (isset($a[$c[0]])) {
$d = explode("#", $a[$c[0]]);
$a[$c[0]] = $c[0] . "#" . ($c[1] + $d[1]);
} else {
$a[$c[0]] = $b;
}
return $a;
}, array());
echo "<pre>";
print_r(array_values($unique));
Output
Array
(
[0] => xxx#4801
[1] => xxy#1357
[2] => xyz#3456
)
Simple Online Demo
You can use array_unique() function and reindex it with array_values() Find the code for example.
array_unique($array)
array_values(array_unique($array));
I'm trying to use a specific object type from a JSON feed, and am having a hard time specifying it. Using the code below I grab and print the specific array (max) I want,
$jsonurl = "LINK";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json,true);
$max_output = $json_output["max"];
echo '<pre>';
print_r($max_output);
echo '</pre>';
And from the Array below, all I want to work with is the [1] objects in each array. How can I specify and get just those values?
Array
(
[0] => Array
(
[0] => 1309924800000
[1] => 28877
)
[1] => Array
(
[0] => 1310011200000
[1] => 29807
)
[2] => Array
(
[0] => 1310097600000
[1] => 33345
)
[3] => Array
(
[0] => 1310184000000
[1] => 33345
)
[4] => Array
(
[0] => 1310270400000
[1] => 33345
)
[5] => Array
(
[0] => 1310356800000
[1] => 40703
)
Well you could fetch those values with array_map:
$max_output = array_map(function($val) { return $val[1]; }, $json_output["max"]);
This requires PHP 5.3, if you use an earlier version, then you can use create_function to achieve similar results:
$max_output = array_map(create_function('$val', 'return $val[1];'), $json_output["max"]);
When you need to create new array which will contain only second values, you may use either foreach loop which will create it or use array_map() (just for fun with anonymous function available since php 5.3.0):
$newArray = array_map( function( $item){
return $item[1]
},$array);
Then you want to use last ("max" -> considering array with numeric keys) item, you can use end():
return end( $item);
And when you can process your data sequentially (eg. it's not part of some big getData() function) you can rather use foreach:
foreach( $items as $key => $val){
echo $val[1] . " is my number\n";
}
After you get $max_output...
for( $i = 0; $i < length( $max_output ); $i++ ) {
$max_output[$i] = $max_output[$i][1];
}
try this:
$ones = array();
foreach ($max_output as $r)
$ones[] = $r[1];