How can i replace/remove some string in this JSON ?.I think this problem can be solve using str_replace method or preg_replace
but i don't know how to add the regex
Please help me.
here my json
Array
(
[data] => Array
(
[0] => Array
(
[DESC] => bla bal bal
[SOLD] => 0
[contact_no] => 1234
[title] => Hiiiii
[price] => 10900
[big_image] => Array
(
[0] => http://example.com/images/user_adv/14.jpg
[1] => http://example.com/images/user_adv/15.jpg
)
[small_image] => Array
(
[0] => http://example.com/images/user_adv/small/14.jpg
[1] => http://example.com/images/user_adv/small/15.jpg
)
[tpe] => user
)
[1] => Array
(
[DESC] => fo fo fof ofof
[SOLD] => 0
[contact_no] => 234522
[title] => Hellooooo sddf
[price] => 0
[big_image] => Array
(
[0] => http://example.com/images/user_adv/154.jpg
[1] => http://example.com/images/user_adv/144.jpg
[2] => http://example.com/images/user_adv/147.jpg
)
[small_image] => Array
(
[0] => http://example.com/images/user_adv/small/154.jpg
[1] => http://example.com/images/user_adv/small/144.jpg
[2] => http://example.com/images/user_adv/small/147.jpg
)
[tpe] => user
)
)
[pis] => 3
[totals] => 23
[curpage] => 1
[total_ads] => 71
)
will i use this function to export the json to csv
function array_flatten ($nonFlat) {
$flat = array();
foreach (new RecursiveIteratorIterator(
new RecursiveArrayIterator($nonFlat)) as $k=>$v) {
$flat[$k] = $v;
}
return $flat;
}
$fp = fopen("output.csv","w");
foreach ($json['data'] as $fields) {
fputcsv($fp, array_flatten($fields));
}
fclose($fp);
the above code work fine but each image link is have one column so looks like bad , i need to make each group of pics on one column
I try to add regex to part of link images except the first image url [0] and merge the other with it , that i can putting them together in one column....
so for the experiment i add this to the above code but seems nothing happen
$flat[$k] = str_replace('[1-7] => http', "http", $v);
here i expect the output something like that
....
[big_image] => Array
(
[0] => http://example.com/images/user_adv/154.jpg
http://example.com/images/user_adv/144.jpg
http://example.com/images/user_adv/147.jpg
)
[small_image] => Array
(
[0] => http://example.com/images/user_adv/small/154.jpg
http://example.com/images/user_adv/small/144.jpg
http://example.com/images/user_adv/small/147.jpg
)
.....
edit this the .csv file output look like this
and I'm looking to be something like that
ok I've fixed by
foreach (
new RecursiveArrayIterator($nonFlat) as $k=>$v) {
$flat[$k] = is_array($v)?implode(" ",$v):$v;
now i got each group of images on one column
thanks :)
Related
I have an array which is as follows:
Array
(
[0] => Array
(
[postId] => 105
[postTitle] => Test
[postNonArray] => Subzero
[postDesc] => Array
(
[0] => Array
(
[para] => Subzero
[align] => L
)
)
[postDate] => 25.08.2016
[postTime] => 13:44
[postImage] => http://testyourprojects.biz/custom/ci/tharjumal/uploads/post/post_1472112857.png
[postVideo] =>
)
[1] => Array
(
[postId] => 106
[postTitle] => Test 2
[postNonArray] => Test
[postDesc] => Array
(
[0] => Array
(
[para] => Test
[align] => L
)
)
[postDate] => 26.08.2016
[postTime] => 18:08
[postImage] => http://testyourprojects.biz/custom/ci/tharjumal/uploads/post/post_1472215085.jpg
[postVideo] =>
)
[2] => Array
(
[postId] => 106
[postTitle] => Test 2
[postNonArray] => Test
[postDesc] => Array
(
[0] => Array
(
[para] => Test
[align] => L
)
)
[postDate] => 26.08.2016
[postTime] => 18:08
[postImage] => http://testyourprojects.biz/custom/ci/tharjumal/uploads/post/post_1472215085.jpg
[postVideo] =>
)
)
As you can see, there is two post details with postId=106;
How can I remove the redundant data from the array based on postId?
The project is on PHP.
I think this is what you are trying to achieve:-
$array = array_map("unserialize", array_unique(array_map("serialize", $array)));
echo "<pre/>";print_r($array);
Check output(whole code with your original array):- https://eval.in/630678
Note:- It will remove the duplicate values (so whole duplicate array will gone as you asked in comment)
I would suggest loop like the one below. It will go through all the elements from $your_array_name and will make an unique array of id where we will store the postIds. We will also check if there are duplicated in the $unique_ids array, and if so we will remove that duplicate element.
$unique_ids = array();
foreach($your_array_name as $key => $value){
//check if the postId is in the array of the unique ids
if(!in_array($value['postId'], $unique_ids)()){
array_push($unique_ids,$value['postId']); //if it is not - push it there
} else {
unset($your_array_name($key)); //if it is - remove the whole element from the array
}
}
You will need to loop the data and create a new array with unique values so here you go:
$ShowNewArray = array();
foreach($array as $key => $value){
if(!array_key_exists('postId', $ShowNewArray)){
$ShowNewArray[$value['postId']] = $value;
}
}
print_r($ShowNewArray);
Hope it will help you.
I have a query using a join on 2 tables which returns something like this :
Array
(
[0] => Array
(
[id] => 1
[description] => 'Test'
[image] => '1.jpg'
)
[1] => Array
(
[id] => 1
[description] => 'Test'
[image] => '2.jpg'
)
[2] => Array
(
[id] => 2
[description] => 'Test 2'
[image] => '11.jpg'
)
)
Is there a way to get an Array like this one :
Array
(
[0] => Array
(
[id] => 1
[description] => 'Test'
[image] => array('1.jpg', '2.jpg')
)
[2] => Array
(
[id] => 2
[description] => 'Test 2'
[image] => '11.jpg'
)
)
I want to group some indexes. For now, I use a loop and a if condition. But I want to know if someone use an other way.
As far as I know, there is no other way.
You can use Mysql GROUP_CONCAT, which will help you get this array :
Array
(
[0] => Array
(
[id] => 1
[description] => 'Test'
[image] => '1.jpg 2.jpg'
)
[2] => Array
(
[id] => 2
[description] => 'Test 2'
[image] => '11.jpg'
)
)
You might be able to split the image string using array_map :
$formattedResults = array_map($results, function($result){
$result['image'] = explode(' ', $result['image']);
return $result;
});
(Code might need some tuning to suit your need)
Try to implement this
$result = [];
foreach ($array as $key => $value) {
$hash = $value['id'];
if(isset($result[$hash])){
$temp[] = "{$value['image']}";
$result[$hash]['image'] = $temp;
}else{
$temp = array();
$temp[] = $value['image'];
$result[$hash] = $value;
}
}
for me its working..
I have an array like the following. This is the results of a query on one of our servers.
Array
(
[count] => 1
[0] => Array
(
[name] => Array
(
[count] => 1
[0] => mac
)
[0] => name
[staffid] => Array
(
[count] => 1
[0] => 1234
)
[1] => staffid
[school] => Array
(
[count] => 1
[0] => western
)
[2] => school
[count] => 3
[dn] => cn=mac,cn=staff
)
)
How do I loop through this array and create a new array as follows.
Array
(
[name] => mac
[staffid] => 1234
[school] => western
)
I've tried a foreach loop echoing the key & values, but I'm not sure where to go from there. There will be more results returned as the query is expanded, but original array layout will be the same and the new layout needs to be the same format.
Any ideas ?
Thanks
Try this:
$result = array();
foreach($yourArray as $element){
for($i=0;$i<$element['count']; $i++){
unset($element[$element[$i]]['count']);
$result[$element[$i]] = implode(', ', $element[$element[$i]]);
}
}
I was creating php code to convert the below json to csv
Array
(
[data] => Array
(
[0] => Array
(
[DESC] => bla bal bal
[SOLD] => 0
[contact_no] => 1234
[title] => Hiiiii
[price] => 10900
[big_image] => Array
(
[0] => http://example.com/images/user_adv/14.jpg
[1] => http://example.com/images/user_adv/15.jpg
)
[small_image] => Array
(
[0] => http://example.com/images/user_adv/small/14.jpg
[1] => http://example.com/images/user_adv/small/15.jpg
)
[tpe] => user
)
[1] => Array
(
[DESC] => fo fo fof ofof
[SOLD] => 0
[contact_no] => 234522
[title] => Hellooooo sddf
[price] => 0
[big_image] => Array
(
[0] => http://example.com/images/user_adv/154.jpg
[1] => http://example.com/images/user_adv/144.jpg
[2] => http://example.com/images/user_adv/147.jpg
)
[small_image] => Array
(
[0] => http://example.com/images/user_adv/small/154.jpg
[1] => http://example.com/images/user_adv/small/144.jpg
[2] => http://example.com/images/user_adv/small/147.jpg
)
[tpe] => user
)
)
[pis] => 3
[totals] => 23
[curpage] => 1
[total_ads] => 71
)
I've been using the below code to export it to .csv
$fp = fopen("output.csv","w");
foreach ($json['data'] as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
I can convert it fine, but I face a small issue that the sub array which is big_image & small_image is NOT appearing in the output file .csv (the row is empty)
[big_image] => Array
(
[0] => http://example.com/images/user_adv/154.jpg
[1] => http://example.com/images/user_adv/144.jpg
[2] => http://example.com/images/user_adv/147.jpg
)
[small_image] => Array
(
[0] => http://example.com/images/user_adv/small/154.jpg
[1] => http://example.com/images/user_adv/small/144.jpg
[2] => http://example.com/images/user_adv/small/147.jpg
)
By the way, if I replace:
foreach ($json['data'] as $fields) {
with
foreach ($json['data'][0] as $fields) {
I get the link pictures as output, so I need to merge them as one output
foreach ($json['data'] as $fields) {
foreach ($json['data'][0] as $fields2) {
edit :
here the output
edit 2 :
i expect the output something like that
You could make a function to make sure those nested arrays are made flat. You can create a utility function for that, like this:
function array_flatten ($nonFlat) {
$flat = array();
foreach (new RecursiveIteratorIterator(
new RecursiveArrayIterator($nonFlat)) as $k=>$v) {
$flat[$k] = $v;
}
return $flat;
}
And call it like this:
$fp = fopen("output.csv","w");
foreach ($json['data'] as $fields) {
fputcsv($fp, array_flatten($fields));
}
fclose($fp);
this is my array
Array
(
[0] => Array
(
[id] => 277558
[text_value] => Jif
[response_count] => 13
[response_percentage] => 92
)
[1] => Array
(
[id] => 277559
[text_value] => Peter Pan
[response_count] => 20
[response_percentage] => 6
)
)
after completing the operation the out put should be
Array
(
[0] => Array
(
[id] => 277558
[text_value] => Jif
[response_count] => 13
[response_percentage] => 92
[encode_param]=>ds!##^(*!ggsfh8236542jsdgf82*&61327
)
[1] => Array
(
[id] => 277559
[text_value] => Peter Pan
[response_count] => 20
[response_percentage] => 6
[encode_param]=>ds!##^(*!ggsfh8236542jsdgf82*&61327
)
)
you can see a new array value encode_paramis added
in that function do some encode algorithms
i have achieve this in the foreach looping statement
but i need to do it in array maping
Can anybody help thank u in advance
$encode_func = function($elem) { // declare function to encode
return $elem['text_value'];
}
$result = array_map(function($elem) use($encode_func) {
$elem['encode_param'] = $encode_func($elem);
return $elem;
}, $array);
Hope it helps.