If I have an array:
Array
(
[0] => Array
(
[0] => |174|September|2001|
[1] => |Pengantar=Hello!!!!
[2] => |Tema= Sami Mawon
[3] => |Tema_isi=meet you!!!
[4] => |Kutip=people
[5] => |Kutip_kitab=Efesus
[6] => |Kutip_pasal=4
[7] => |Kutip_ayat=28
[8] => |Tema_sumber=Kiriman dari Maurits albert (romind# )
[9] => [[Kategori:e-humor 2001]]
)
How can I get the value of Pengantar, Tema, Tema_isi etc?
You're going to have to loop over the array and use preg_match with a reference.
Regex something like this (off the top of my head) would probably work:
/\|(.*?)=(.*?)|?/
Just use preg_match('/\|(.*?)=(.*?)|?/', $subject[$x], $matches); and var_dump($matches); to see the results.
Don't forget that the $matches array passed into the preg_match function is a reference to an array which you should instantiate first and that it will be overwritten in each loop cycle.
just use array walk, a matching reg ex and a lambda function:
$array = array(
array(
'|174|September|2001|',
'Pengantar=Hello!!!!',
'Tema= Sami Mawon ',
'Kategori:e-humor 2001',
[...]
)
);
$values = array();
array_walk($array[0],function(&$item1, $key) use(&$values) {
if(preg_match('#[^=]=(.+)#',$item1,$match)){
$values[] = $match[1];
}
});
print_r($values);
The regular expression can be like this, using the named subpattern of "preg_match()":-
$a = array(
array(
'|174|September|2001|',
'|Pengantar=Hello!!!!',
'|Tema= Sami Mawon',
'|Tema_isi=meet you!!!',
'|Kutip=people',
'|Kutip_kitab=Efesus',
'|Kutip_pasal=4',
'|Kutip_ayat=28',
'|Tema_sumber=Kiriman dari Maurits albert (romind# )',
'[[Kategori:e-humor 2001]]',
)
);
$pattern = '/(?<first>\w+)[:=](?<rest>[\d|\w|\s]+)/';
$matches = array();
foreach ($a as $_arrEach) {
foreach ($_arrEach as $_each) {
$result = preg_match($pattern, $_each, $matches[]);
}
}
echo "<pre>";
print_r($matches);
echo "</pre>";
You will find that the array key "first" satisfies your requirement.
The above will output as:-
Array
(
[0] => Array
(
)
[1] => Array
(
[0] => Pengantar=Hello
[first] => Pengantar
[1] => Pengantar
[rest] => Hello
[2] => Hello
)
[2] => Array
(
[0] => Tema= Sami Mawon
[first] => Tema
[1] => Tema
[rest] => Sami Mawon
[2] => Sami Mawon
)
[3] => Array
(
[0] => Tema_isi=meet you
[first] => Tema_isi
[1] => Tema_isi
[rest] => meet you
[2] => meet you
)
[4] => Array
(
[0] => Kutip=people
[first] => Kutip
[1] => Kutip
[rest] => people
[2] => people
)
[5] => Array
(
[0] => Kutip_kitab=Efesus
[first] => Kutip_kitab
[1] => Kutip_kitab
[rest] => Efesus
[2] => Efesus
)
[6] => Array
(
[0] => Kutip_pasal=4
[first] => Kutip_pasal
[1] => Kutip_pasal
[rest] => 4
[2] => 4
)
[7] => Array
(
[0] => Kutip_ayat=28
[first] => Kutip_ayat
[1] => Kutip_ayat
[rest] => 28
[2] => 28
)
[8] => Array
(
[0] => Tema_sumber=Kiriman dari Maurits albert
[first] => Tema_sumber
[1] => Tema_sumber
[rest] => Kiriman dari Maurits albert
[2] => Kiriman dari Maurits albert
)
[9] => Array
(
[0] => Kategori:e
[first] => Kategori
[1] => Kategori
[rest] => e
[2] => e
)
)
Hope it helps.
Apart from the question of "which pattern", I suggest you take a look into preg_replaceDocs which is able to operate on arrays directly.
$pattern = "...";
$matches = preg_replace($pattern, '\1', $array);
It is not clear from the question, but it looks like you want preg_filter.
This function can perform a regex match and replace on an array, returning a new array containing only the replaced values of the matched items.
Related
I want to remove duplicate data in my array.
That's my array :
$monTableau = array (
array("pomme","noix de coco","pêche"),
array("fraise","pomme", "framboise"),
array("ananas","citron","raisin"),
array("pêche","pruneau","pomme")
);
My multidimensionnal array :
Array
(
[0] => Array
(
[0] => pomme
[1] => noix de coco
[2] => pêche
)
[1] => Array
(
[0] => fraise
[1] => pomme
[2] => framboise
)
[2] => Array
(
[0] => ananas
[1] => citron
[2] => raisin
)
[3] => Array
(
[0] => pêche
[1] => pruneau
[2] => pomme
)
)
and that's my code to try remove duplicate data:
$monTableau = array_map("unserialize", array_unique(array_map("serialize", $monTableau)));
Don't work unfortunately :(
Advance Thanks,
Your code does work properly, but there is no duplicates in the main array. If you want one single array as output that contains only unique elements of the whole list take a look at this code:
$monTableau = array (
array("pomme","noix de coco","pêche"),
array("fraise","pomme", "framboise"),
array("ananas","citron","raisin"),
array("pêche","pruneau","pomme")
);
$merged = call_user_func_array('array_merge', $monTableau);
$unique = array_unique($merged);
Output of $unique:
Array
(
[0] => pomme
[1] => noix de coco
[2] => pêche
[3] => fraise
[5] => framboise
[6] => ananas
[7] => citron
[8] => raisin
[10] => pruneau
)
Duplicates of pomme have been removed
I have two arrays that was converted from csv file.
The first csv line looks like this:
franchise_id,franchise_name,phone,website,email,region_codes;
1,"Abbott, Hackett and O`Conner",1-648-177-9510,auto-service.co/bw-319-x,Lupe-2485#auto-service.co,"36101,36055,36071";
The second csv line looks like this:
postal_code,region_code,city,state,region;
14410,36055,Adams Basin,NY,Monroe;
I converted these lines to arrays like this:
//Region Array
$region_lines = explode(PHP_EOL, $region_mappings_string);
$region_array = array();
foreach ($region_lines as $region_line) {
$region_array[] = str_getcsv($region_line);
}
//Franchise Array
$franchise_lines = explode(PHP_EOL, $franchises_string);
$franchise_array = array();
foreach ($franchise_lines as $franchise_line) {
$franchise_array[] = str_getcsv($franchise_line);
}
After that I got the result like this for Region:
Array
(
[0] => Array
(
[0] => postal_code
[1] => region_code
[2] => city
[3] => state
[4] => region;
)
[111] => Array
(
[0] => 14410
[1] => 36055
[2] => Adams Basin
[3] => NY
[4] => Monroe;
)
[112] => Array
(
[0] => 14617
[1] => 36055
[2] => Rochester
[3] => NY
[4] => Monroe;
)
And for franchise:
Array
(
[0] => Array
(
[0] => franchise_id
[1] => franchise_name
[2] => phone
[3] => website
[4] => email
[5] => region_codes;
)
[1] => Array
(
[0] => 1
[1] => Abbott, Hackett and O`Conner
[2] => 1-648-177-9510
[3] => auto-service.co/bw-319-x
[4] => Lupe-2485#auto-service.co
[5] => 36101,36055,36071;
)
What I need to do is to look for 14410 postal code from the first array, get the region_code and look for this region_code in second array and then output the results in PHP. How can this be done?
Will have to loop over franchises array and run simple comparison
function getDataByRegionCode($code, $franchiseArray) {
foreach ($franchiseArray as $key => $data) {
if(isset($data[5])){
$codes = explode(',',$data[5]);
if(in_array($code,$codes)) return $data;
}
}
return NULL;
}
print_r(getDataByRegionCode(14410,$franchiseArray));
I want to build a array or multiple array by breaking the main array , and my array is like ,
Array
(
[0] => string1
[1] => 1
[2] => 2
[3] => 3
[4] => 66
[5] => 34
[6] => string1
[7] => aww
[8] => brr
[9] => string3
[10] => xas
)
So basically by the value 'string1' i want to make a new array or first array which has only those three values (1,2,3) and same for string2 and string3,So each array has its values(three).
Please help me to build this.
Note: those all string names will be static.
Thank you in advance.
Result should me like:
string1 array:
<pre>Array
(
[1] => 1
[2] => 2
[3] => 3
[4] => 66
[5] => 34
)
string2 array:
<pre>Array
(
[1] => aww
[2] => brr
)
string3 array:
<pre>Array
(
[1] => xas
)
This I think will get you what you want.
It does assume that the first entry in the old array will be a keyword!
$old = array('string1',1,2,3,66,34,'string2','aww','brr','string3','xas');
$new = array();
$keywords = array('string1', 'string2', 'string3');
$last_keyword = '';
foreach ($old as $o) {
if ( in_array($o, $keywords) ) {
$last_keyword = $o;
} else {
$new[$last_keyword][] = $o;
}
}
print_r($new);
It creates a new array like this
Array
(
[string1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 66
[4] => 34
)
[string2] => Array
(
[0] => aww
[1] => brr
)
[string3] => Array
(
[0] => xas
)
)
However I still maintain that it would be better to go back to where the original array gets created and look to amend that process rather than write a fixup for it
I declare the following array
$job_scope = array( "proposal_id",
"will_provide" => array("0","Supervision","Labor","Material","Equpment"),
"general_scope",
"per_bid" => array("Yes","No","Omit"),
"job_type" => array("Painting","Sandblasting","Scappling")
);
I expect it to be created like
array([0] => 'proposal_id',
[1] => 'will_provide' => array([0] => "0",
[1] => "Supervision",
[2] => "Labor",
[3] => "Material",
[4] => "Equpment"),
[2] => 'general_scope',
[3] => 'per_bid' => array([0] => "Yes",
[1] => "No",
[2] => "Omit"),
[4] => 'job_type' => array([0] => "Painting",
[1] => "Sandblasting",
[2] => "Scappling")
But when I print the array it looks like
Array ( [0] => proposal_id [will_provide] => Array (
[0] => 0
[1] => Supervision
[2] => Labor
[3] => Material
[4] => Equpment )
[1] => general_scope [per_bid] => Array (
[0] => Yes
[1] => No
[2] => Omit )
[job_type] => Array (
[0] => Painting
[1] => Sandblasting
[2] => Scappling )
I would like the array to be created in the same format as the second section of code.
All you need to do is assign an empty array to the proposal_id and general_scope. So the code will look like this
$job_scope = array( "proposal_id" => array(),
"will_provide" => array("0","Supervision","Labor","Material","Equpment"),
"general_scope" => array(),
"per_bid" => array("Yes","No","Omit"),
"job_type" => array("Painting","Sandblasting","Scappling")
);
It will produce this array
Array (
[proposal_id] => Array ( )
[will_provide] => Array ( [0] => 0
[1] => Supervision
[2] => Labor
[3] => Material
[4] => Equpment
)
[general_scope] => Array ( )
[per_bid] => Array ( [0] => Yes
[1] => No
[2] => Omit
)
[job_type] => Array ( [0] => Painting
[1] => Sandblasting
[2] => Scappling
))
If you want to callback the value, (ex : call supervision value).
All you need to do is
print_r($job_scope['will_provide'][1])
and that will print the supervision value
use $new_job_scope = array_values($job_scope);
$job_scope = array(
"proposal_id",
"will_provide" => array(
"0",
"Supervision",
"Labor",
"Material",
"Equpment"
),
"general_scope",
"per_bid" => array(
"Yes",
"No",
"Omit"
),
"job_type" => array(
"Painting",
"Sandblasting",
"Scappling"
)
);
$new_job_scope = array_values($job_scope);
print_r($new_job_scope);
PhpFiddle
Create array first !!! Reassign at specified index with 2D array will be more clear to me
<?php
$arr = array('proposal_id','','general_scope','',''); //create array first
$arr[1] = array("will_provide" => array("0","Supervision","Labor","Material","Equpment"));
$arr[3] = array("per_bid" => array("Yes","No", "Omit"));
$arr[4] = array("job_type" => array("Painting","Sandblasting","Scappling"));
var_dump($arr);
?>
I think this process can serve you. I have just used a foreach loop to convert non-int key to int key:
$new_array = '';
foreach($job_scope as $k => $v){
if(is_int($k)){
$new_array[] = $v;
}else{
$new_array[] = [$k => $v];
}
}
print_r($new_array);
Output would be:
Array
(
[0] => proposal_id
[1] => Array
(
[will_provide] => Array
(
[0] => 0
[1] => Supervision
[2] => Labor
[3] => Material
[4] => Equpment
)
)
[2] => general_scope
[3] => Array
(
[per_bid] => Array
(
[0] => Yes
[1] => No
[2] => Omit
)
)
[4] => Array
(
[job_type] => Array
(
[0] => Painting
[1] => Sandblasting
[2] => Scappling
)
)
)
i have created a function in php to convert an string like:
[20110911, 20110913, [20110915, 20110918], 20110920, 20110922, 20110924, [20110926, 20110927], 20110929]
to php array like:
Array
(
[0] => 20110911
[1] => 20110913
[2] => Array
(
[0] => 20110915
[1] => 20110918
)
[3] => 20110920
[4] => 20110922
[5] => 20110924
[6] => Array
(
[0] => 20110926
[1] => 20110927
)
[7] => 20110929
[8] => Array
(
[0] => 20111001
[1] => 20111002
)
[9] => 20111004
[10] => Array
(
[0] => 20111006
[1] => 20111007
)
)
The function is:
function dates2Array($d){
if($d!==''){
$d=substr($d, 1, strlen($d)-2);
$d=explode(', ', $d);
$dates=array();
if(!empty($d)){
$j=1;
foreach($d as $k=>$v){
if(substr($v, 0, 1)==='[') $dates[]=array(substr($v, 1, strlen($v)));
elseif(substr($v, strlen($v)-1, strlen($v))===']'){
$dates[$k-$j][1]=substr($v, 0, strlen($v)-1);
$j++;
}
else $dates[]=$v;
}
}
}
return $d!==''?$dates:'';
}
I am not fully happy with my function.
I think it can be more optimized and compressed for speed..
Can it be?
Use JSON (json_decode() and json_encode()) instead
http://sandbox.phpcode.eu/g/b3814/2
result:
Array
(
[0] => 20110911
[1] => 20110913
[2] => Array
(
[0] => 20110915
[1] => 20110918
)
[3] => 20110920
[4] => 20110922
[5] => 20110924
[6] => Array
(
[0] => 20110926
[1] => 20110927
)
[7] => 20110929
)
You can pass your string in {...} and pass it to json_decode(str, true) to get back an array.
>> json_decode("[20110911, 20110913, [20110915, 20110918], 20110920, 20110922, 2
0110924, [20110926, 20110927], 20110929]")
array (
0 => 20110911,
1 => 20110913,
2 =>
array (
0 => 20110915,
1 => 20110918,
),
3 => 20110920,
4 => 20110922,
5 => 20110924,
6 =>
array (
0 => 20110926,
1 => 20110927,
),
7 => 20110929,
)