Currently I have an array like :
Array(
[0] => Array([range]=>1-10 [count]=>3 [type]=>A)
[1] => Array([range]=>11-20 [count]=>6 [type]=>A)
[2] => Array([range]=>21-30 [count]=>5 [type]=>A)
[3] => Array([range]=>1-10 [count]=>5 [type]=>B)
[4] => Array([range]=>11-20 [count]=>3 [type]=>B)
[5] => Array([range]=>21-30 [count]=>8 [type]=>B)
[6] => Array([range]=>1-10 [count]=>4 [type]=>C)
[7] => Array([range]=>11-20 [count]=>3 [type]=>C)
[8] => Array([range]=>21-30 [count]=>6 [type]=>C)
[9] => Array([range]=>1-10 [count]=>3 [type]=>D)
[10] => Array([range]=>11-20 [count]=>7 [type]=>D)
And then I am trying to regroup/remake the array depends on their type and the expected output would be like :
Array(
[0] => Array([type]=>A [1-10]=>3 [11-20]=>6 [21-30]=>5)
[1] => Array([type]=>B [1-10]=>5 [11-20]=>3 [21-30]=>8)
[2] => Array([type]=>C [1-10]=>4 [11-20]=>3 [21-30]=>6)
[3] => Array([type]=>D [1-10]=>3 [11-20]=>7)
)
I have tried array_column but isn't what exactly I want...
Example Here.
Thanks in advance.
This should work for you:
Here I simply loop through the entire array and then check with isset() if the result array already has an innerArray with the same type (e.g $result["A"]), if not I add the type as value to the inner array (.e.g. $result["A"]["type"] = "A";).
After this check I simply add the range and count to each type (e.g. $result["A"]["1-10"] = 3;)
At the end I simply reindex the entire $result array with array_values().
<?php
foreach($arr as $k => $v) {
if(!isset($result[$v["type"]]))
$result[$v["type"]]["type"] = $v["type"];
$result[$v["type"]][$v["range"]] = $v["count"];
}
$result = array_values($result);
print_r($result);
?>
output:
Array
(
[0] => Array
(
[type] => A
[1-10] => 3
[11-20] => 6
[21-30] => 5
)
//...
)
Related
I have here an array below:
<?php
print_r( $result );
?>
If I am going to execute the code above, it resulted below:
Array
(
[0] => Array
(
[id] => 1
[uploaded_by] => 1
[image_url] => http://localhost/dir/img_2.jpg
[work_description] => test
[date_added] => 2017-08-03 02:12:38
)
[1] => Array
(
[id] => 2
[uploaded_by] => 1
[image_url] => http://localhost/dir/img_4.jpg
[work_description] => test
[date_added] => 2017-08-03 02:13:04
)
[2] => Array
(
[id] => 3
[uploaded_by] => 1
[image_url] => http://localhost/dir/img_2.jpg
[work_description] => test
[date_added] => 2017-08-03 02:46:28
)
[3] => Array
(
[id] => 4
[uploaded_by] => 1
[image_url] => http://localhost/dir/img_2.jpg
[work_description] => sdfsdf
[date_added] => 2017-08-03 02:46:34
)
)
Now, from the $result array I wanted to change the values of all image_url
programmatically using php into an image in html.
example:
http://localhost/dir/img_2.jpg will become
<img src="http://localhost/dir/img_2.jpg"/>
Those values must be changed if I am going to execute the code.
Does anybody know?
You can put it in a foreach and modify only the part what you want:
foreach($result as $key => $value) {
$result[$key]['image_url'] = '<img src="'.$value['image_url'].'"/>';
}
print_r($result);
You can use a compact syntax and modify the subarray elements by reference:
Code: (Demo)
foreach ($result as &$subarray) { // & means modify by reference, so you are overwriting the input array, not traversing a copy.
$subarray['image_url'] = "<img src=\"{$subarray['image_url']}\"/>";
}
var_export($result);
There is no need to declare $key because the foreach is traversing the actual input array, not a copy of the input array. The image_urls are simply overwritten with each iteration.
I have been trying to work this out for two days now and am hitting a brick wall. I have a skyscanner array that has flight itineraries where I have the flight
Leg - being Itineraries -> OutboundLegId -
and also the legs which shows the flight number - being
Legs -> FlightNumbers -> FlightNumber.
What I am trying to achieve is to display the Itinerary and then join the Flight Number on to that. But no matter what I try I cannot get this to work. I have read all about Keys on here and tried loads of examples but am coming up with nothing. Can someone point me in the right direction please?
Example arrays below
[Itineraries] => Array
(
[0] => Array
(
[OutboundLegId] => 13542-1610140610-29-0-13445-1610141240
[InboundLegId] => 13445-1610211340-29-0-13542-1610211640
[PricingOptions] => Array
(
[0] => Array
(
[Agents] => Array
(
[0] => 2174187
)
[QuoteAgeInMinutes] => 31
[Price] => 200.98
[DeeplinkUrl] => http://partners.api.skyscanner.net/apiservices/deeplink/v2?_cje=5JlLCgyPUKY0hT8T0Ybh6dL0Xf0htAiHTFX7RU79eeI3XvrsxvEqP1QUJAoHiHRd&url=http%3a%2f%2fwww.apideeplink.com%2ftransport_deeplink%2f4.0%2fUK%2fen-gb%2fGBP%2fcook%2f2%2f13542.13445.2016-10-14%2c13445.13542.2016-10-21%2fair%2fairli%2fflights%3fitinerary%3dflight%7c-32294%7c1152%7c13542%7c2016-10-14T06%3a10%7c13445%7c2016-10-14T12%3a40%2cflight%7c-32294%7c1153%7c13445%7c2016-10-21T13%3a40%7c13542%7c2016-10-21T16%3a40%26carriers%3d-32294%26passengers%3d1%2c0%2c0%26channel%3ddataapi%26cabin_class%3deconomy%26facilitated%3dfalse%26ticket_price%3d200.98%26is_npt%3dfalse%26is_multipart%3dfalse%26client_id%3dskyscanner_b2b%26request_id%3d3bc96bda-fd7c-403a-b841-2ccc3c26071d%26commercial_filters%3dfalse%26q_datetime_utc%3d2016-09-29T08%3a18%3a27
)
[Legs] => Array
(
[0] => Array
(
[Id] => 13542-1610140610-29-0-13445-1610141240
[SegmentIds] => Array
(
[0] => 1
)
[OriginStation] => 13542
[DestinationStation] => 13445
[Departure] => 2016-10-14T06:10:00
[Arrival] => 2016-10-14T12:40:00
[Duration] => 270
[JourneyMode] => Flight
[Stops] => Array
(
)
[Carriers] => Array
(
[0] => 105
)
[OperatingCarriers] => Array
(
[0] => 105
)
[Directionality] => Outbound
[FlightNumbers] => Array
(
[0] => Array
(
[FlightNumber] => 1152
[CarrierId] => 105
)
)
)
Assuming this is one big array and its called $data you can nest a couple of foreach loops.
I use foreach loops as I assume there are cases where this data structure get more complex than the one you show
foreach ( $data['Itineraries'] as $itin ) {
foreach ( $data['Legs'] as $legs) {
if ($legs['Id'] == $itin['OutboundLegId']) {
// we matched the itinerary with a leg
echo $legs['OutboundLegId'] . ' ' . $legs['FlightNumbers'][0]['FlightNumber'];
}
}
}
Use it as draft. Can't perform function without feedback.
Put proper arrays instead of {YOUR-ARRAY-WITH-LEGS} and {YOUR-ARRAY-WITH-ITINERARIES}
$sortedLegs = array_column('Id', {YOUR-ARRAY-WITH-LEGS});
$joinedArray = array_map(function($itinerary) use($sortedLegs){
if(array_key_exists($itinerary['OutboundLegId'],$sortedLegs)) {
$itinerary['legs'] = $sortedLegs[$itinerary['OutboundLegId']];
}
return $itinerary;
},{YOUR-ARRAY-WITH-ITINERARIES});
I have this array:
Array ( [0] => ../files/flv/1 [1] => ../files/flv/10 [2] => ../files/flv/2 [3] => ../files/flv/3 [4] => ../files/flv/4 [5] => ../files/flv/5 [6] => ../files/flv/6 [7] => ../files/flv/7 [8] => ../files/flv/8 [9] => ../files/flv/9 )
I need to sort it this way:
Array ( [0] => ../files/flv/1 [1] => ../files/flv/2 [2] => ../files/flv/3 [3] => ../files/flv/4 [4] => ../files/flv/5 [5] => ../files/flv/6 [6] => ../files/flv/7 [7] => ../files/flv/8 [8] => ../files/flv/9 [9] => ../files/flv/10 )
I tried to use sort($array,SORT_NUMERIC);, but no luck because of this prefix ../files/flv/
I know only this solution: $array2 = array_map('basename', $array); and then sort($array2,SORT_NUMERIC);
Is there any other solutions not so complex?
Use SORT_NATURAL instead of SORT_NUMERIC (requires PHP 5.4.0 or latest):
sort($array, SORT_NATURAL);
EDIT: I used this code to test it:
$array = array(
'../files/flv/1',
'../files/flv/10',
'../files/flv/2'
);
sort($array, SORT_NATURAL);
print_r($array);
It outputs:
Array
(
[0] => ../files/flv/1
[1] => ../files/flv/2
[2] => ../files/flv/10
)
EDIT 2: Alternatively you can use the natsort() function, it works on older versions too:
natsort($array);
If "../files/flv/" path is always same you can try using str_replace function on all elements and then do sorting using sort numeric and later add same path back to all elements.
So three steps are:
Use foreach loop over array and use str_replace() function on ecah element.
do sorting as you did using sort numeric.
Use foreach loop over array and put the constant path back as prefix.
To make it more perfect you can do this by creating your own function and pass path as parameter.
Have you tried without the SORT_NUMERIC flag?
I have a array like this
[0] => Array
(
[0] => LBLdss_COLLEsdfCTIONS_RsdfsdEPORT_TITfsdfLE
[1] =>
[2] =>
[3] => Array
(
[Administration] => Array
(
[bidsflldsf6] => Array
(
[0] => themes/Care2012/images/Payments
[1] => LsdfBL_OPsddfD_BIsfdfsLLING_SsdfdsUMsdfMARY_TITLE
[2] => LsdfsdBL_OPDfdsfd_BILfdsLING_dsfdsSUMMARY
[3] => ./index.php?module=Rsdfepfdsforts&action=reposfdfdsrtsel&rname=Opdpasfdfypdf
)
[bilhghjgl_pat_reg] => Array
(
[0] => themsdfsdes/Casfdfre2aasd012/imasdfges/Pasdymesddfnts
[1] => LBL_sdfsPAT_RsdfEG_TsdfITLE
[2] => LBL_PdfsdAT_sfdREG_TdsfdITLE_DsdfsETAIL
[3] => ./index.php?module=Rexcvpofdsrts&action=reportsel&rname=Pacxvtregcollxcvectionpdf
)
)
)
[4] =>
)
Now i need to extract value of rname from this index [3] => ./index.php?module=Rexcvpofdsrts&action=reportsel&rname=Pacxvtregcollxcvectionpdf (which is Pacxvtregcollxcvectionpdf in this case)and have to save it
I can try explode function but it is heavy for me since my array size is large
please help in resolving this
Thanks
Try placing a foreach for ciclying the array and formulate a regular expression for extract your param. Like this:
foreach($youvar as $text) {
preg_match('$rname=(.+?)$', $text, $rname[])
}
After that you can try a print_r on $rname and adapt to you.
Try below :-
$a = explode("rname=", index[3]);
echo $a[1;]
Code
$array = array(
'./index.php?module=Rsdfepfdsforts&action=reposfdfdsrtsel&rname=Opdpasfdfypdf',
'./index.php?module=Rexcvpofdsrts&action=reportsel&rname=Pacxvtregcollxcvectionpdf',
);
function getRname($str){
//return null if not found
preg_match('/rname\=([a-zA-Z]+)($|&)/', $str , $matches);
return $matches[1];
}
foreach($array as $str){
echo "Rname: " . getRname($str). "<br/>";
}
Result
Rname: Opdpasfdfypdf
Rname: Pacxvtregcollxcvectionpdf
Try it?
I have this array, $array :
Array
(
[0] => http://download.server.com/18821_SM_139.jpg
[1] => http://download.server.com/18821_SM_134.jpg
[2] => http://download.server.com/18821_SM_138.jpg
[3] => http://download.server.com/18821_SM_138.jpg
[4] => http://download.server.com/18821_ABS_132.jpg
[5] => http://download.server.com/18821_SM_138.jpg
)
and in this case, I am looking for any line that has ABS inside.
I could look for that by using the regexp http://.+ABS.+, and this will select the entire line.
But I still need to remove it from the array, not just replace it (or leave it empty.) But in this case, the array will become:
Array
(
[0] => http://download.server.com/18821_SM_139.jpg
[1] => http://download.server.com/18821_SM_134.jpg
[2] => http://download.server.com/18821_SM_138.jpg
[3] => http://download.server.com/18821_SM_138.jpg
[4] => http://download.server.com/18821_SM_138.jpg
)
Any ideas what method i need to use?
Thanks.
edit:
i am using OOP php
Use array_filter() with a custom callback.
Example:
function testABS($elem) {
return strpos($elem, 'ABS') === false;
}
print_r(array_filter($the_array, 'testABS'));
Note: This is a contrived example. You will need to adjust the logic in the callback function to meet your needs.
There's preg_grep:
$abs = preg_grep('/ABS/', $your_array);
and returns the matches as an array. It also has a flag to return only the non-matching entries, which is probably what you want: return all entries which do NOT have 'abs' in them.
What i understood is u want to remove that element from the array.
can do like this.
$arr = Array
(
[0] => http://download.server.com/18821_SM_139.jpg
[1] => http://download.server.com/18821_SM_134.jpg
[2] => http://download.server.com/18821_SM_138.jpg
[3] => http://download.server.com/18821_SM_138.jpg
[4] => http://download.server.com/18821_ABS_132.jpg
[5] => http://download.server.com/18821_SM_138.jpg
);
$new_arr = array();
foreach($arr as $link){
if(!preg_match('ABS', $link)){
$new_arr[] = $link;
}
}
//ths will give array with only 4 elements as '18821_ABS_132.jpg' will be removed
return $new_arr;