I've got an array that is an array of arrays, ordered by date, as so:
Array
(
[1379167200] => Array
(
[110] => Introduction to Banking | Saturday, September 14, 2013 - 10:00am
)
[1380376800] => Array
(
[71] => Saving, Investing, Debt | Saturday, September 28, 2013 - 10:00am
)
[1381588200] => Array
(
[72] => Setting Personal Goals | Saturday, October 12, 2013 - 10:30am
)
[1382796000] => Array
(
[74] => Type of Account: What's Right for You? | Saturday, October 26, 2013 - 10:00am
)
[1384009200] => Array
(
[81] => Creating an Account: Learning to Budget | Saturday, November 09, 2013 - 10:00am
)
)
I want to keep the ordering (i.e., ordered by date), but preferably only include the most inner array items in a single array. Like so:
Array
(
[110] => Introduction to Banking | Saturday, September 14, 2013 - 10:00am
[71] => Saving, Investing, Debt | Saturday, September 28, 2013 - 10:00am
[72] => Setting Personal Goals | Saturday, October 12, 2013 - 10:30am
[74] => Type of Account: What's Right for You? | Saturday, October 26, 2013 - 10:00am
[81] => Creating an Account: Learning to Budget | Saturday, November 09, 2013 - 10:00am
)
How would this be possible? Would I need to convert the integers to strings or something?
I absolutely need to maintain the ordering by date, but I also need to maintain the relationship between the integer key and the value, both are used.
Iterate through array, create new array:
$newArray = Array();
foreach($array as $row) { // $row is subarray
$value = current($row); // first value in subarray "Itroduction to ..."
$key = key($row); // first key in subarray 101, 71, 72
$newArray[$key] = $value;
}
Demo;
http://codepad.viper-7.com/dd3PLV
Docs:
http://php.net/manual/en/function.current.php
http://php.net/manual/en/function.key.php
Related
I have the following array:
$avail = array($this->item->extraFields->RoomONEDateAvailable->value, $this->item->extraFields->RoomTWODateAvailable->value, $this->item->extraFields->RoomTHREEDateAvailable->value, $this->item->extraFields->RoomFOURDateAvailable->value, $this->item->extraFields->RoomFIVEDateAvailable->value, $this->item->extraFields->RoomSIXDateAvailable->value, $this->item->extraFields->RoomSEVENDateAvailable->value, $this->item->extraFields->RoomEIGHTDateAvailable->value, $this->item->extraFields->RoomNINEDateAvailable->value, $this->item->extraFields->RoomTENDateAvailable->value);
These are the actual values:
Array ( [0] => Thursday, 01 January 1970 [1] => Thursday, 03 September 2015 [2] => Thursday, 01 January 1970 [3] => Thursday, 01 January 1970 [4] => Thursday, 01 January 1970 [5] => Thursday, 01 January 1970 [6] => Thursday, 01 January 1970 [7] => Thursday, 01 January 1970 [8] => Thursday, 01 January 1970 [9] => Thursday, 01 January 1970 )
I need to remove all the 'Thursday, 01 January 1970' dates from the array, so it only contains valid dates, how can I do this please?
Try:
$avail = array_diff($avail, array('Thursday, 01 January 1970'));
you don't need to rebuild your array, you can remove array entries with unset()[reference].
update: you don't need a reference here, you can work with the corresponding array directly:
foreach($avail as $key => $val)
{
if($val == "Thursday, 01, January 1970")
unset($avail[$key]);
}
var_dump($avail);
this is tested and should work for you.
You can use the array_keys() functions, adding the option to search for a specific value, like this:
array_keys($array, "Thursday, 01 January 1970")
It will return an array of matching index, which you should unset from the original array.
what i need
I Need to group array of same type.
like array1 ['type']=a and array2 ['type']=a then it should be grouped in single array.
array structure
Array
(
[1] => Array
(
[type] => ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Regular
[amount] => 850
[comment] => On or Before June 28, 2014
)
[2] => Array
(
[type] => ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Friend
[amount] => 1000
[comment] => On or Before June 28, 2014
)
[3] => Array
(
[type] => ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Student
[amount] => 500
[comment] => On or Before June 28, 2014
)
[4] => Array
(
[type] => ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Regular)
[amount] => 990
[comment] => June 29 thru July 20, 2014
)
PHP Code
foreach($data as $k=>$v){
$type[$v['type']][]=$k;
}
//loop types, creating result array
foreach($type as $k=>$v){
$tmp=array(
'type'=>$kk,
'metadata'=>array()
);
//loop all the arrays of this type
foreach($v as $w){
//store in TMP
$t=array(
'amount' => $data[$w]['amount'],
'comment' => $data[$w]['comment']
);
//store
$tmp['metadata'][]=$t;
}
$result[]=$tmp;
}
output of type
[ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Regular] => Array
(
[0] => 1
[1] => 7
)
[ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Friend] => Array
(
[0] => 2
[1] => 5
[2] => 8
)
[ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Student] => Array
(
[0] => 3
[1] => 6
[2] => 9
)
[ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Regular)] => Array
(
[0] => 4
)
Problem im getting
[ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Regular] => Array
(
[0] => 1
[1] => 7
)
[ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Regular)] => Array
(
[0] => 4
)
this array having same type but is not grouped.
i want ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Regular to grouped in single array .
how to tackle this problem any suggestion are most welcome.
Most likely because of that superfluous ) on that other value:
ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Regular
ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Regular)
// extra `)` on the end
Alternatively, you could simply fix it by trimming it:
foreach($data as $k => $v){
$type_val = rtrim($v['type'], ')');
$type[$type_val][] = $k;
}
You could push them and group them like this:
$new_data = array();
foreach($data as $k => $v){
$type_val = rtrim($v['type'], ')');
// simple initialize
if(!isset($new_data[$type_val])) {
$new_data[$type_val] = array(
'amount' => 0,
'comment' => array(),
);
}
// push values
$new_data[$type_val]['amount'] += $v['amount'];
$new_data[$type_val]['comment'][] = $v['comment'] . "\n";
}
echo '<pre>';
print_r($new_data);
Simple Output of the snippet above
Both type are not the same
ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Regular
ECOOP Conference, Workshops and UPMARC Summer School (Mon-Fri July 28 - August 1) Regular) // extra `)` on the end
code for group them
$newArr = array();
foreach($array as $key=>$value) {
$newArr[$value['type']][] = $value;
}
I have a bunch of strings like this, for example:
Time: 22:30 (25) | Date: 19 March 2011 | Contributor: Salesman
Now, I want to extract the date and the string after Contributor, i.e, Salesman.
Currently, I am using explode() function in PHP.
But the problem is, the string has many variations like:
Time: 22:30 (25) | Date: 19 March 2011
Time: 22:30 (25) | Date: 2011 | Contributor: Salesman
Time: 22:30 (25) | Contributor: Salesman
Time: 22:30 (25) | Date: 2011
I want something that works perfect for all the variations. Where a field is unavailable, I shall consider it as NULL. For full date I need to store the date in database, and for only year I shall save the year.
Suggest me some code for this problem, or a regular expression in PHP if this problem can be solved through it.
Time:.*?\|\s*(?:Date:\s*([0-9a-zA-Z ]+))?\|?\s*(?:Contributor:\s*([a-zA-Z0-9 ]+))?
Try this.This will only give groups available.
See demo.
http://regex101.com/r/nG1gU7/18
You could try something like this:
$string = 'Time: 22:30 (25) | Date: 19 March 2011';
function str_func($string, $key, $remove_key = FALSE)
{
$string = trim($string);
if ($string[strlen($string) - 1] !== '|') {
$string .= '|';
}
$pos = strpos(strtolower($string), strtolower($key) . ':');
if ($pos !== FALSE) {
$return = strstr(substr($string, $pos), '|', true);
return ($remove_key) ? trim(substr($return, strlen($key) + 1)) : $return;
} else {
return NULL;
}
}
So just pass it the string and 'Time' or 'Contributor' (and optionally whether you'd like the key to be removed) e.g.
//Will return Time: 22:30 (25)
echo str_func($string, 'time');
//Will remove 'date: ' from the returned string
echo str_func($string, 'date', TRUE);
Hope this helps!
I think you want something like this,
^.*?\|\s*\K(?:Date:\s*((?:\d{2}\s*\S+)?\s*\d{4})\s*\|?)?\s*(?:Contributor:\s*(\S+))?
DEMO
Group index 1 contains the value of Date: field and group index 2 contains the value of Contributor: field.
Code:
<?php
$data = <<< EOT
Time: 22:30 (25) | Date: 19 March 2011
Time: 22:30 (25) | Date: 2011 | Contributor: Salesman
Time: 22:30 (25) | Contributor: Salesman
Time: 22:30 (25) | Date: 2011
EOT;
$regex = '~^(?:.*?\|\s*)(Date:\s*((?:\d{2} \S+ )?\d{4}))?(?:\s\|\s*)?(Contributor:\s*(\S+))?\s*$~m';
preg_match_all($regex, $data, $matches);
print_r($matches);
?>
Output:
Array
(
[0] => Array
(
[0] => Time: 22:30 (25) | Date: 19 March 2011
[1] => Time: 22:30 (25) | Date: 2011 | Contributor: Salesman
[2] => Time: 22:30 (25) | Contributor: Salesman
[3] => Time: 22:30 (25) | Date: 2011
)
[1] => Array
(
[0] => Date: 19 March 2011
[1] => Date: 2011
[2] =>
[3] => Date: 2011
)
[2] => Array
(
[0] => 19 March 2011
[1] => 2011
[2] =>
[3] => 2011
)
[3] => Array
(
[0] =>
[1] => Contributor: Salesman
[2] => Contributor: Salesman
[3] =>
)
[4] => Array
(
[0] =>
[1] => Salesman
[2] => Salesman
[3] =>
)
)
I have an array that looks like this and I want to display it in a more readable format. I would like the bird name (Gray Hawk etc) and then each listing (which the number of result will vary. This is the code I currently have:
$xml = simplexml_load_file($noteable);
$result = array();
foreach ($xml->result->sighting as $sighting) {
$location = (string) $sighting->{'loc-name'};
$bird = (string) $sighting->{'com-name'};
$howMany = (string) $sighting->{'how-many'};
$obsdt = (string) $sighting->{'obs-dt'};
$thenotedate = $obsdt;
$thenotedate = split('T',$thenotedate);
$thenotedate = $thenotedate[0];
$thenotedate = strftime('%a %b %e at %I:%M %p',strtotime($thenotedate));
ksort($result);
if (!isset($result[$bird])) $result[$bird] = array();
$result[$bird][] = $howMany . ' seen at ' . $location . ' on ' . $thenotedate;
}
print"<pre>";
print_r($result);
print"</pre>";
}
And this is the array
[Gray Hawk] => Array
(
[0] => 1 seen at Florida Canyon--lower on Sun Jun 2 at 04:50 PM
[1] => 1 seen at Madera Canyon--Whitehouse Picnic area on Sat Jun 1 at 07:30 AM
[2] => 1 seen at Florida Canyon--lower on Thu May 30 at 07:56 AM
[3] => 1 seen at Florida Canyon--lower on Wed May 29 at 07:40 AM
[4] => 1 seen at Florida Canyon--lower on Wed May 29 at 07:37 AM
[5] => 1 seen at Madera Canyon--Madera Picnic Area on Tue May 28 at 04:45 PM
[6] => 1 seen at Madera Canyon--Proctor Rd. on Mon May 27 at 09:40 AM
)
[MacGillivray's Warbler] => Array
(
[0] => 1 seen at Madera Canyon--Proctor Rd. on Sat May 25 at 05:45 PM
[1] => 1 seen at Madera Canyon--Proctor Rd. on Sat May 25 at 05:45 PM
[2] => 1 seen at Madera Canyon--Proctor Rd. on Sat May 25 at 05:30 PM
)
try something like this -
foreach ($result as $key=>$value){
//echo Bird Name
echo "<h3>$key</h3>";
//start an unordered list
echo "<ul>";
//echo each sighting
foreach($value as $row){
echo "<li>$row</li>";
}
//close the unordered list
echo "</ul>";
}
when printing out the result don't use print_r($result); use a loop which returns each element in the array! If you need infos on how to display the array as you want to tell us exactly how do you want it to be outputted
i have array like below which is sorted by array_count_values function,
Array
(
[Session #1: Tues May 31st - Fri June 10th (1-5:30PM)#1 only: 1pmADV] => 3
[Session #2: Tues June 14th - Fri June 24th (9-2:00PM)#1 only: 2pmADV] => 2
[Session #4: Tues July 12th - Fri July 22nd (9-2:00PM)#1 only: 1:30pmADV] => 2
)
i need to convert this array into array of object like below,
stdClass Object
(
[Itemname] => Session #1: Tues May 31st - Fri June 10th (1-5:30PM)#1 only: 1pmADV
[Quantity] => 3
)
stdClass Object
(
[Itemname] => Session #2: Tues June 14th - Fri June 24th (9-2:00PM)#1 only: 2pmADV
[Quantity] => 2
)
stdClass Object
(
[Itemname] => Session #4: Tues July 12th - Fri July 22nd (9-2:00PM)#1 only: 1:30pmADV
[Quantity] => 2
)
how can i do this?
$arrayOfObjects = array();
foreach ($values as $Itemname => $Quantity) {
$arrayOfObjects[] = (object)compact('Itemname', 'Quantity');
}
Something like this?:
foreach($array as $key => $value){
$object = new stdClass;
$object->Itemname = $key;
$object->Quantity = $value;
print_r($object);
}