codeigniter: inserting value from array 1 to array 2? - php

i have an array like this
array 1
array(3) {
[0]=> string(2) "47"
[1]=> string(2) "48"
[2]=> string(2) "49"
}
i have plan to giving array with name, the array name is number
array 2
array(3) {
[0]=> object(stdClass)#18 (2) {
["address"]=> string(9) "Address 1"
["price"]=> string(16) "120000" }
[1]=> object(stdClass)#21 (2) {
["address"]=> string(9) "Address 2"
["price"]=> string(16) "150000" }
[2]=> object(stdClass)#20 (2) {
["address"]=> string(9) "Address 3"
["price"]=> string(16) "180000" }
}
I want to inserting array 1 into array 2 which same array key
I want to insert array 1 data into array 2 accordance with the key array . so i I was expecting to be joined both of arrays and became joined array like this
array(3) {
[0]=> object(stdClass)#18 (2) {
["address"]=> string(9) "Address 1"
["price"]=> string(16) "120000"
["number"]=> string(2) "47" }
[1]=> object(stdClass)#21 (2) {
["address"]=> string(9) "Address 2"
["price"]=> string(16) "150000"
["number"]=> string(2) "48"}
[2]=> object(stdClass)#20 (2) {
["address"]=> string(9) "Address 3"
["price"]=> string(16) "180000"
["number"]=> string(2) "49"}
}
is there any way to create or manipulate into an array like that ? my array is dynamically so number of array can be changed anytime.
I would greatly appreciate it if you could help me

Read up on the basic language control structures and foreach in particular.
foreach ($array2 as $index => $object) {
if (isset($array1[$index])) {
$object->number = $array1[$index];
}
}
Outcome:
array(3) {
[0]=>
object(stdClass)#1 (3) {
["address"]=>
string(9) "Address 1"
["price"]=>
string(6) "120000"
["number"]=>
string(2) "47"
}
[1]=>
object(stdClass)#2 (3) {
["address"]=>
string(9) "Address 2"
["price"]=>
string(6) "150000"
["number"]=>
string(2) "48"
}
[2]=>
object(stdClass)#3 (3) {
["address"]=>
string(9) "Address 3"
["price"]=>
string(6) "180000"
["number"]=>
string(2) "49"
}
}
Here is a Codepad demo

Related

PHP push new data into given array

I may seem having difficulty in understanding on how array works. So result, I can't work on the issue.
foreach($results as $key => $value){
$product_key = array(
'key' => $key
);
array_push($results, $product_key);
}
var_dump($results); exit;
Expected Output
array(2) {
[0]=>
object(stdClass)#21 (4) {
["items_id"]=>
string(1) "1"
["item_name"]=>
string(6) "laptop"
["price"]=>
string(5) "20000"
["quantity"]=>
string(2) "10"
["key"]=>
int(0)
}
[1]=>
object(stdClass)#22 (4) {
["items_id"]=>
string(1) "2"
["item_name"]=>
string(10) "smartphone"
["price"]=>
string(5) "10000"
["quantity"]=>
string(3) "200"
["key"]=>
int(1)
}
Unexpected Output
array(4) {
[0]=>
object(stdClass)#21 (4) {
["items_id"]=>
string(1) "1"
["item_name"]=>
string(6) "laptop"
["price"]=>
string(5) "20000"
["quantity"]=>
string(2) "10"
}
[1]=>
object(stdClass)#22 (4) {
["items_id"]=>
string(1) "2"
["item_name"]=>
string(10) "smartphone"
["price"]=>
string(5) "10000"
["quantity"]=>
string(3) "200"
}
[2]=>
array(1) {
["key"]=>
int(0)
}
[3]=>
array(1) {
["key"]=>
int(1)
}
}
You push new value (which is array) to the end of existsing array, what do you expect then?
If you want to modify current interated array value use this approach:
foreach($results as $key => $value) {
// use `->` as `$value` is object
$value->key = $key;
}
var_dump($results); exit;

Merge array where duplicate in multidimensional array

I've been trying to merge this array ONLY WHERE $array[4] exists more than one time, example: $array[4] == 'red' exactly twice. How can I merge only those arrays while keeping the others? I have made several attempts at this and I am willing to include my efforts if asked.
Consider this array:
array(3) {
[0]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "532"
[2]=>
string(1) "2"
[3]=>
string(9) "Domain(s)"
[4]=>
string(20) "red"
}
[1]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "532"
[2]=>
string(7) "License"
[3]=>
string(3) "Fee"
[4]=>
string(20) "red"
}
[2]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "536"
[2]=>
string(7) "License"
[3]=>
string(3) "Fee"
[4]=>
string(16) " University Test"
}
}
TRYING TO ACHIEVE:
array(3) {
[0]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "532"
[2]=>
string(1) "2"
[3]=>
string(9) "Domain(s)"
[4]=>
string(20) " red"
[5]=>
string(3) "Fee"
[6]=>
string(7) "License"
}
[1]=>
array(5) {
[0]=>
string(3) "UID"
[1]=>
string(3) "536"
[2]=>
string(7) "License"
[3]=>
string(3) "Fee"
[4]=>
string(16) " University Test"
}
}
foreach ($test as $item) {
if (!isset($merged[$item[4]])) {
// add the item to the merged array using key=$item[4]
$merged[$item[4]] = $item;
} else {
// merge the item with the item that is already in the array at key=$item[4]
$merged[$item[4]] = array_unique(array_merge($merged[$item[4]], $item));
// array_unique is necessary because array_merge will not overwrite numeric keys
}
}
// convert the keys back to numeric (if you care to)
$merged = array_values($merged);

PHP - multi-d array values changing between foreach loops

New PHP user left scratching my head. This function contains two foreach loops nested in if/else the page is index.php. On index.php, the code works fine, and the page shows the last four items in the array.
However, if the page is NOT index.php, the array seems to have been affected by code in between the first and second foreach loop, and only shows as a 1D array with the first-level keys as integers. How do I fix this? I've gone through virtually every PHP documentation I can find with no success.
function list_products_html($products) {
//If page is INDEX.PHP, show only last 4 products in reverse order with PayPal drop-down and Add To Cart
if ($_SERVER['PHP_SELF'] == "index.php") {
$products_reverse = array_reverse($products, true);
$products_new = array_slice($products_reverse, 0, 4, true);
foreach ($products_new as $product_new_id => $product_new) {
echo list_product_html($products_new);
echo generate_paypal_form($products_new);
echo '</li>';
}
//Else, show all products
} else {
foreach ($products as $product_id => $product) {
echo list_product_html($products);
echo generate_paypal_form($products);
echo '</li>';
}
}
}
In the first foreach loop, the var_dump shows my array as the following (which is correct):
array(8) { [101]=> array(5) { ["name"]=> string(15) "Logo Shirt, Red" ["img"]=> string(24) "img/shirts/shirt-101.jpg" ["price"]=> int(18) ["paypal"]=> string(13) "BJDE2BMPHMQ7Q" ["sizes"]=> array(4) { [0]=> string(5) "Small" [1]=> string(6) "Medium" [2]=> string(5) "Large" [3]=> string(7) "X-Large" } } [102]=> array(5) { ["name"]=> string(26) "Mike the Frog Shirt, Black" ["img"]=> string(24) "img/shirts/shirt-102.jpg" ["price"]=> int(20) ["paypal"]=> string(13) "2H6VLSYHY3QC8" ["sizes"]=> array(4) { [0]=> string(5) "Small" [1]=> string(6) "Medium" [2]=> string(5) "Large" [3]=> string(7) "X-Large" } } [103]=> array(5) { ["name"]=> string(25) "Mike the Frog Shirt, Blue" ["img"]=> string(24) "img/shirts/shirt-103.jpg" ["price"]=> int(20) ["paypal"]=> string(13) "MDP23L74U3F4L" ["sizes"]=> array(4) { [0]=> string(5) "Small" [1]=> string(6) "Medium" [2]=> string(5) "Large" [3]=> string(7) "X-Large" } } [104]=> array(5) { ["name"]=> string(17) "Logo Shirt, Green" ["img"]=> string(24) "img/shirts/shirt-104.jpg" ["price"]=> int(18) ["paypal"]=> string(13) "Q39L2XSPANB3Y" ["sizes"]=> array(4) { [0]=> string(5) "Small" [1]=> string(6) "Medium" [2]=> string(5) "Large" [3]=> string(7) "X-Large" } } [105]=> array(5) { ["name"]=> string(27) "Mike the Frog Shirt, Yellow" ["img"]=> string(24) "img/shirts/shirt-105.jpg" ["price"]=> int(25) ["paypal"]=> string(13) "7LVQ78CEPSKVN" ["sizes"]=> array(4) { [0]=> string(5) "Small" [1]=> string(6) "Medium" [2]=> string(5) "Large" [3]=> string(7) "X-Large" } } [106]=> array(5) { ["name"]=> string(16) "Logo Shirt, Gray" ["img"]=> string(24) "img/shirts/shirt-106.jpg" ["price"]=> int(20) ["paypal"]=> string(13) "G9A9UEVWCTDQN" ["sizes"]=> array(4) { [0]=> string(5) "Small" [1]=> string(6) "Medium" [2]=> string(5) "Large" [3]=> string(7) "X-Large" } } [107]=> array(5) { ["name"]=> string(21) "Logo Shirt, Turquoise" ["img"]=> string(24) "img/shirts/shirt-107.jpg" ["price"]=> int(20) ["paypal"]=> string(13) "NNERMRTJSTG6Q" ["sizes"]=> array(4) { [0]=> string(5) "Small" [1]=> string(6) "Medium" [2]=> string(5) "Large" [3]=> string(7) "X-Large" } } [108]=> array(5) { ["name"]=> string(18) "Logo Shirt, Orange" ["img"]=> string(24) "img/shirts/shirt-108.jpg" ["price"]=> int(25) ["paypal"]=> string(13)
In the SECOND foreach loop, I am getting the
"Warning: Invalid argument supplied for foreach()"
error, because var_dump for the same $products array shows the following:
int(101) int(102) int(103) int(104) int(105) int(106) int(107)
int(108)

How to get object like value:item:private from PHP Array

I have the array like this:
} ["items":"Jcart":private]=> array(3) {
[0]=>
string(1) "3"
[1]=>
string(1) "2"
[2]=>
string(7) "ABC-123" }
How to get the "items" values in a php variable?
///////
Te complete object is:
object(Jcart)#1 (8) {
["config"]=>
array(12) {
["jcartPath"]=>
string(6) "jcart/"
["checkoutPath"]=>
string(12) "checkout.php"
["item"]=>
array(6) {
["id"]=>
string(10) "my-item-id"
["name"]=>
string(12) "my-item-name"
["price"]=>
string(13) "my-item-price"
["qty"]=>
string(11) "my-item-qty"
["url"]=>
string(11) "my-item-url"
["add"]=>
string(13) "my-add-button"
}
["paypal"]=>
array(5) {
["id"]=>
string(38) "seller_1282188508_biz#conceptlogic.com"
["https"]=>
bool(true)
["sandbox"]=>
bool(false)
["returnUrl"]=>
string(0) ""
["notifyUrl"]=>
string(0) ""
}
["currencyCode"]=>
string(3) "USD"
["csrfToken"]=>
bool(false)
["text"]=>
array(14) {
["cartTitle"]=>
string(13) "Shopping Cart"
["singleItem"]=>
string(4) "Item"
["multipleItems"]=>
string(5) "Items"
["subtotal"]=>
string(8) "Subtotal"
["update"]=>
string(6) "update"
["checkout"]=>
string(8) "checkout"
["checkoutPaypal"]=>
string(20) "Checkout with PayPal"
["removeLink"]=>
string(6) "remove"
["emptyButton"]=>
string(5) "empty"
["emptyMessage"]=>
string(19) "Your cart is empty!"
["itemAdded"]=>
string(11) "Item added!"
["priceError"]=>
string(21) "Invalid price format!"
["quantityError"]=>
string(38) "Item quantities must be whole numbers!"
["checkoutError"]=>
string(34) "Your order could not be processed!"
}
["button"]=>
array(4) {
["checkout"]=>
string(0) ""
["paypal"]=>
string(0) ""
["update"]=>
string(0) ""
["empty"]=>
string(0) ""
}
["tooltip"]=>
bool(true)
["decimalQtys"]=>
bool(false)
["decimalPlaces"]=>
int(1)
["priceFormat"]=>
array(3) {
["decimals"]=>
int(2)
["dec_point"]=>
string(1) "."
["thousands_sep"]=>
string(1) ","
}
}
["items":"Jcart":private]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "2"
}
["names":"Jcart":private]=>
array(2) {
[3]=>
string(12) "Hockey Stick"
[2]=>
string(13) "Baseball Mitt"
}
["prices":"Jcart":private]=>
array(2) {
[3]=>
string(5) "33.25"
[2]=>
string(5) "19.50"
}
["qtys":"Jcart":private]=>
array(2) {
[3]=>
string(1) "1"
[2]=>
string(2) "20"
}
["urls":"Jcart":private]=>
array(2) {
[3]=>
string(15) "http://bing.com"
[2]=>
string(16) "http://yahoo.com"
}
["subtotal":"Jcart":private]=>
float(423.25)
["itemCount":"Jcart":private]=>
int(21)
}
I just need the values in ["items":"Jcart":private]
I assume you got this by casting an object to an array. If that's the case, you shouldn't be doing that! The property is private because you're not supposed to access it directly because it's not a public API. The object should have a public method which you're supposed to call instead, that's the public API. Something like $foo->getItems() or such. Read the documentation and/or source code.

get array content in php

I want to get data from an array using PHP loop like FOR or FOREACH.
when I print the content of the array using var_dump it looks like this:
array(2) {
[0]=>
array(2) {
["name"]=>
string(6) "query1"
["fql_result_set"]=>
array(13) {
[0]=>
array(3) {
["aid"]=>
string(19) "2944783819003364347"
["name"]=>
string(16) "Profile Pictures"
["photo_count"]=>
string(2) "37"
}
[1]=>
array(3) {
["aid"]=>
string(19) "2944783820076875780"
["name"]=>
string(7) "only me"
["photo_count"]=>
string(1) "2"
}
[2]=>
array(3) {
["aid"]=>
string(19) "2944783819003517141"
["name"]=>
string(35) "Ways To Tie Your Shoelaces. Nice :)"
["photo_count"]=>
string(1) "8"
}
[3]=>
array(3) {
["aid"]=>
string(19) "2944783819003490957"
["name"]=>
string(12) "Cover Photos"
["photo_count"]=>
string(2) "12"
}
[4]=>
array(3) {
["aid"]=>
string(19) "2944783819003481818"
["name"]=>
string(14) "Mobile Uploads"
["photo_count"]=>
string(2) "55"
}
}
}
what should I do to get: ["aid"] , ["name" ] and ["photo_count"]?
like this
foreach ($array[0]['fql_result_set'] as $record) {
echo "{$record['aid']}, {$record['name']}";
}

Categories