I have a string like this: "abc#gmail.com;ABC,xyz#gmail.com;XYZ" and I want to convert into array boject. How can I do that?
Below is sample
$noactivity_noassignedto = "abc#gmail.com;ABC,xyz#gmail.com;XYZ";
$assignedto = explode(';', $noactivity_noassignedto);
$assignedto = array((object) array("Email" => $assignedto[0], "Name" => $assignedto[1]));
$fromadd = 'some#abc.com';
$fromname = 'somename';
/*sending mail here*/
$this->init()->setsubject($sub)->addto($assignedto)->setfrom($fromadd, $fromname)->send();
Try this code:
$noactivity_noassignedto = "abc#gmail.com;ABC,xyz#gmail.com;XYZ";
$assignedto = explode(',', $noactivity_noassignedto);
foreach ($assignedto as $recipient) {
$tmp = explode(';', $recipient);
$recipients[] = (object)array("Email" => $tmp[0], "Name" => $tmp[1]);
}
$recipients = (object)$recipients;
$noactivity_noassignedto = "abc#gmail.com;ABC,xyz#gmail.com;XYZ";
$elements = explode(',', $noactivity_noassignedto);
foreach ($elements as $element) {
$dummy = explode(';', $element);
$assignedto[] = (object)array("Email" => $dummy[0], "Name" => $dummy[1]);
}
You need two explode
$noactivity_noassignedto = "abc#gmail.com;ABC,xyz#gmail.com;XYZ";
$assignedAll = explode(',', $noactivity_noassignedto);
// here you can put loop to send all, for now just first
$assignedto = explode(';', $assignedAll[0]);
$assignedto = array((object) array("Email" => $assignedto[0], "Name" => $assignedto[1]));
This will generate the array of objects that you want
$noactivity_noassignedto = "abc#gmail.com;ABC,xyz#gmail.com;XYZ";
$assignedto = explode(',', $noactivity_noassignedto);
foreach($assignedto as $item) {
list($email, $name) = explode(';', $item);
$addresses[] = (object)['Name'=>$name, 'Email'=>$email];
}
print_r($addresses);
Result: a print_r($addresses);
Array
(
[0] => stdClass Object
(
[Name] => ABC
[Email] => abc#gmail.com
)
[1] => stdClass Object
(
[Name] => XYZ
[Email] => xyz#gmail.com
)
)
You are on the right path. explode is what you are looking for to split a string into an array.
Given
$str = "abc#gmail.com;ABC";
$arr = explode($str, ';');
$arr will be array('abc#gmail.com', 'ABC');.
I think your problem is you want to invoke this multiple times. To do this:
$str = "abc#gmail.com;ABC,def#gmail.com;DEF";
$people = explode($str, ',');
foreach ($people as $person) {
$assigned_to = explode($person, ';');
// rest of your code
}
Related
I try to take each "name" and send it to my database for verification
the problem is that I can't take each one and go through the foreach
$name = $this->UserSkills->where('id', session()->get('id'))->first();
print_r($name) // return Array (
[id] => 5
[user] => 15
[name] => name,name1,name2,name3,name4
)
$name = implode(",", $name['name']);
foreach($name as $row) {
$setname[] = $this->checkName->getIDbyName($row['name']);
}
$setname = implode(",", $setname);
$data['id_name'] = $setname;
finally I want him back
echo $data['id_name'] // return: 1,4,8,367, etc...
Your first usage of implode is incorrect, you need explode to take a string and make it into an array.
<?php
$name = $this->UserSkills->where('id', session()->get('id'))->first();
$name = explode(",", $name['name']);
foreach($name as $row) {
$setname[] = $this->checkName->getIDbyName($row);
}
$setname = implode(",", $setname);
$data['id_name'] = $setname;
I think your $name['name'] returns string not array, So you need to explode() not to implode().
Try something like this:
$name = $this->UserSkills->where('id', session()->get('id'))->first();
print_r($name); // return Array ([id] => 5, [user] => 15, [name] => name,name1,name2,name3,name4)
$names = explode(',', $name['name']); // return Array (0 => name, 1 => name1, 2 => name2, 3 => name3, 4 => name4)
foreach ($names as $row) {
$setname[] = $this->checkName->getIDbyName($row['name']);
}
$setname = implode(",", $setname); // return 1,4,8,367, etc
$data['id_name'] = $setname; // return 1,4,8,367, etc
echo $data['id_name']; // return 1,4,8,367, etc
Assuming that your getIDbyName() return ID as integer
I have the following array in PHP
array(
"lat = -0.47023808202763651",
"lon = -163.04466494518647",
"alt = 4263.5330573538085",
"hgt = 0.382990122",
"nrm = 0.0816367865,0.996595144,-0.0115590692",
"rot = 0.34263891,-0.470143765,0.647551596,0.492179215",
"CoM = 0,0,0",
"stg = 0"
)
How could I convert this to an associative array where the keys are what's before equals and the values are what's after the equals:
array(
"lat" => "-0.47023808202763651",
"lon" => "-163.04466494518647",
"alt" => "4263.5330573538085",
"hgt" => "0.382990122",
"nrm" => "0.0816367865,0.996595144,-0.0115590692",
"rot" => "0.34263891,-0.470143765,0.647551596,0.492179215",
"CoM" => "0,0,0",
"stg" => "0"
)
I have seen walking an array here: Explode a string to associative array But have been unable to convert using this method...
Any tips? Sample code?
You need to loop the array and explode it and create an new array with key and value
$new_array = array();
foreach( $array as $value ){
list($key,$value)=explode('=',$value);
$new_array[trim($key)] = trim($value);
}
print_r($new_array);
Out put:
Array
(
[lat] => -0.47023808202763651
[lon] => -163.04466494518647
[alt] => 4263.5330573538085
[hgt] => 0.382990122
[nrm] => 0.0816367865,0.996595144,-0.0115590692
[rot] => 0.34263891,-0.470143765,0.647551596,0.492179215
[CoM] => 0,0,0
[stg] => 0
)
function splitStringsToArray($array) {
$need = [];
foreach ($array as $v) {
list($key, $value) = explode(' = ', $v);
$need[$key] = $value;
}
return $need;
}
$arrayYouHave = array(
"lat = -0.47023808202763651",
"lon = -163.04466494518647",
"alt = 4263.5330573538085",
"hgt = 0.382990122",
"nrm = 0.0816367865,0.996595144,-0.0115590692",
"rot = 0.34263891,-0.470143765,0.647551596,0.492179215",
"CoM = 0,0,0",
"stg = 0"
);
$arrayYouNeed = splitStringsToArray($arrayYouHave);
print_r($arrayYouHave, $arrayYouNeed);
Here is what I want to output:
Array
(
[0] => Array
(
[restriction_type_code] => CALORICONTROL
[restriction_detail_code] => 3000CAL
)
[1] => Array
(
[restriction_type_code] => GLUTENFREE
[restriction_detail_code] => NR
)
)
and my actual code looks like this:
restriction :
foreach ($restriction as $value)
{
$itemSplit = explode("||", $value);
$itemSplit1 = explode("|", $itemSplit[0]);
$itemSplit2 = explode("|", $itemSplit[0]);
$arrOrderDiet['restriction_type_code'][] = $itemSplit1 //CALORICONTROL;
$arrOrderDiet['restriction_detail_code'][] = $itemSplit2//3000CAL;
}
Im trying all the possibilities but I think i ran out of solutions.
Try this
foreach ($restriction as $value)
{
$itemSplit = explode("||", $value);
$itemSplit1 = explode("|", $itemSplit[0]);
$itemSplit2 = explode("|", $itemSplit[1]);
$arrOrderDiet[] = array('restriction_type_code' => $itemSplit1, 'restriction_detail_code' => $itemSplit2);
}
Edit:
foreach ($restriction as $value)
{
$itemSplit = explode("||", $value);
$itemSplit1 = explode("|", $itemSplit[0]);
$itemSplit2 = explode("|", $itemSplit[1]);
$arrOrderDiet[] = array('restriction_type_code' => $itemSplit1[0], 'restriction_detail_code' => $itemSplit2[2]);
}
Why do you have both indices the same in:
$itemSplit1 = explode("|", $itemSplit[0]);
$itemSplit2 = explode("|", $itemSplit[0]);
Shouldn't it be like this:
$itemSplit1 = explode("|", $itemSplit[0]);
$itemSplit2 = explode("|", $itemSplit[1]);
Plus:
$tmp['restriction_type_code'] = $itemSplit1[0];
$tmp['restriction_detail_code'] = $itemSplit2[0];
$arrOrderDiet[] = $tmp;
I'm having an array "pollAnswers" which displays:
Array
(
[0] => Sachin
[1] => Dhoni
)
in PHP and I want it to display as:
"pollAnswers":[
{"pollAnswersID":0, "pollAnswer":"Sachin"},
{"pollAnswersID":1, "pollAnswer":"Dhoni"}
]
in JSON output.
I've tried using array_fill_keys and array_flip but that's not solution for this. It seems I need to split the array_keys and array_values and then do some concatenation to get this, but I'm stuck here!
Online check link
Try this
$arr = array("Sachin", "Dhoni");
$sub_arr = array();
$final = array();
foreach($arr as $key => $val){
$sub_arr['pollAnswersId'] = $key;
$sub_arr['pollAnswer'] = $val;
$sub_final[] = $sub_arr;
}
$final['pollAnswers'] = $sub_final;
echo json_encode($final);
result
{"pollAnswers":[
{"pollAnswersId":0,"pollAnswer":"Sachin"},
{"pollAnswersId":1,"pollAnswer":"Dhoni"}
]}
You can try with array_map.
$Array = array('Sachin', 'Dhoni');
$new = array_map(function($v, $k) {
return ['pollAnswersId' => $k, 'pollAnswer' => $v]; // return the sub-array
}, $Array, array_keys($Array)); // Pass the values & keys
var_dump(json_encode(array("pollAnswers" => $new)));
Output
"{"pollAnswers":[
{"pollAnswersId":0,"pollAnswer":"Sachin"},
{"pollAnswersId":1,"pollAnswer":"Dhoni"}
]}"
For older versions of PHP.
return array('pollAnswersId' => $k, 'pollAnswer' => $v);
Fiddle
<?php
$answerArray = [];
foreach($yourArray as $key => $r)
$answerArray[] = ['pollAnswersId' => $key, 'pollAnswer' => $r];
echo json_encode($answerArray);
Here you go.
Try this:
$givenArray = array("Sachin","Dhoni");
$answerArray = [];
foreach($givenArray as $key => $r)
$answerArray[] = ['pollAnswersId' => $key, 'pollAnswer' => $r];
echo $out = json_encode(array('pollAnswers' => $answerArray));
I have multiple strings similar to:
$str = "/One/Two";
$str2 = "/One/Two/Flowers";
$str3 = "/One/Two/Grass";
$str4 = "/One/Another/Deeper";
$str5 = "/Uno/Dos/Cow";
I want to split it into a deep nested array that looks similar to the following:
Array
(
[One] => Array
(
[Two] => Array
(
[Flowers] =>
[Grass] =>
)
[Another] => Array
(
[Deeper] =>
)
)
[Uno] => Array
(
[Dos] => Array
(
[Cow] =>
)
)
)
This should do it:
$strings = array(
"/One/Two",
"/One/Two/Flowers",
"/One/Two/Grass",
"/One/Another/Deeper",
"/Uno/Dos/Cow"
);
$result = array();
foreach($strings as $string) {
$parts = array_filter(explode('/', $string));
$ref = &$result;
foreach($parts as $p) {
if(!isset($ref[$p])) {
$ref[$p] = array();
}
$ref = &$ref[$p];
}
$ref = null;
}
print_r($result);
Working example:
http://codepad.org/GmAoXLXp
Something like this should work. I couldn't think of any nice functional way to build the structure, so I fell back to a couple foreach loops.
<?php
$strings = array(
'/One/Two',
'/One/Two/Flowers',
'/One/Two/Grass',
'/One/Another/Deeper',
'/Uno/Dos/Cow'
);
$paths = array_map(
function ($e) {
return explode('/', trim($e, '/'));
},
$strings
);
$pathStructure = array();
foreach ($paths as $path) {
$ref =& $pathStructure;
foreach ($path as $dir) {
$ref =& $ref[$dir];
}
}
unset($ref);
print_r($pathStructure);