my output is
Array ( [bid] => 1 [bookiing_date] => 2014-11-19 )
Array ( [bid] => 2 [bookiing_date] => 2014-11-15 )
Array ( [bid] => 3 [bookiing_date] => 2015-01-10 )
Array ( [bid] => 4 [bookiing_date] => 2015-01-27 )
but i want to convert in this form..
$date = ['2015-01-10','2015-01-27','2014-11-19'];
Please any one help me
What you've written above doesn't look too much like PHP, but something like this should do the trick
$single_dimensional_array = array();
foreach($multi_dimensional_array as $array_item) {
if(isset($array_item['bookiing_date'])) {
$single_dimensional_array[] = $array_item['bookiing_date'];
}
}
echo(var_export($single_dimensional_array), true);
Btw, the $single_dimensional_array[] = syntax above simply pushes the value on the right side of the equal sign onto the end of the array on the left side of the equal sign.
There is a very simple approach to get that... I think you did not tried anyway..here is the basic and simple answer ...
$sing_arr = array();
$multi_arr = array(array ( "bid"=> 1, "bookiing_date"=> "2014-11-19" ) ,
array ( "bid"=> 2, "bookiing_date"=> "2014-11-15" ) ,
array ( "bid"=> 3, "bookiing_date"=> "2015-01-10" ) ,
array ( "bid"=> 4 ,"bookiing_date"=> "2015-01-27" )
);
foreach($multi_arr as $key=>$val)
{
array_push($sing_arr,"'".$val["bookiing_date"]."'");
}
echo var_dump($sing_arr);
You Can Loop Through the array using foreach and store the date in another array:
Code:
<?php
$array1=array(
Array ( "bid" => 1 ,"bookiing_date" => "2014-11-19" ),
Array ( "bid" => 2 ,"bookiing_date" => "2014-11-15" ),
Array ( "bid" => 3 ,"bookiing_date" => "2015-01-10" ),
Array ( "bid" => 4 ,"bookiing_date" => "2015-01-27" )
);
// print_r($array1);
$date=array();
foreach ($array1 as $temparray){
$date[]=$temparray["bookiing_date"];
}
print_r($date);
Output:
Array
(
[0] => 2014-11-19
[1] => 2014-11-15
[2] => 2015-01-10
[3] => 2015-01-27
)
Use this
$array = array(array('bid' => 1, 'bookiing_date' => 2014 - 11 - 19),
array('bid' => 2, 'bookiing_date' => 2014 - 11 - 15),
array('bid' => 3, 'bookiing_date' => 2015 - 01 - 10),
array('bid' => 4, 'bookiing_date' => 2015 - 01 - 27));
$data = array();
foreach ($array as $array_item){
if(isset($array_item['bookiing_date'])) {
$data[] = $array_item['bookiing_date'];
}
}
print_r($data);
if you are using PHP 5.5+, use array_column()
$data = array_column($array, 'bookiing_date');
print_r($data);
Related
I am running SQL select query,getting the result in below format after executing the query.
Array
(
[0] => Array
(
[usertype_id] => 14
)
[1] => Array
(
[usertype_id] => 15
)
[2] => Array
(
[usertype_id] => 17
)
)
But i need result in below format
Array
(
[0] => 14
[1] => 15
[2] => 17
)
So how to loop through this to get the output in above format.
array_column works just fine here:
https://3v4l.org/qX46k
<?php
$input = [['usertype_id' => 14], ['usertype_id' => 15], ['usertype_id' => 17]];
$expected = [14,15,17];
$result = array_column($input, 'usertype_id');
var_dump($result === $expected);
Output for 7.1.25 - 7.3.2
bool(true)
use array array_map()
$res= array_map(function($arr=array()){
return $arr['usertype_id'];
},$input);
print_r($res);
If you are using PDO, then you can do as below
<?php
$sth = $dbh->prepare("SELECT usertype_id FROM user");
$sth->execute();
/* Fetch all of the values of the first column */
$result = $sth->fetchAll(PDO::FETCH_COLUMN, 0);
print_r($result);
?>
Reference: http://php.net/manual/en/pdostatement.fetchall.php
I have an array in PHP code below, and I want to convert this array to be grouped by data value. It's always hard to simplify arrays.
Original array:
Array
(
[0] => Array
(
[date] => 2017-08-22
[AAA] => 1231
)
[1] => Array
(
[date] => 2017-08-21
[AAA] => 1172
)
[2] => Array
(
[date] => 2017-08-20
[AAA] => 1125
)
[3] => Array
(
[date] => 2017-08-21
[BBB] => 251
)
[4] => Array
(
[date] => 2017-08-20
[BBB] => 21773
)
[5] => Array
(
[date] => 2017-08-22
[CCC] => 3750
)
[6] => Array
(
[date] => 2017-08-20
[CCC] => 321750
)
)
Below is my desired array:
Array
(
[2017-08-22] => Array
(
[AAA] => 1231
[CCC] => 3750
)
[2017-08-21] => Array
(
[AAA] => 1172
[BBB] => 251
)
[2017-08-20] => Array
(
[AAA] => 1125
[BBB] => 21773
[CCC] => 321750
)
)
It is also ok to have empty null value if the data doesn't exist. [BBB] => NULL for 2017-08-22.
Can anybody help? Thanks in advance...
A simple loop should do this..
$group = [];
foreach ($data as $item) {
if (!isset($group[$item['date']])) {
$group[$item['date']] = [];
}
foreach ($item as $key => $value) {
if ($key == 'date') continue;
$group[$item['date']][$key] = $value;
}
}
Here : this should do the work.
$dst_array = array();
foreach ($array as $outerval) {
foreach ($outerval as $key => $innerval) {
if ($key != 'date') {
$dst_array[$outerval['date']][$key] = $innerval;
}
}
}
It iterates through the array and then through the entries in each subarray. Any any that is not a date is assigned in the destination array in the subarray corresponding to its date and with its own current key.
I definitely wouldn't recommend any techniques that involve more than one loop -- this process can certainly be performed in a single loop.
If you like language construct iteration, use a foreach() loop: (Demo)
$result = [];
foreach ($array as $row) {
$date = $row['date'];
unset($row['date']);
$result[$date] = array_merge($result[$date] ?? [], $row);
}
var_export($result);
If you like to use functional programming and fewer global variables, use array_reduce(): (Demo)
var_export(
array_reduce(
$array,
function($accumulator, $row) {
$date = $row['date'];
unset($row['date']);
$accumulator[$date] = array_merge($accumulator[$date] ?? [], $row);
return $accumulator;
},
[]
)
);
These techniques unconditionally push data into the subarray with the key based on the date column value.
The above technique will work consistently even if the order of your subarray elements changes.
The ?? (null coalescing operator) is to ensure that array_merge() always has an array in the first parameter -- if processing the first occurrence of a given date, you simply merge the current iteration's data (what's left of it after unset() removes the date element) with an empty array.
I believe this solution will work for you:
<?php
$array = Array
(
0 => Array
(
'date' => '2017-08-22',
'AAA' => '1231',
),
1 => Array
(
'date' => '2017-08-21',
'AAA' => '1172',
),
2 => Array
(
'date' => '2017-08-20',
'AAA' => '1125'
),
3 => Array
(
'date' => '2017-08-21',
'BBB' => '251'
),
4 => Array
(
'date' => '2017-08-20',
'BBB' => '21773',
),
5 => Array
(
'date' => '2017-08-22',
'CCC' => '3750'
),
6 => Array
(
'date' => '2017-08-20',
'CCC' => '321750'
)
);
echo '<pre>';
$array1 = array('AAA' => null, 'BBB' => null, 'CCC' => null);
$array2 = array();
array_walk($array, function ($v) use (&$array2, $array1) {
$a = $v['date'];
if (!isset($array2[$a])) {
$array2[$a] = $array1;
}
unset($v['date']);
$array2[$a] = array_merge($array2[$a], $v);
});
print_r($array2);
Output
Array
(
[2017-08-22] => Array
(
[AAA] => 1231
[BBB] =>
[CCC] => 3750
)
[2017-08-21] => Array
(
[AAA] => 1172
[BBB] => 251
[CCC] =>
)
[2017-08-20] => Array
(
[AAA] => 1125
[BBB] => 21773
[CCC] => 321750
)
)
check output at: https://3v4l.org/NvLB8
Another approach (quick & dirty) making use of an arrays internal pointer:
$newArray = [];
foreach ($array as $childArray) {
$date = current($childArray);
$value = next($childArray); // this advances the internal pointer..
$key = key($childArray); // ..so that you get the correct key here
$newArray[$date][$key] = $value;
}
This of course only works with the given array structure.
Another perfect usage example for the PHP function array_reduce():
// The input array
$input = array(
0 => array(
'date' => '2017-08-22',
'AAA' => '1231',
),
// The rest of your array here...
);
$output = array_reduce(
$input,
function (array $carry, array $item) {
// Extract the date into a local variable for readability and speed
// It is used several times below
$date = $item['date'];
// Initialize the group for this date if it doesn't exist
if (! array_key_exists($date, $carry)) {
$carry[$date] = array();
}
// Remove the date from the item...
// ...and merge the rest into the group of this date
unset($item['date']);
$carry[$date] = array_merge($carry[$date], $item);
// Return the partial result
return $carry;
},
array()
);
The question is not clear. What is the expected result if one key (AAA f.e) is present on two or more dates? This answer keeps only the last value associated with it.
I have two arrays and I would like to join them. Both arrays are a product of a foreach loop. The first one being:
$cleanNums[] = array(
'01'=>$numbers[1],
'02'=>$numbers[2],
'03'=>$numbers[3],
'04'=>$numbers[4],
'05'=>$numbers[5],
);
and the second one being:
$newDates[] = array(
'day'=>$cleanDate[1],
'month'=>$cleanDate[2],
'year'=>$cleanDate[3],
'draw'=>$cleanDate[6],
);
Using array_merge $weeklyValues = array_merge($newDates,$cleanNums); I'm getting:
Array
(
[0] => Array
(
[day] => 1st
[month] => March
[year] => 2017
[draw] => 660
)
[1] => Array
(
[01] => 3
[02] => 23
[03] => 40
[04] => 20
[05] => 28
)
)
I would like my output to read as follows:
Array
(
[0] => Array
(
[day] => 1st
[month] => March
[year] => 2017
[draw] => 660
[01] => 3
[02] => 23
[03] => 40
[04] => 20
[05] => 28
)
)
Please use this code:
$resultArray = array(
0 => current($newDates) + current($cleanNums)
);
print_r($resultArray);
the array_merge is the way to do it http://php.net/manual/en/function.array-merge.php. The reason for incorrect output is in the way you define variables e.g.
$cleanNums[] = array(...)
Will result in nested array:
array(1) {
[0]=>
array(5) {
...
}
}
To avoid it either change the way of the assignment:
$cleanNums = array(...)
Or the way how you provide it as array_merge parameters:
$weeklyValues = array_merge($newDates[0],$cleanNums[0]);
The same needs to be applied for $newDates of course.
I'm assuming $cleanNums and $newDates are multidimensional arrays and will get more [] values;
$weeklyValues = array();
foreach($cleanNums as $array)
{
$weeklyValues = array_merge($weeklyValues,$array);
}
foreach($newDates as $array)
{
$weeklyValues = array_merge($weeklyValues,$array);
}
print_r($weeklyValues);
$count = count($newDates);
$mergedArray = array();
for($i=0; $i < $count; $i++){
//assuming both arrays have equal number of records
$mergedArray[] = current($newDates[$i]) + current($cleanNums[$i]);
}
var_dump($mergedArray);
This is my code
$pro_qty = '';
$se_pro = '';
$pro_id_nn = $this->getDataAll("SELECT session_pro_id,session_pro_qty FROM `jp_session` WHERE session_pro_id IN (".$pro_id.") AND order_status='3'");
foreach($pro_id_nn as $pro)
{
$pro_qty[] = $pro['session_pro_qty'];
$se_pro[] = $pro['session_pro_id'];
}
$proqty = array_combine($pro_qty,$se_pro);
echo '<br>';
print_r($se_pro);
echo '<br>';
print_r($pro_qty);
echo '<br>';
print_r($proqty);
OUTOUT
first array
$se_pro = Array ( [0] => 5 [1] => 1 [2] => 1 ) ;
second array
$pro_qty = Array ( [0] => 24 [1] => 24 [2] => 22 ) ;
Finally combine two array result is
$proqty = Array ( [5] => 24 [1] => 22 );
but my expecting result is
$proqty = Array ( [5] => 24 [1] => 24 [1] => 22 );
how can i get my expecting result . thanks in advance.
Your expected result is not possible, you cannot map one key (1) to two different values (24 and 22). Perhaps you should look at a different solution, such as a "jp_session" class which contains the two values, and then just store it in a list.
simple solution
foreach($pro_id_nn as $pro)
{
$pro_qty[$pro['session_pro_id']][] = $pro['session_pro_qty'];
}
Try this one
<?php
$se_pro = Array ( 0 => 5, 1 => 1, 2 => 1 ) ;
$pro_qty = Array ( 0 => 24, 1 => 24, 2 => 22 ) ;
$a=sizeof($se_pro);
for($i=0;$i<$a;$i++)
{
$b=$se_pro[$i];
$c=$pro_qty[$i];
$temp[$b]=$c;
$i++;
}
print_r($temp);
?>
But one condition '$se_pro' values not repeat and both array are same size
in array_combine() If two keys are the same, the second one prevails..
you can get the result like -
Array
(
[24] => Array
(
[0] => 5
[1] => 1
)
[22] => 3
)
the other way can be
$keys = array ( '24', '24', '22' );
$values = array ( '5', '1', '1' );
$output = array();
$size = sizeof($keys);
for ( $i = 0; $i < $size; $i++ ) {
if ( !isset($output[$keys[$i]]) ) {
$output[$keys[$i]] = array();
}
$output[$keys[$i]][] = $values[$i];
}
this will give the output like -
Array ( [24] => Array ( [0] => 5 [1] => 1 ) [22] => Array ( [0] => 1 ) )
or you can use
<?php
$keys = array ( '24', '24', '22' );
$values = array ( '5', '1', '1' );
function foo($key, $val) {
return array($key=>$val);
}
$arrResult = array_map('foo', $keys, $values);
print_r($arrResult);
?>
depending upon which output is more suitable for you to work with.
I'm in need of splitting of the single array into multiple array for some report generating purpose.
I have an array like this which I have given below.
Array
(
[blah_1] => 1
[blahblah_1] => 31
[blahblahblah_1] => 25
[blah_3] => 1
[blahblah_3] => 3
[blahblahblah_3] => 5
[blah_10] => 1
[blahblah_10] => 10
[blahblahblah_10] => 2
)
I want to split the above to,
Array
(
[blah_1] => 1
[blahblah_1] => 31
[blahblahblah_1] => 25
)
Array
(
[blah_3] => 1
[blahblah_3] => 3
[blahblahblah_3] => 5
)
Array
(
[blah_10] => 1
[blahblah_10] => 10
[blahblahblah_10] => 2
)
How can I do this in PHP ??
$oldarray=array('blah_1'=>1,'blahblah_1'=>31,'blahblahblah_1'=>25,
'blah_3'=>1,'blahblah_3'=>3,'blahblahblah_3'=>5,
'blah_10'=>1,'blahblah_10'=>10,'blahblahblah_10'=>2
)
$newarray=array();
foreach ($oldarray as $key=>$val) { //Loops through each element in your original array
$parts=array_reverse(explode('_',$key)); //Splits the key around _ and reverses
$newarray[$parts[0]][$key]=$val; //Gets the first part (the number) and adds the
//value to a new array based on this number.
}
The output will be:
Array (
[1]=>Array
(
[blah_1] => 1
[blahblah_1] => 31
[blahblahblah_1] => 25
)
[3]=>Array
(
[blah_3] => 1
[blahblah_3] => 3
[blahblahblah_3] => 5
)
[10]=>Array
(
[blah_10] => 1
[blahblah_10] => 10
[blahblahblah_10] => 2
)
)
Use array_chunk. Example:
<?php
$chunks = array_chunk($yourarray, 3);
print_r($chunks);
?>
Use array_chunk
i.e
$a = array(1,2,3,4,5,6,7);
var_dump(array_chunk($a, 3));
Not sure if you are looking for array_slice
but take a look at this example:
<?php
$input = array("a", "b", "c", "d", "e");
print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));
?>
will result in this:
Array
(
[0] => c
[1] => d
)
Array
(
[2] => c
[3] => d
)
array_chunk ( array $input , int $size);