I'm stuck and I just need a pointer.
I have this string with special characters retrieved from db and i want to segment it to many strings or arrays and in the process i want to exchange those characters with "()".
|p| stands for parent. |c| child.
Example sentence is :
my beautiful car is awesome
*where my , beautiful and car have a parent-child relationship.
The pattern for it :
|p|my|c||p|beautiful|c||c||p|car|p|is|p|awesome
In case you need to see the original array:
Array
(
[0] => Array
(
[name] => |p|my
[parent_id] => 0
[child] => Array
(
[0] => Array
(
[name] => |c||p|beautiful
[parent_id] => 1
[child] => Array
(
[0] => Array
(
[name] => |c||c||p|car
[parent_id] => 2
)
)
)
)
)
[1] => Array
(
[name] => |p|is
[parent_id] => 0
)
[2] => Array
(
[name] => |p|awesome
[parent_id] => 0
)
)
The ultimate goal after segmenting and replacement is:
((My(beautiful(car)) (is)(awesome))
<?php
$items =
[
[
'name' => 'foo',
'children' =>
[
[
'name' => 'bar',
'children' =>
[
[
'name' => 'baz',
]
]
]
]
],
[
'name' => 'bat',
],
[
'name' => 'man',
]
];
function bracketize($array) {
$output = '';
foreach($array as $item) {
$output .= '(';
$output .= $item['name'];
if(isset($item['children']))
$output .= bracketize($item['children']);
$output .= ')';
}
return $output;
}
$output = '(' . bracketize($items) . ')';
print $output;
Output:
((foo(bar(baz)))(bat)(man))
Since you requested only a pointer I suggest to start with a regex to get an intermediate array:
$str = '|p|my|c||p|beautiful|c||c||p|car|p|is|p|awesome';
$res = preg_split('/((?:\|c\|)*\|p\|)/', $str, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
var_export($res);
/*
array (
0 => '|p|',
1 => 'my',
2 => '|c||p|',
3 => 'beautiful',
4 => '|c||c||p|',
5 => 'car',
6 => '|p|',
7 => 'is',
8 => '|p|',
9 => 'awesome',
)
*/
Related
I am stuck on this i have multiple associative array and i want to convert in to one:-
Here is the array:-
Array
(
[0] => Array
(
[0] => Women
)
[1] => Array
(
[0] => children
[1] => smile
)
[2] => Array
(
[0] => Abstract
)
[3] => Array
(
[0] => Lion
[1] => Cheetah
)
)
I want output something like this:-
Array
(
[0] => Women
[1] => children
[2] => smile
[3] => Abstract
[4] => Lion
[5] => Cheetah
)
Here i have tried so far:-
$getKeywords = DB::table('contributor_images')->select('keywords')->get();
$getKeywords = json_decode(json_encode($getKeywords),true);
foreach($getKeywords as $keyword){
$AllKeywords[] = $keyword['keywords'];
}
foreach ($AllKeywords as $key => $ExplodeKeywords) {
$searchkeywords[] = explode(',',$ExplodeKeywords);
}
echo "<pre>"; print_r($searchkeywords); die;
I am using laravel framework of php. THANKS IN ADVANCE :)
You can use Laravel helper function array_flatten for this:
$array = [
0 => [
0 => 'Women',
],
1 => [
0 => 'children',
1 => 'smile',
],
2 => [
0 => 'Abstract',
],
3 => [
0 => 'Lion',
1 => 'Cheetah',
],
];
$result = array_flatten($array);
var_dump($result);
Output:
array (size=6)
0 => string 'Women' (length=5)
1 => string 'children' (length=8)
2 => string 'smile' (length=5)
3 => string 'Abstract' (length=8)
4 => string 'Lion' (length=4)
5 => string 'Cheetah' (length=7)
Try this:
foreach ($old as $data) {
foreach ($data as $value) {
$new[] = $value;
}
}
print_r($new);
}
In first foreach you are getting array inside array and in second foreach you will get the value. Insert these values in new array to get desired result. Use print_r to see the result
Simply you can use : call_user_func_array
<?php
$array = array (
0 =>
array (
0 => 'Women',
),
1 =>
array (
0 => 'children',
1 => 'smile',
),
2 =>
array (
0 => 'Abstract',
),
3 =>
array (
0 => 'Lion',
1 => 'Cheetah',
),
);
$result = call_user_func_array('array_merge', $array);
print_r($result);
?>
Output :
Array
(
[0] => Women
[1] => children
[2] => smile
[3] => Abstract
[4] => Lion
[5] => Cheetah
)
Check here : https://eval.in/829111
Ref : http://php.net/manual/en/function.call-user-func-array.php
Since Laravel 5.7 the syntax is Arr::flatten($array) so that now it looks like
use Illuminate\Support\Arr;
$array = [
0 => [
0 => "Women"
],
1 => [
0 => 'children',
1 => 'smile'
],
2 => [
0 => 'Abstract'
],
3 => [
0 => 'Lion',
1 => 'Cheetah'
]
];
Arr::flatten($array);
Output :
array:6 [
0 => "Women"
1 => "children"
2 => "smile"
3 => "Abstract"
4 => "Lion"
5 => "Cheetah"
]
Docs be here
So as you can see below I have an array that I get from an ajax request.. Now is my question how can I use the [name] as an array? The arrays below are two different arrays
Changing the arrays below is done in PHP
So this:
(
[name] => template[options][4892][is_delete]
[value] => 1
)
(
[name] => template[options][4892][name]
[value] => just_a_name
)
Into this
(
[template] => (
[options] => (
[4892] => (
name => just_a_name,
is_delete => 1
)
)
)
)
Edited: changed value to is_delete
Edit2: changed some things to make it more clear
Hope this is clear enough
$data = [
[
'name' => 'template[options][4892][is_delete]',
'value' => 1
],
[
'name' => 'template[options][4892][name]',
'value' => 'name'
]
];
$parsedData = [];
foreach ($data as $item) {
parse_str($item['name'] . '=' . $item['value'], $out);
$parsedData = array_replace_recursive($parsedData, $out);
}
print_r($parsedData);
result:
Array(
[template] => Array(
[options] => Array(
[4892] => Array(
[is_delete] => 1
[name] => name
)
)
)
)
I have three array (is about the data migration)
$a = Array
(
[0] => Array
(
[0] => province
[1] => 701
[2] => AA
[3] => A
)
..
)
$b = Array
(
[0] => Array
(
[0] => district
[1] => 70101
[2] => BB
[3] => B
)
[1] => Array
(
[0] => district
[1] => 70102
[2] => BB1
[3] => B1
)
..
)
$c = Array
(
[0] => Array
(
[0] => commune
[1] => 7010101
[2] => CC
[3] => C
),
[1] => Array
(
[0] => commune
[1] => 7010102
[2] => CC1
[3] => C1
)
..
)
What I want is to merge all $a , $b , $c' to become a new array
in this example array that have value701is the key of sub array70101and70101is the key of sub array7010101`
So the final array may look something like this:
$d = array (
701=>array(
70101=>array(7010101,7010102),
70102=>array(7010201,7010202),
),
)
The attempt is like the following:
# Your data structure here:
$a = array(
'701' => 'foo',
'702' => 'bar',
);
$b = array(
'70101' => 'foo-foo',
'70102' => 'foo-bar',
);
$c = array(
'7010101' => 'foo-foo-foo',
'7010102' => 'foo-foo-bar',
'7020101' => 'bar-foo-foo',
'7020201' => 'bar-bar-foo',
);
# The array you want
$buffer = array();
# Loop through the deepest elements (here: commune)
foreach ($c as $key => $value) {
# Find the keys for the parent groups
$province_key = substr($key, 0, 3);
$district_key = substr($key, 0, 5);
# Fill the buffer
$buffer[$province_key][$district_key][$key] = $value;
}
# Debug: The generated array
echo '<pre>';
print_r($buffer);
echo '</pre>';
You can copy&paste it here and hit run.
Test data:
$arrayA = [
[
0 => 'province',
1 => 701,
2 => 'AA',
3 => 'A'
],
[
0 => 'province',
1 => 702,
2 => 'AA1',
3 => 'A1'
],
];
$arrayB = [
[
0 => 'district',
1 => 70102,
2 => 'BB',
3 => 'B'
],
[
0 => 'district',
1 => 70101,
2 => 'BB1',
3 => 'B1'
],
];
$arrayC = [
[
0 => 'commune',
1 => 7010101,
2 => 'CC',
3 => 'C'
],
[
0 => 'commune',
1 => 7010102,
2 => 'CC1',
3 => 'C1'
]
];
Solution:
function mergeArraysToOneOnField(array $arrayA, array $arrayB, array $arrayC, $fieldName) {
$result = [];
/*
checks like
!is_string($fieldName) && !is_integer($fieldName)
*/
$arrayARelevantFields = array_column($arrayA, $fieldName);
$arrayBRelevantFields = array_column($arrayB, $fieldName);
$arrayCRelevantFields = array_column($arrayC, $fieldName);
foreach ($arrayARelevantFields as $arrayARelevantField) {
$arrayAFilteredRelevantField = filterArrayByStrpos($arrayBRelevantFields, $arrayARelevantField);
foreach ($arrayAFilteredRelevantField as $arrayBRelevantField) {
$result[$arrayARelevantField][$arrayBRelevantField] =
filterArrayByStrpos($arrayCRelevantFields, $arrayBRelevantField)
;
}
}
return $result;
}
Test run:
$mergedArray = mergeArraysToOneOnField($arrayA, $arrayB, $arrayC, 1);
print_r($mergedArray);
Test Result:
Array
(
[701] => Array
(
[70102] => Array
(
)
[70101] => Array
(
[0] => 7010101
[1] => 7010102
)
)
)
The solution can be extended with recursion, in order to hanlde a variable number of input arrays:
function mergeArraysToOneOnField(array &resultArray, array $inputAarray, $field) {
...
}
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
Output
Array ( [color] => green [0] => 2 [1] => 4 [2] => a [3] => b [shape] => trapezoid [4] => 4 )
we have an array like this :
Array
(
[0] => Array
(
[value] => aaa
[parent_id] => 5
)
[1] => Array
(
[value] => bbb
[parent_id] => 3
)
[2] => Array
(
[value] => ccc
[parent_id] => 3
)
)
as you can see 2 index have same [parent_id] , we need convert this array to this
Array
(
[0] => Array
(
[parent_id] => 5
[sub] => Array(
[0] =>(
[value] => aaa
)
)
)
[1] => Array
(
[parent_id] => 3
[sub] => Array(
[0] =>(
[value] => bbb
)
[1] =>(
[value] => ccc
)
)
)
)
in php we used this functions :
foreach ($Array as $item) {
$item['subs'] = array();
$indexedItems[$item['parent_id']] = (object) $item;
}
for($i=0; $i<count($Array); $i++){
if($Array[$i]['parent_id'] == $Array[$i-1]['parent_id']){
$indexedItems[$item['parent_id']]->subs[]=$Array[$i]['value'];
}
}
but it doesnot works , can you help us to do that , please ?
If u realy want that complicated array you gave as example you have to track the parent_id.
This is how :
<?php
$foo = [
[
'value' => 'aaa',
'parent_id' => 900,
],
[
'value' => 'aaa',
'parent_id' => 813,
],
[
'value' => 'aaa',
'parent_id' => 900,
],
[
'value' => 'aaa',
'parent_id' => 813,
],
];
$indexed = [];
foreach($foo as $f) {
$index = getParentIndex($indexed, $f['parent_id']);
if ($index === null) {
$indexed[] = [
'parent_id' => $f['parent_id'],
'subs' => [
['value' => $f['value'] ],
],
];
}else{
$indexed[$index]['subs'][] = [ 'value' => $f['value'], ];
}
}
function getParentIndex($array, $parent_id) {
for($i=0;$i<count($array);$i++) {
if ($array[$i]['parent_id'] == $parent_id) return $i;
}
return null;
}
var_dump($indexed);
However, this looks a very complicated array to work with imho.
I would suggest the following snippet :
$indexed = [];
foreach($foo as $f) {
if (!isset($indexed[$f['parent_id']])) $indexed[$f['parent_id']] = [];
$indexed[$f['parent_id']][] = $f['value'];
}
var_dump($indexed);
This will output an array similar to :
array (size=2)
900 =>
array (size=2)
0 => string 'aaa' (length=3)
1 => string 'aaa' (length=3)
813 =>
array (size=2)
0 => string 'aaa' (length=3)
1 => string 'aaa' (length=3)
How combine arrays in this way?
source:
Array
(
[0] => Array
(
[id] => 3
[title] => book
[tval] => 10000
)
[1] => Array
(
[id] => 3
[title] => book
[tval] => 1700
)
[3] => Array
(
[id] => 27
[title] => fruit
[tval] => 3000
)
.......
)
result:
Array
(
[0] => Array
(
[id] => 3
[title] => book
[tval] => 10000,1700
)
[1] => Array
(
[id] => 27
[title] => fruit
[tval] => 3000
)
.......
)
please help to solve this problem,
thanks!!!
sorry for bad english(
This should work:
$result = array();
foreach($array as $elem) {
$key = $elem['id'];
if (isset($result[$key])) {
$result[$key]['tval'] .= ',' . $elem['tval'];
} else {
$result[$key] = $elem;
}
}
This basically groups elements by id, concatenating tvals (separated by ,).
Simply building slightly on user576875's method:
$a = array ( 0 => array ( 'id' => 3,
'title' => 'book',
'tval' => 10000
),
1 => array ( 'id' => 3,
'title' => 'book',
'tval' => 1700
),
3 => array ( 'id' => 27,
'bcalias' => 'fruit',
'tval' => 3000
)
);
$result = array();
foreach ($a as $elem) {
$key = $elem['id'];
if (isset($result[$key])) {
$result[$key]['tval'] .= ',' . $elem['tval'];
} else {
$result[$key] = $elem;
}
}
$result = array_merge($result);
var_dump($result);
gives a result of:
array
0 =>
array
'id' => int 3
'title' => string 'book' (length=4)
'tval' => string '10000,1700' (length=10)
1 =>
array
'id' => int 27
'bcalias' => string 'fruit' (length=5)
'tval' => int 3000
The only real difference is the array_merge() to reset the keys