I try to import an XML file:
<works_list>
<licenses>
<name>First Company Name</name>
<work_address_list>
<address_place>
<address>36234020,first address </address>
<works>
<work>Service 1</work>
<work>Service 2</work>
<work>Service 3</work>
</works>
</address_place>
<address_place>
<address>36234020,second address</address>
<works>
<work>Service 1</work>
<work>Service 3</work>
</works>
</address_place>
</work_address_list>
</licenses>
<licenses>
...
</licenses>
</works_list>
and I want to put it in array:
$companyName = [
[
'address' => '36234020,first address',
'works' => ['Service 1', 'Service2', 'Service3']
],
[
'address' => '36234020,second address',
'works' => ['Service 1', 'Service3']
]
]
With this code
$node = simplexml_import_dom($doc->importNode($reader->expand(), true));
$companyName = (string) $node->name;
foreach ($node->work_address_list as $item) {
var_dump($item);
}
I get all elements in var_dump:
object(SimpleXMLElement)#29 (1) { ["address_place"]=> array(2) { [0]=> object(SimpleXMLElement)#30 (7) { ["address"]=> string(142) "36234020,first address" ["works"]=> object(SimpleXMLElement)#32 (1) { ["work"]=> array(3) { [0]=> string(121) "Service 1" [1]=> string(117) "Service 2" [2]=> string(117) "Service 3" } } } [1]=> object(SimpleXMLElement)#31 (7) { ["address"]=>'..'..}
How can I access all elements and put it in array?
I thought that $company_places[] = $item->address_place; will create two arrays but it doesn't work.
You can use simplexml_load_file if you're trying to read from an XML file. Then use json_encode and json_decode respectively.
<?php
$xml = simplexml_load_file('filename.xml');
$json = json_encode($xml);
$array = json_decode($json, true);
print_r($array);
Output:
Array
(
[licenses] => Array
(
[name] => First Company Name
[work_address_list] => Array
(
[address_place] => Array
(
[0] => Array
(
[address] => 36234020,first address
[works] => Array
(
[work] => Array
(
[0] => Service 1
[1] => Service 2
[2] => Service 3
)
)
)
[1] => Array
(
[address] => 36234020,second address
[works] => Array
(
[work] => Array
(
[0] => Service 1
[1] => Service 3
)
)
)
)
)
)
)
Related
I have the following array
Array
(
[tags] => Array
(
[0] => hello
)
[assignee] => 60b6a8a38cf91900695dd46b
[multiple_assignee] => Array
(
[0] => Array
(
[accountId] => 60b6a8a38cf91900695dd46b
)
[1] => Array
(
[accountId] => 5b39d23d32e26a2de15f174f
)
)
)
I want to remove 60b6a8a38cf91900695dd46b from the multiple_assignee array.
I have tried with the following code:
if (($key = array_search($this->getUsersHashMapValue($responsiblePartyIds[0]), $mutipleAssignee)) !== false) {
unset($mutipleAssignee[$key]['accountId']);
}
But it is not removing that element. The intention is I don't want to repeat the 60b6a8a38cf91900695dd46b assignee in the multiple assignee array.
I have also tried with the following code:
foreach($mutipleAssignee as $subKey => $subArray){
if($subArray['accountId'] == $this->getUsersHashMapValue($responsiblePartyIds[0])){
unset($mutipleAssignee[$subKey]);
}
}
But it is resulting as
Array
(
[tags] => Array
(
[0] => hello
)
[assignee] => 60b6a8a38cf91900695dd46b
[multiple_assignee] => Array
(
[1] => Array
(
[accountId] => 5b39d23d32e26a2de15f174f
)
)
)
rather than
[multiple_assignee] => Array
(
[0] => Array
(
[accountId] => 5b39d23d32e26a2de15f174f
)
)
Thank you
Just extract the accountId column and search that. Then use that key:
$key = array_search($this->getUsersHashMapValue($responsiblePartyIds[0]),
array_column($mutipleAssignee, 'accountId'));
unset($mutipleAssignee[$key]);
After your edit it seems you just want to reindex the subarray after unset:
$mutipleAssignee = array_values($mutipleAssignee);
I would just use a simple for loop. All of the array_* functions are really helpful but I find that they hide nuances. Since I don't have your functions I'm just making a plain-old one, but you should be able to port this.
$data = [
'tags' => [
'hello',
],
'assignee' => '60b6a8a38cf91900695dd46b',
'multiple_assignee' => [
[
'accountId' => '60b6a8a38cf91900695dd46b',
],
[
'accountId' => '5b39d23d32e26a2de15f174f',
],
],
];
$assignee = $data['assignee'];
foreach ($data['multiple_assignee'] as $multiple_assignee_key => $multiple_assignee) {
// if the root assignee is also listed in the multiple assignee area
if ($multiple_assignee['accountId'] === $assignee) {
// remove the duplicate from the multiple area
unset($data['multiple_assignee'][$multiple_assignee_key]);
// re-index the array
$data['multiple_assignee'] = array_values($data['multiple_assignee']);
}
}
This outputs:
array(3) {
["tags"]=>
array(1) {
[0]=>
string(5) "hello"
}
["assignee"]=>
string(24) "60b6a8a38cf91900695dd46b"
["multiple_assignee"]=>
array(1) {
[0]=>
array(1) {
["accountId"]=>
string(24) "5b39d23d32e26a2de15f174f"
}
}
}
Demo here: https://3v4l.org/tYppK
My array data :
$opt_val =
Array
(
[0] => Array
(
[0] => Array
(
[0] => 0|0|0|P3D
[1] => 0|0|1|P4D
[2] => 0|0|2|P5D
)
[1] => Array
(
[0] => 0|1|0|P3D
[1] => 0|1|1|P4D
[2] => 0|1|2|P5D
)
)
[1] => Array
(
[0] => Array
(
[0] => 1|0|0|P3D
[1] => 1|0|1|P4D
[2] => 1|0|2|P5D
)
[1] => Array
(
[0] => 1|1|0|P3D
[1] => 1|1|1|P4D
[2] => 1|1|2|P5D
)
)
)
I want to join above array with result :
Array
(
[0] => Array
(
[0] => 0|0|0|P3D#0|1|0|P3D (from Array[0][0][0]#Array[0][1][0])
[1] => 0|0|1|P4D#0|1|1|P4D
[2] => 0|0|2|P5D#0|1|2|P5D
)
[1] => Array
(
[0] => 1|0|0|P3D#1|1|0|P3D (from Array[1][0][0]#Array[1][1][0])
[1] => 1|0|1|P4D#1|1|1|P4D
[2] => 1|0|2|P5D#1|1|2|P5D
)
)
My code
for ($ov = 0; $ov < count($opt_val); $ov++) {
for ($ovi = 0; $ovi < count($opt_val[$ov]); $ovi++) {
for ($iv = 0; $iv < count($opt_val[$ov][$iv]); $iv++) {
$im_opt_val[$iv] = implode("#", $opt_val[$ov][$ovi]);
}
$impl_opt_val[$ov] = $im_opt_val;
}
}
Thank you
The following code snippet should do the trick.
<?php
declare(strict_types=1);
$opt_val = [
[
[
'0|0|0|P3D',
'0|0|1|P4D',
'0|0|2|P5D',
],
[
'0|1|0|P3D',
'0|1|1|P4D',
'0|1|2|P5D',
],
],
[
[
'1|0|0|P3D',
'1|0|1|P4D',
'1|0|2|P5D',
],
[
'1|1|0|P3D',
'1|1|1|P4D',
'1|1|2|P5D',
],
],
];
$result = [];
foreach ($opt_val as $outerArrayKey => $outerArray) {
foreach ($outerArray as $innerArray) {
foreach ($innerArray as $innerArrayKey => $innerArrayElement) {
if (!isset($result[$outerArrayKey][$innerArrayKey])) {
$result[$outerArrayKey][$innerArrayKey] = $innerArrayElement;
} else {
$result[$outerArrayKey][$innerArrayKey] .= '#'.$innerArrayElement;
}
}
}
}
var_dump($result);
The output would be:
array(2) {
[0]=>
array(3) {
[0]=>
string(19) "0|0|0|P3D#0|1|0|P3D"
[1]=>
string(19) "0|0|1|P4D#0|1|1|P4D"
[2]=>
string(19) "0|0|2|P5D#0|1|2|P5D"
}
[1]=>
array(3) {
[0]=>
string(19) "1|0|0|P3D#1|1|0|P3D"
[1]=>
string(19) "1|0|1|P4D#1|1|1|P4D"
[2]=>
string(19) "1|0|2|P5D#1|1|2|P5D"
}
}
I hope this work for you:
$finalFinalArray = [];
foreach($outerArray as $key => $innerArray){
$finalArray = [];
$inQueueArray = [];
foreach ($innerArray as $inInnerArray) {
$inQueueArray = array_merge($inQueueArray, $inInnerArray);
}
// Now, inner array is merged
for ($i = 0; $i < count($inQueueArray); $i++){
$finalArray[] = $inQueueArray[$i] + "#" + $inQueueArray[$i+3];
}
$finalFinalArray[] = $finalArray;
}
var_dump($finalFinalArray);
Didn't test it!
I have three following arrays.
Array ( [0] => 395 [1] => 295 )
Array ( [0] => 5a3a13f237715637629.jpeg [1] => 5a3b602654cfd527057.jpg )
Array ( [0] => Seller Test Product [1] => Offline Product for Test )
The first array is the quantity, the second array is for the images, the third array is for the name of the products.
I want to combine all these three array into one and display it using foreach loop in PHP.
if I use array_merge(), I am getting the output:
Array ( [0] => 395 [1] => 295 ) Array ( [0] => 5a3a13f237715637629.jpeg [1] => 5a3b602654cfd527057.jpg ) Array ( [0] => Seller Test Product [1] => Offline Product for Test ) Array ( [0] => 5a3a13f237715637629.jpeg [1] => 5a3b602654cfd527057.jpg [2] => Seller Test Product [3] => Offline Product for Test [4] => 395 [5] => 295 )
Now, how can I display it using foreach loop in the view.
in the view the code is :
<?php foreach($c as $key => $strs)
{ ?>
<img style="width:150px;" src="<?php echo #getimagesize(base_url("assets/upload/product/".$key)) ? base_url("assets/upload/product/".$key):'http://placehold.it/350x200';?>" class="img-responsive">
<input type="text" name="vala" value="<?php echo $strs; ?>">
<input type="text" name="valas" value="<?php echo $strss; ?>">
<?php } ?>
Any help is welcome.
So what you really want is to group all fields of all arrays together. Values with the same index shall be merged into a single object. array_map() can be used for this.
$final = array_map(function($quantity, $image, $name) {
return (object)['quantity' => $quantity, 'image' => $image, 'name' => $name];
}, $quantityArray, $imageArray, $nameArray);
The result will be:
[
{
'qunatity' => 395,
'image' => '5a3a13f237715637629.jpeg',
'name' => 'Seller Test Product'
},
{
'qunatity' => 295,
'image' => '5a3b602654cfd527057.jpeg',
'name' => 'Offline Product for Test'
}
]
You can then address them in your foreach like this:
foreach($final as $product) {
echo $product->name;
echo $product->image;
echo $product->quantity;
}
For those of you who are in a real hurry, the following call to array_map() will do the same trick, but without mapping the array fields to a specific key in the new multidimensional array:
$final = array_map(NULL, $quantityArray, $imageArray, $nameArray);
The result will be:
[
[
0 => 395,
1 => '5a3a13f237715637629.jpeg',
2 => 'Seller Test Product'
],
[
0 => 295,
1 => '5a3b602654cfd527057.jpeg',
2 => 'Offline Product for Test'
],
]
The inner arrays of the newly created mltidimensinal array will be filled in the order of which the arrays were provided to array_map().
You can loop one array and use key to get the corresponding value from the other arrays.
$a=array ( 0 => 395,1 => 295 );
$b=array ( 0 =>" 5a3a13f237715637629.jpeg" ,1 => "5a3b602654cfd527057.jpg" ) ;
$c=array ( 0 => "Seller Test Product",1 => "Offline Product for Test" );
Foreach($a as $key => $val){
$res[$key]['qty'] = $val;
$res[$key]['img'] = $b[$key];
$res[$key]['desc'] = $c[$key];
}
Var_dump($res);
Output:
array(2) {
[0]=>
array(3) {
["qty"]=> int(395)
["img"]=> string(25) " 5a3a13f237715637629.jpeg"
["desc"]=> string(19) "Seller Test Product"
}
[1]=>
array(3) {
["qty"]=> int(295)
["img"]=> string(23) "5a3b602654cfd527057.jpg"
["desc"]=> string(24) "Offline Product for Test"
}
}
https://3v4l.org/h8B0u
Example Code
<?php
$array1=array ( 0 => 395,1 => 295 );
$array2=array ( 0 =>" 5a3a13f237715637629.jpeg" ,1 => "5a3b602654cfd527057.jpg" ) ;
$array3=array ( 0 => "Seller Test Product",1 => "Offline Product for Test" );
echo "<pre>";
print_r($array1);print_r($array2);print_r($array3);
$result=array_merge($array1,$array2,$array3);
print_r($result);
?>
This is pretty basic, but my question is:
Given an array:
$a = array(
0 => array('Rate'=> array('type_id'=>1, 'name' => 'Rate_1', 'type'=>'day','value'=>10)),
1 => array('Rate'=> array('type_id'=>1, 'name' => 'Rate_2', 'type'=>'night','value'=>8)),
2 => array('Rate'=> array('type_id'=>2, 'name' => 'Rate_3', 'type'=>'day','value'=>7)),
3 => array('Rate'=> array('type_id'=>2, 'name' => 'Rate_4', 'type'=>'nigh','value'=>16)),
4 => array('Rate'=> array('type_id'=>3, 'name' => 'Rate_5', 'type'=>'day','value'=>10))
);
What is the most efficient way to change it so we have something like:
$new_array = array(
[type_id] => array(
[type] => array(
[value]
)
)
)
);
In other words, I would like to strip some data (the name, which I don't need) and reorganise the dimensions of the array. In the end I would have an array which I would be able to access the values by $new_array['type_id']['type']['value'].
Not entirely sure if this is exactly what you want, but with this you can access the values by saying
echo $new[TYPE_ID][DAY_OR_NIGHT];
$new = array();
foreach($a AS $b){
$c = $b['Rate'];
$new[$c['type_id']][$c['type']] = $c['value'];
}
Using print_r on $new would give you:
Array
(
[1] => Array
(
[day] => 10
[night] => 8
)
[2] => Array
(
[day] => 7
[night] => 16
)
[3] => Array
(
[day] => 10
)
)
Since php 5.3.0, array_reduce() allows using an array as the initial value, given your initial array $a, you can use the following code
function my_reducer ($result, $item) {
$result[$item['Rate']['type_id']][$item['Rate']['type']] = $item['Rate']['value'];
return $result;
}
$assoc_arr = array_reduce($a, 'my_reducer', array());
var_dump($assoc_arr);
This returns
array(3) { [1]=> array(2) {
["day"]=>
int(10)
["night"]=>
int(8) } [2]=> array(2) {
["day"]=>
int(7)
["nigh"]=>
int(16) } [3]=> array(1) {
["day"]=>
int(10) } }
I would like to calculate duplicate values in my array by "groupid":
Example:
Array
(
[0] => Array
(
[id] => 1230
[groupid] => 177
[activity_group_last] => 1229
[name] => First name
)
[1] => Array
(
[id] => 1231
[groupid] => 177
[activity_group_last] => 1229
[name] => Second name
)
[2] => Array
(
[id] => 1232
[groupid] => 178
[activity_group_last] => 1229
[name] => Other name
)
)
Output array (2 groupid = 177 and 1 groupid = 178):
Array
(
[0] => Array
(
[id] => 1231
[groupid] => 177
[activity_group_last] => 1229
[name] => Second name
[count] => 2
)
[1] => Array
(
[id] => 1232
[groupid] => 178
[activity_group_last] => 1229
[name] => Other name
[count] => 1
)
)
Thanks!
If $value contains your array, then:
$count = array_count_values(array_map(function($item) {
return $item['groupid'];
}, $value));
var_dump($count);
$_tmp = $count;
$unique = array_filter($value, function(&$item) use (&$_tmp, $count) {
if (!--$_tmp[$item['groupid']]) {
$item['count'] = $count[$item['groupid']];
return true;
}
return false;
});
var_dump($unique);
results in:
array(2) {
[1]=>
array(5) {
["id"]=>
int(1231)
["groupid"]=>
int(177)
["activity_group_last"]=>
int(1229)
["name"]=>
string(11) "Second name"
["count"]=>
int(2)
}
[2]=>
array(5) {
["id"]=>
int(1232)
["groupid"]=>
int(178)
["activity_group_last"]=>
int(1229)
["name"]=>
string(10) "Other name"
["count"]=>
int(1)
}
}
You can also do this using an iterative function. If your input array is stored in $input and you want the results in $output:
function remove_duplicates($input_array) {
$output_array = array(); // Create an empty array for output
foreach($input_array as $input) { // Loop the input array
if(array_key_exists($input['groupid'], $output_array)) {
// We've already seen this groupid at least once
// Increment count
$input['count'] = $output_array[$input['groupid']]['count'] + 1;
} else {
// First time we've seen this groupid
// Set count to 1
$input['count'] = 1;
}
// Store data in $output_array, indexed by group_id
$output_array[$input['groupid']] = $input;
}
}
// This is your input array
$input = array(array('id'=>1230,'groupid'=>177,'activity_group_last'=>1229,'name'=>'First name'),
array('id'=>1231,'groupid'=>177,'activity_group_last'=>1229,'name'=>'Second name'),
array('id'=>1232,'groupid'=>178,'activity_group_last'=>1229,'name'=>'Other name'));
// This will set the output array correctly
$output = remove_duplicates($input);