COMPANY ARRAY
array(1) {
[0]=> array(19) {
["entityid"]=> string(4) "3626"
["entityparentid"]=> string(1) "0"
["entityduplicateof"]=> string(1) "0"
["entitytype"]=> string(1) "0"
["entityname"]=> string(12) "Facebook Inc"
}
}
DISTANCE ARRAY
array(1) {
["distance"]=> string(4) "1.22"
}
What I'd like the output to look like:
array(1) {
[0]=> array(19) {
["entityid"]=> string(4) "3626"
["entityparentid"]=> string(1) "0"
["entityduplicateof"]=> string(1) "0"
["entitytype"]=> string(1) "0"
["entityname"]=> string(12) "Facebook Inc"
["distance"]=> string(4) "1.22" // here
}
}
Question:
array_push($company_array,$distance_array); seems to not do what I want it do.
It adds it to the end, but not where i want it to (notice the difference in where it is placed):
array(1) {
[0]=> array(19) {
["entityid"]=> string(4) "3626"
["entityparentid"]=> string(1) "0"
["entityduplicateof"]=> string(1) "0"
["entitytype"]=> string(1) "0"
["entityname"]=> string(12) "Facebook Inc"
},
["distance"]=> string(4) "1.22" // not here
}
It has another level inside $company, if you want the single array inside that another nesting, point it to index zero directly, and use array_merge:
$company[0] = array_merge($company[0], $distance);
Sample Output
Another way to merge the two arrays is the + operator:
$company[0] = $company[0] + $distance;
A detailed explanation of the difference between array_merge and the + can be found here.
Related
Hello i have an array_dif function between 2 arrays and the result it's not as it should.I don't understand why it does not return the status as difference. First array is data second is row and the third is the result. In the result it should also be status because the value is different.
$result = array_diff($data,$row );
array(9) {
["scooter_id"]=>
string(6) "RO0001"
["battery_lvl"]=>
string(2) "80"
["lat"]=>
string(9) "44.312150"
["lng"]=>
string(9) "23.872900"
["alt"]=>
string(1) "0"
["speed"]=>
string(1) "0"
["status"]=>
string(1) "2"
["ip"]=>
string(14) "213.233.101.62"
["port"]=>
int(24600)
}
array(11) {
["battery_lvl"]=>
string(2) "80"
["nr_satelites"]=>
string(1) "1"
["lat"]=>
string(9) "44.312154"
["longi"]=>
string(9) "23.873007"
["alt"]=>
string(1) "0"
["speed"]=>
string(1) "0"
["status"]=>
string(1) "1"
["location"]=>
string(7) "romania"
["ip"]=>
string(14) "213.233.101.62"
["port"]=>
string(5) "24600"
["status_intermediar"]=>
string(1) "2"
}
array(3) {
["scooter_id"]=>
string(6) "RO0001"
["lat"]=>
string(9) "44.312150"
["lng"]=>
string(9) "23.872900"
}
array_diff checks only the values.
Because your 2nd array contains ["status_intermediar"]=> string(1) "2" it finds the value so it doesn't see it as a difference
If you want to check both keys and values you should use array_diff_assoc
Also if you want to find all the different values from BOTH arrays you should run it twice
$difference1=array_diff_assoc($array1,$array2);
$difference2=array_diff_assoc($array2,$array1);
array_dif is one way function ("Compares array1 against one or more other arrays and returns the values in array1 that are not present in any of the other arrays."- https://www.php.net/manual/en/function.array-diff.php).
If you want all diffs, you have to call it twice: array_dif($first, $second) and array_dif($second, $one) and optionally merge results.
$array_difference1 = array_merge(array_diff($array1, $array2),
array_diff($array2, $array1));
$array_differnce = array_merge(array_diff($array_difference1, $array3),
array_diff($array3, $array_difference1));
array(2) {
[0]=>
object(stdClass)#21 (7) {
["id"]=>
string(1) "1"
["title"]=>
string(7) "cvxzcvd"
["con"]=>
string(10) "gvsdvgsdfg"
["is_important"]=>
string(1) "1"
["date"]=>
string(3) "123"
["image"]=>
string(2) "55"
["cat_id"]=>
string(1) "1"
}
[1]=>
object(stdClass)#22 (7) {
["id"]=>
string(1) "2"
["title"]=>
string(4) "fsdf"
["con"]=>
string(9) "dfdsfvfds"
["is_important"]=>
string(1) "1"
["date"]=>
string(4) "5145"
["image"]=>
string(7) "5454124"
["cat_id"]=>
string(1) "2"
}
}
I passed this array into a view
$this->load->view('home/index',$news_data);
but I wanna use the data separately.
I mean if I wanna use the second title
or the second data.
how can I express that in the view
thanks
You can go like this:-
$news_data[1]->title;
$news_data[1]->data;
Store the array into variable like
$news_data['arr'] = { } ;
,and in view file access it by $arr;
print_r($arr);
guys is that possible to calculate between (+,-,*,/) in multidimensional array?
example i have a multidimensional array in $menu_info with this following code :
array(3) {
[0]=>
array(5) {
["menu_order_id"]=>
string(3) "190"
["menu_name"]=>
string(13) "Golden Salmon"
["menu_variant"]=>
string(0) ""
["qty"]=>
string(1) "1"
["price"]=>
string(4) "15.4"
}
[1]=>
array(5) {
["menu_order_id"]=>
string(3) "191"
["menu_name"]=>
string(13) "Golden Salmon"
["menu_variant"]=>
string(0) ""
["qty"]=>
string(1) "1"
["price"]=>
string(4) "15.4"
}
[2]=>
array(5) {
["menu_order_id"]=>
string(3) "192"
["menu_name"]=>
string(13) "Golden Salmon"
["menu_variant"]=>
string(0) ""
["qty"]=>
string(1) "1"
["price"]=>
string(4) "15.4"
}
}
i want trying to calculate all of the price*qty like (15*1)+(15*1)+(15*1)
guys how to count the multidimensional array using math operator ?
thank you very much (:
p.s the length of array can be change.
You can try this:
$sum = array_sum(array_map(function($item) {
return $item['price']*$item['qty'];
}, $menu_info));
With some more explanation on your specific issue, I may be able to assist you in better alternatives.
I'm trying to combine two array in PHP with array_combine() function, but sometimes it working fine and sometimes it's not. I can't understand why it's working like this!
My Code:
var_dump($selectedDuretion);
var_dump($selectedDuretionType);
$combination = array_combine($selectedDuretion, $selectedDuretionType);
return $combination;
Expected OUTPUT:
array(4)
{
[0]=> string(1) "3"
[1]=> string(2) "12"
[2]=> string(1) "4"
[3]=> string(1) "3"
}
array(4)
{
[0]=> string(4) "days"
[1]=> string(4) "days"
[2]=> string(5) "weeks"
[3]=> string(5) "weeks"
}
{"3":"days","12":"days","3":"weeks","4":"weeks"}
Actual OUTPUT :
array(4)
{
[0]=> string(1) "3"
[1]=> string(2) "12"
[2]=> string(1) "4"
[3]=> string(1) "3"
}
array(4)
{
[0]=> string(4) "days"
[1]=> string(4) "days"
[2]=> string(5) "weeks"
[3]=> string(5) "weeks"
}
{"3":"weeks","12":"days","4":"weeks"}
The combination of arrays it shocking, I'll be thankful if anyone tell me why is this happening and how to solve it.
PHP Does not allow you to have duplicate indices in an array while JSON does allow you to have that for whatever reasons.
Since you are trying to convert PHP arrays to JSON your duplicate key gets eliminated. Hence you will have to manually build the JSON string.
$json="";
for($i=0;$i<count($selectedDuration);$i++)
{
$json.='"'.$selectedDuration[$i].'":"'.$selectedDurationType[$i].'",';
}
$json=rtrim($json,",");
$json="{".$json."}";
echo $json;
Output
{"3":"days","12":"days","4":"weeks","3":"weeks"}
Fiddle
first I'm looking help from any expert here for PHP array calling. I try using var_dump but still I can't call my data. I'm really a beginner and need a lot of help. Thanks before for read my question below...
<?php var_dump($this->product->customfieldsSorted); ?>
I use that and get this array list...
array(1) {
["normal"]=>
array(1) {
[0]=>
object(stdClass)#222 (36) {
["virtuemart_custom_id"]=>
string(2) "21"
["custom_parent_id"]=>
string(1) "0"
["virtuemart_vendor_id"]=>
string(1) "1"
["custom_jplugin_id"]=>
string(1) "0"
["custom_element"]=>
string(0) ""
["admin_only"]=>
string(1) "0"
["custom_title"]=>
string(5) "Slide"
["show_title"]=>
string(1) "0"
["custom_tip"]=>
string(0) ""
["custom_value"]=>
string(0) ""
["custom_desc"]=>
string(0) ""
["field_type"]=>
string(1) "M"
["is_list"]=>
string(1) "0"
["is_hidden"]=>
string(1) "1"
["is_cart_attribute"]=>
string(1) "0"
["is_input"]=>
string(1) "0"
["layout_pos"]=>
string(0) ""
["custom_params"]=>
string(26) "width="1024"|height="400"|"
["shared"]=>
string(1) "0"
["published"]=>
string(1) "1"
["ordering"]=>
string(1) "0"
["virtuemart_customfield_id"]=>
string(3) "110"
["virtuemart_product_id"]=>
string(2) "95"
["customfield_value"]=>
string(2) "15"
["customfield_price"]=>
NULL
["customfield_params"]=>
string(25) "width="200"|height="200"|"
["fpublished"]=>
string(1) "0"
["override"]=>
string(1) "0"
["disabler"]=>
string(1) "0"
["_varsToPushParamCustom"]=>
array(2) {
["width"]=>
array(2) {
[0]=>
string(3) "200"
[1]=>
string(6) "string"
}
["height"]=>
array(2) {
[0]=>
string(3) "200"
[1]=>
string(6) "string"
}
}
["_varsToPushParamCustomField"]=>
array(2) {
["width"]=>
array(2) {
[0]=>
string(3) "200"
[1]=>
string(6) "string"
}
["height"]=>
array(2) {
[0]=>
string(3) "200"
[1]=>
string(6) "string"
}
}
["_varsToPushParam"]=>
array(2) {
["width"]=>
array(2) {
[0]=>
string(3) "200"
[1]=>
string(6) "string"
}
["height"]=>
array(2) {
[0]=>
string(3) "200"
[1]=>
string(6) "string"
}
}
["width"]=>
string(3) "200"
["height"]=>
string(3) "200"
["_xParams"]=>
string(18) "customfield_params"
["display"]=>
string(101) "<img src="/angga/images/stories/virtuemart/product/resized/marinecap_200x200.jpg" alt="marinecap" />"
}
}
Nah I want to get the:
["display"]=>
string(101) "<img src="/angga/images/stories/virtuemart/product/resized/marinecap_200x200.jpg" alt="marinecap" />"
using this:
<?php echo $this->product->customfieldsSorted->display;?>
and got nothing. Can anyone help? I have no clue. None at all. Thanks before :)
$this->product->customfieldsSorted is an array with the key "normal", you can get corresponding value: $this->product->customfieldsSorted['normal'].
$this->product->customfieldsSorted['normal'] is an array with the key 0, get its value: $this->product->customfieldsSorted['normal'][0].
$this->product->customfieldsSorted['normal'][0] is an object of stdClass, you can get value of the property display using $this->product->customfieldsSorted['normal'][0]->display.